In: Advanced Math
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max.
3. Root finding; Bisection, Secant, Newton. a) Write your own version of the bisection method to solve e 2x − 10x = 0. Assume the root is in the interval [0, 1]. Try to use the matlab command ”fzero” to accomplish the same thing. b) Write your own version of secant method to solve e −2x − 7x = 0. Assume the root is in the interval [1/9, 2/3]. Try to use the matlab command ”fzero” to accomplish the same thing. c) Write your own version of Newton’s method to solve x + sin(x) − cos(2x) = 0.
%%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(2*x)-10*x;
%displaying the function
fprintf('\tFor the function\n')
disp(fun)
%Finding roots
r1=fzero(fun,0);
fprintf('The root using fzero is %f.\n',r1)
%Root using Bisection method
x0=0; x1=1; %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\n',x0,x1,root);
%Function for which root have to find
fun=@(x) exp(-2*x)-7*x;
%displaying the function
fprintf('\tFor the function\n')
disp(fun)
%Finding roots
r1=fzero(fun,1);
fprintf('The root using fzero is %f.\n',r1)
%Root using Secant method
x0=1/9; x1=2/3; %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\n',x0,x1,root);
%Function for which root have to find
fun=@(x) x+sin(x)-cos(2*x);
%displaying the function
fprintf('\tFor the function\n')
disp(fun)
%Finding roots
r1=fzero(fun,2);
fprintf('The root using fzero is %f.\n',r1)
%Root using Newton method
x0=0; %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\n',x0,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>eps
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=eps; %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>eps
count=count+1;
xx=double(x1-(fun(x1)*((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 %%%%%%%%%%%%%%%%