Secant Method - Matlab Code - M File, Study Guides, Projects, Research of Mathematical Methods for Numerical Analysis and Optimization

This is Matlab code for Secant Method. Just copy and paste this code into an m-file and run. It works perfect.

Typology: Study Guides, Projects, Research

2012/2013
On special offer
30 Points
Discount

Limited-time offer


Uploaded on 06/19/2013

naachiz
naachiz 🇵🇰

4.5

(24)

34 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
% ============= %
% SECANT METHOD %
% ============= %
% Find the roots of using Secant Method
% func --> function is taken by user
% like sin(x) or x^2 + x - 1 or any other but use the same format
% i.e. use paranthesis as used above with 'x' for sin, cos,... etc
% and spacing between '+' or '-' operation
% a --> Xn-1
% b --> Xn
% c --> Xn+1
% maxerr --> Maximum Error in Root
syms x;
func = input('Enter Function in terms of x: ');
a = input('Ener Lower Limit: ');
b = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
f = inline(func);
c = (a*f(b) - b*f(a))/(f(b) - f(a));
pf2
Discount

On special offer

Partial preview of the text

Download Secant Method - Matlab Code - M File and more Study Guides, Projects, Research Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

% SECANT METHOD %

% Find the roots of using Secant Method % func --> function is taken by user % like sin(x) or x^2 + x - 1 or any other but use the same format % i.e. use paranthesis as used above with 'x' for sin, cos,... etc % and spacing between '+' or '-' operation % a --> Xn- % b --> Xn % c --> Xn+ % maxerr --> Maximum Error in Root syms x; func = input('Enter Function in terms of x: '); a = input('Ener Lower Limit: '); b = input('Ener Upper Limit: '); maxerr = input('Enter Maximum Error: '); f = inline(func); c = (af(b) - bf(a))/(f(b) - f(a));

disp(' Xn-1 f(Xn-1) Xn f(Xn) Xn+1 f(Xn+1)'); disp([a f(a) b f(b) c f(c)]); while abs(f(c)) > maxerr b = a; a = c; c = (af(b) - bf(a))/(f(b) - f(a)); disp([a f(a) b f(b) c f(c)]); end display(['Root is x =' num2str(c)]); % ========= % % Thank you % ========= %