0% found this document useful (0 votes)
86 views1 page

Secant Method MATLAB Implementation

This MATLAB function implements the secant method to find the root of a function. It takes in the function, initial guesses for the root, tolerance, and maximum number of iterations. It iteratively computes new guesses for the root using the secant line formula until the change in guesses is less than the tolerance or the maximum number of iterations is reached. It displays the results of each iteration.

Uploaded by

j000ys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views1 page

Secant Method MATLAB Implementation

This MATLAB function implements the secant method to find the root of a function. It takes in the function, initial guesses for the root, tolerance, and maximum number of iterations. It iteratively computes new guesses for the root using the secant line formula until the change in guesses is less than the tolerance or the maximum number of iterations is reached. It displays the results of each iteration.

Uploaded by

j000ys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The

Secant Method



function [ iter ] = mysecant1(f,x0,x1, tol,n)
%UNTITLED3 Summary of this function goes here--please write
% Detailed explanation goes here -please write
%
%
%
%
iter=0;
u=feval(f, x0);
v=feval(f,x1);
err=abs(x1-x0);
disp('-------------------------------------------')
disp('iter xn f(xn) |xn+1-xn| ')
disp('-------------------------------------------')
fprintf('%2.0f %12.6f %12.6f\n', iter, x0,u)
fprintf('%2.0f %12.6f %12.6f %12.6f\n', iter, x1, v, err)
%you can modigy fprintf and diplay more decimal points.
while (err>tol)&(iter<=n)&((v-u)~=0)
x=x1-v*(x1-x0)/(v-u);
%see the formula for the secant line method.
x0=x1;
u=v;
x1=x;
v=feval(f,x1);
err=abs(x1-x0);
iter=iter+1;
fprintf('%2.0f %12.6f %12.6f %12.6f\n', iter,x1,v,err)
%you can modigy fprintf and diplay more decimal points.
end
if((v-u)==0)
disp(' division by zero')
end
if (iter>n)
disp(' Method failed to converge')
end

end

You might also like