In: Advanced Math
Computing3√25 using MATLAB.
(a) Beginning with the interval [2,3], and f(x) =x^3−25, use the error bound for bisection to predict how many iterates bisection would need to get error within 10^−20.
(b) Run bisection on this problem. How many iterations did it need? For some of the iterates compute the absolute error. What is happening approximately to the number of significant digits of accuracy with each iteration?
(c) Write a program to perform Newton’s method on f(x) =x^3−25 with p0= 3 to get an approximation to 3√25 that is accurate to within 10^−20. Calculate the derivative by hand and hard code it in as a new function. How many iterates did it need? Save all of the iterates.
(d) Compute the absolute error and the number of significant digits of accuracy for each iterate. What is happening to the number of significant digits with each iteration? Compare to bisection
%Matlab code for root finding
clear all
close all
%function for which root have to do
func=@(x) x^3-25;
a=2; b=3;
tol=10^-20;
fprintf('For the function \n')
disp(func)
[root,iter]=bisection_method(func,a,b,tol);
fprintf('Root using Bisection method for a=%2.2f and b=%2.2f is
%f\n',a,b,root)
fprintf('Number of iterations used for 10^-20 tol is
%d\n',iter)
[root,iter]=newton_method(func,3,tol);
fprintf('\nRoot using Newton method for x0=%2.2f is
%f\n',b,root)
fprintf('Number of iterations used for 10^-20 tol is
%d\n',iter)
%Matlab function for Newton Method
function [root,iter]=newton_method(fun,x0,tol)
syms x
g1(x) =diff(fun,x); %1st Derivative of this
function
xx=x0;
%initial guess]
maxit=100000;
fprintf('\nRoot using Newton method\n')
%Loop for all intial guesses
n=tol; %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
fprintf('\tAfter %d
iteration root using Newton method is %f\n',i,xx)
end
%fprintf('\tAfter %d iteration root using Newton
method is %f\n',i,xx)
root=xx;
iter=i;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Bisection Method
function [root,iter]=bisection_method(fun,x0,x1,tol)
if fun(x0)<=0
t=x0;
x0=x1;
x1=t;
end
fprintf('\nRoot using Bisection method\n')
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0; maxit=100000;
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));
if count>=maxit
break
end
%
end
fprintf('\tAfter %d iteration root using Bisection method is
%f\n',count,xx(count))
root=xx(end);
iter=count;
end
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%