0% found this document useful (0 votes)
2K views

Secent Method MATLAB Code

This lab report discusses two root-finding algorithms: the secant method and the false position method. For the secant method, the report provides code in MATLAB to approximate the root of a function using successive secant lines. For the false position method, the report explains the algorithm and provides MATLAB code to iteratively find a root within a specified tolerance using test values and adjustment. The report demonstrates running both algorithms on example functions to find approximate roots.

Uploaded by

asad malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Secent Method MATLAB Code

This lab report discusses two root-finding algorithms: the secant method and the false position method. For the secant method, the report provides code in MATLAB to approximate the root of a function using successive secant lines. For the false position method, the report explains the algorithm and provides MATLAB code to iteratively find a root within a specified tolerance using test values and adjustment. The report demonstrates running both algorithms on example functions to find approximate roots.

Uploaded by

asad malik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Report # 03

 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)

x(1)=input('Enter first point of guess interval: ');


x(2)=input('Enter second point of guess interval: ');
n=input('Enter allowed Error in calculation: ');
iteration=0;

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

Enter first point of guess interval: 0

Enter second point of guess interval: -0.1

Enter allowed Error in calculation: 0.001

root =

-0.6593

iteration =

6
Result:
I find the approximate root of both of given function.

 False position method:

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

1 850734e-01 0.736299 -0.00466004

2 362990e-01 0.738945 -0.000233926

3 389454e-01 0.739078 -1.17192e-05

Solution 0.739085 Accuracy -5.87047e-07 iterations 4

Result:

I find the approximate root of false position method


False position method in matlab:

x0 = input('enter the value of x0 = ');


x1 = input('enter the value of x1 = ');
tolerance=input('inter the tolerance = ');
f =@(x) sin(2*pi*x)+ exp(1.2*x) + x - 2.5;
for i=0:inf
x2= x1 - (f(x1)* (x1-x0)/(f(x1)-f(x0)))
c = f(x2)
absolute_c= abs(c);
if absolute_c < tolerance
break
end
if f(x0)*c <0
x1=x2;
continue
else
x0=x2;
continue
end
end

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

You might also like