Numerical Methods
Root Finding
Assignment
BSME 18-22
Muhammad Ahmed Zaheer
1
2. Write Matlab function m-files for Bisection method, Secant Method, False
position Method, Newton Raphson method.
Bisection Method
function [mid,error,itr] = bisectionmethod(func,a,b,toll,N)
%this function uses bisection method and takes func a function input a and
%b as input limits toll as tellerance and N as number of iterations and
%gives mid point as approx root and error and number of iterations as an
%output
syms x
%syms x for taking function in terms of x
itr=0;
iterplot=[];
%this for ploting error against number of iterations
error=[];
errorcheck=100;
%first rough check to operate atleast once
mid=0.5*(a+b);
if double(subs(func,x,a))*double(subs(func,x,b))<0
while errorcheck>toll && itr<N
oldmp=mid;
%oldmp and new mid will compute error
if double(subs(func,x,a))*double(subs(func,x,mid))<0
b=mid;
elseif double(subs(func,x,b))*double(subs(func,x,mid))<0
a=mid;
end
2
mid=0.5*(a+b);
itr=itr+1;
iterplot(itr)=itr;
error(itr)=abs(oldmp)-abs(mid);
errorcheck=error(itr);
end
else
fprintf('The root is not in the given limits\n')
end
fprintf('Number of Iteration is %g\n',itr)
fprintf('\nThe root is %g\n',mid)
fprintf('Error is %g\n',error(itr))
plot(iterplot,error);
return
end
Secant Method
function [p,error,itr] = secantmethod(func,p_1,p_2,toll,N)
syms x
itr=0;
error=[];
ploterror=[];
check=10;
while check>toll && itr<N
a=double(subs(func,x,p_1));
b=double(subs(func,x,p_2));
p=p_2-((p_2-p_1)*b)/(b-a);
3
itr=itr+1;
error(itr)=abs(abs(p)-abs(p_2));
check=error(itr);
ploterror(itr)=itr;
p_1=p_2;
p_2=p;
end
fprintf('The approx root in %g iterations is %g with error
%g',itr,p,error(itr));
plot(ploterror,error);
return
end
False Position Method
function [p,error,itr] = falsepositionmethod(func,p_1,p_2,toll,N)
syms x
itr=0;
error=[];
ploterror=[];
check=10;
if double(subs(func,x,p_1))*double(subs(func,x,p_2))>0
fprintf('The root does not lie in the given limits\n')
else
while check>toll && itr<N
a=double(subs(func,x,p_1));
b=double(subs(func,x,p_2));
p=p_2-((p_2-p_1)*b)/(b-a);
4
itr=itr+1;
error(itr)=abs(abs(p)-abs(p_2));
check=error(itr);
ploterror(itr)=itr;
if double(subs(func,x,p_1))*double(subs(func,x,p))<0
p_2=p;
elseif double(subs(func,x,p_2))*double(subs(func,x,p))<0
p_1=p;
end
end
fprintf('The approx root in %g iterations is %g with error
%g',itr,p,error(itr));
plot(ploterror,error);
end
return
end
Newton-Rapson Method
function [p,error,itr] = newtonmethod(func,p,toll,N)
syms x
dfunc=diff(func);
itr=0;
error=[];
ploterror=[];
check=10;
while check>toll && itr<N
oldp=p;
5
a=subs(func,x,p);
b=subs(dfunc,x,p);
p=p-(a/b);
itr=itr+1;
error(itr)=abs(p-oldp);
ploterror(itr)=itr;
check=error(itr);
end
fprintf('Number of Iteration is %g\n',itr)
fprintf('\nThe root is %g\n',p)
fprintf('Error is %g\n',error(itr))
plot(ploterror,error);
return
end
3. Find the solution of 2 different equations (one algebraic and one non-algebraic)
using Matlab function m-file. Estimate the error in each case and plot them.
Algebraic Equation
f(x)= x^3+3*x^2+4*x+2
6
7
False position
8
Secant Method
9
Newton method
10
Non Algebraic Equation
F(x)=ln(4x)-x
Bisection method
11
Secant
12
False position
13
Newton Method
14
4. Make a comparison of different root finding methods by plotting the error of
each method on the same plot.
Code
function compare1(f,a,b,TOL,N)
[~,~,~,~,~,~,Bisect] = bisect3(f,a,b,N,TOL)
[~,~, ~, ~, ~, ~,False]=false2(f,a,b,TOL,N)
[~, ~,~, Secant, ~, ~, ~]=secant(f,a,2,TOL,N)
[~, ~, Newton, ~, ~]=newton3(f,b,TOL,N)
clc
figure(1)
subplot(5,1,1)
15
plot(1:length(Bisect),Bisect,'b')
title('Bisection Method')
xlabel('Number of iterations');
ylabel('Xn-Xn-1');
xlim([1 length(Bisect)])
subplot(5,1,2)
plot(1:length(False),False,'k')
title('False Position Method')
xlabel('Number of iterations')
ylabel('Xn-Xn-1')
xlim([1 length(False)])
subplot(5,1,3)
plot(1:length(Secant),Secant,'r')
title('Secant Method')
xlabel('Number of iterations')
ylabel('Xn-Xn-1')
xlim([1 length(Secant)])
subplot(5,1,4)
plot(1:length(Newton),Newton,'g')
title('Secant Method')
xlabel('Number of iterations')
ylabel('Xn-Xn-1')
xlim([1 length(Newton)])
subplot(5,1,5)
plot(1:length(Secant),Secant,'r',1:length(Newton),Newton,'g',1:length(Bisect)
,Bisect,'b',1:length(False),False,'k')
title('Error Vs Number of iteration of All Methods','Fontsize',15);
xlabel('Number of iterations');
ylabel('Xn-Xn-1');
grid on;
grid minor;
legend('Secant','Newton','Bisect','False');
figure(2)
plot(1:length(Secant),Secant,'r',1:length(Newton),Newton,'g',1:length(Bisect)
,Bisect,'b',1:length(False),False,'k')
title('Error Vs Number of iteration of All Methods','Fontsize',15);
xlabel('Number of iterations');
ylabel('Xn-Xn-1');
xlim([1 inf])
grid on;
grid minor;
legend('Secant','Newton','Bisect','False');
%To see clear comparison
figure(3)
plot(1:length(Secant),Secant,'r',1:length(Newton),Newton,'g',1:length(Bisect)
,Bisect,'b',1:length(False),False,'k')
title('Error Vs Number of iteration of All Methods','Fontsize',15);
xlabel('Number of iterations');
ylabel('Xn-Xn-1');
xlim([1 inf])
ylim([0 0.95])
grid on;
grid minor;
legend('Secant','Newton','Bisect','False');
end
16
Algebraic Equation
f(x)= x^3+3*x^2+4*x+2
17
18
Non Algebraic Function
19
20
5. Compare the solution of your methods with the Matlab built in function
“roots” and "fzero"
Equation 1: f(x)= 2x3+x2+3x
With using ‘roots’ function
With using ‘fzero’ function