In: Advanced Math
Write a Matlab function for:
1. Root Finding: Calculate the root of the equation f(x)=x^3 −5x^2 +3x−7
Calculate the accuracy of the solution to 1 × 10−10. Find the number of iterations required to achieve this accuracy. Compute the root of the equation with the bisection method.
Your program should output the following lines:
• Bisection Method: Method converged to root X after Y iterations with a relative error of Z.
%Matlab function for Bisection Method
clear all
close all
%function for which root have to find
fun=@(x) x.^3-5*x.^2+3*x-7;
fprintf('function for which root have to find\n')
disp(fun)
x0=0; x1=10; tol=10^-10;
[root,iter]=bisection(fun,x0,x1,tol);
fprintf('Method converged to root %f after %d iterations with a
relative error of %e.\n',root,iter,tol)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [root,iter]=bisection(fun,x0,x1,tol)
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; maxit=1000;
while k>tol
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));
%k=abs(x0-x1);
if count>=maxit
break
end
end
root=xx(end);
iter=count;
end
%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%