In: Advanced Math
Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of scalar nonlinear equa-tions. Use these programs to compute approximations to real roots of the
following equations:
exp(x)−3x^2=0, (1)
x^3=x^2+x+1, (2)
exp(x) =1/(0.1 +x^2), (3)
and
x= 1 + 0.3 cos(x). (4)
Use an error tolerance tol=10^(−12). Display the obtained approximations to the roots of these equations, and compare the number of iterations, starting with the same initial values x0 for bisection and Newton methods, and with x0 and x1, where x1 was computed by bisection starting with x0, for secant method.
%%Matlab code for finding root using Newton, Secant and
Bisection method
clear all
close all
%Function for which root have to find
fun=@(x) exp(x)-3*x.^2;
%fun=@(x) x-1 -0.3.*cos(x);
%displaying the function
fprintf('For the function\n')
disp(fun)
%Root using Newton method
x0=4; %Initial guess
maxit=1000; %maximum iteration
[root]=newton_method(fun,x0,maxit);
fprintf('Root using Newton method for initial guess %f is
%2.15f.\n',x0,root);
%Root using Secant method
x0=1; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=secant_method(fun,x0,x1,maxit);
fprintf('Root using Secant method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%Root using Bisection method
x0=1; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=bisection_method(fun,x0,x1,maxit);
fprintf('Root using Bisection method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fun=@(x) x.^3-x.^2-x-1;
%displaying the function
fprintf('\nFor the function\n')
disp(fun)
%Root using Newton method
x0=4; %Initial guess
maxit=1000; %maximum iteration
[root]=newton_method(fun,x0,maxit);
fprintf('Root using Newton method for initial guess %f is
%2.15f.\n',x0,root);
%Root using Secant method
x0=1; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=secant_method(fun,x0,x1,maxit);
fprintf('Root using Secant method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%Root using Bisection method
x0=1; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=bisection_method(fun,x0,x1,maxit);
fprintf('Root using Bisection method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fun=@(x) exp(x)-1./(0.1 +x.^2);
%displaying the function
fprintf('\nFor the function\n')
disp(fun)
%Root using Newton method
x0=4; %Initial guess
maxit=1000; %maximum iteration
[root]=newton_method(fun,x0,maxit);
fprintf('Root using Newton method for initial guess %f is
%2.15f.\n',x0,root);
%Root using Secant method
x0=0; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=secant_method(fun,x0,x1,maxit);
fprintf('Root using Secant method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%Root using Bisection method
x0=0; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=bisection_method(fun,x0,x1,maxit);
fprintf('Root using Bisection method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fun=@(x) x-1 -0.3.*cos(x);
%displaying the function
fprintf('For the function\n')
disp(fun)
%Root using Newton method
x0=4; %Initial guess
maxit=1000; %maximum iteration
[root]=newton_method(fun,x0,maxit);
fprintf('Root using Newton method for initial guess %f is
%2.15f.\n',x0,root);
%Root using Secant method
x0=0; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=secant_method(fun,x0,x1,maxit);
fprintf('Root using Secant method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%Root using Bisection method
x0=0; x1=4; %Initial guess
maxit=1000; %maximum iteration
[root]=bisection_method(fun,x0,x1,maxit);
fprintf('Root using Bisection method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Bisection Method
function [root]=bisection_method(fun,x0,x1,maxit)
if fun(x0)<=0
t=x0;
x0=x1;
x1=t;
end
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>10^-12
count=count+1;
xx(count)=(x0+x1)/2;
mm=double(fun(xx(count)));
if mm>=0
x0=xx(count);
else
x1=xx(count);
end
err(count)=abs(fun(x1));
k=abs(fun(x1));
if count>=maxit
break
end
end
root=xx(end);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Newton Method
function [root]=newton_method(fun,x0,maxit)
syms x
g1(x) =diff(fun,x); %1st Derivative of this
function
xx=x0;
%initial guess]
%Loop for all intial guesses
n=10^-12; %error limit for close
itteration
for i=1:maxit
x2=double(xx-(fun(xx)./g1(xx))); %Newton Raphson Formula
cc=abs(fun(x2));
%Error
err(i)=cc;
xx=x2;
if cc<=n
break
end
end
root=xx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Secant Method
function [root]=secant_method(fun,x0,x1,maxit)
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>10^-12
count=count+1;
xx=double(x1-(fun(x1)*abs((x1-x0)/(fun(x1)-fun(x0)))));
x0=x1;
x1=xx;
k=abs(fun(xx));
if count>=maxit
break
end
end
root=xx;
end
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%