Secent Method MATLAB Code
Secent Method MATLAB Code
Secant method:
Objective:
We’ll go through a program for Secant method in MATLAB with example.
Introduction:
In numerical analysis, the secant method is a root-finding algorithm that uses a succession
of roots of secant lines to better approximate a root of a function f. The secant method can be thought of
as a finite-difference approximation of Newton's method.
Secant method in matlab:
% Secant Method in MATLAB
a=input('Enter function:','s');
f=inline(a)
for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) - f(x(i-2))));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<n
root=x(i)
iteration=iteration
break
end
end
By Puttting value of function:
Enter function:cos(x)+2*sin(x)+x^2
f=
Inline function:
f(x) = cos(x)+2*sin(x)+x^2
root =
-0.6593
iteration =
6
Result:
I find the approximate root of both of given function.
Objective:
To follow the algorithm of the false-position method of solving a nonlinear equation
Introduction:
In mathematics, the false position method or regular falsi is a very old method for solving an equation in
one unknown, that, in modified form, is still in use. In simple terms, the method is the trial and
error technique of using test ("false") values for the variable and then adjusting the test value according
to the outcome. This is sometimes also referred to as "guess and check
False position method in matlab:
function FaslePosition
syms f(x)
f(x)=x-cos(x);
a=0;b=1;Tol=1e-5;
N0=100;
FA=eval(f(a));
FB=eval(f(b));
i=0;
while i<=N0
p=b-(FB*(b-a))/(FB-FA);%(a+b)/2;
FP=eval(f(p));
if abs(FP)<=Tol
fprintf('Solution %g Accuracy %g iterations %d\n',p,FP,i);
return
end
fprintf('%d %d %d %g %g\n',i,a,b,p,FP)
i=i+1;
if FA*FP>0
a=p;
FA=FP;
else
b=p;
FB=FP
end
end
fprintf('Method failed after %d iterations',i);
end
output:
>> FaslePosition
0 1 0.685073 -0.0892993
Result:
Example;
x0=-6
x1=6
Tolerance=0.001
It reached the end at i==590
Result:
I find the approximate root of false position method