In: Computer Science
Write a program using Newton's method:
Use your programs to find approximations to within 10^(-4) to all zeros of the following cubic polynomials.
Use |P_(n+1)-P_n| as a measure of the error in the
iteration. Save all of the iterations. What are your
conclusions?
(a) f(x) =
x^3-5x^2 +
2x
(b) f(x) = x^3-2x^2-5
The program has to be used with MATLAB. I'm still learning how to use the program. I would love some help and tips on solving these methods. Thank you
f1=@(x) x^3-5*x^2+2*x; %Function for part a
f1d=@(x) 3*x^2-10*x+2; %Derivative of function
f2=@(x) x^3-2*x^2-5; %Function for part b
f2d=@(x) 3*x^2-4*x; %Derivative of function
roota=NewtonRoot(f1,f1d,1,20) %Function call for root to part (a)
initial guess=1,iterations=20
rootb=NewtonRoot(f2,f2d,2,20)%Function call for root to part (b)
initial guess=2,iterations=20
function Xs=NewtonRoot(Fun,Funder,Xo,imax) %Function implementing
Newton's method
Err=0.0001; %Error
for(i=1:imax) %Running a loop till maximum iterations
Xi=Xo-Fun(Xo)/Funder(Xo); %NR method iterative scheme
if(abs((Xi-Xo))<Err) %If error is permissible loop breaks
Xs=Xi;
break
end
Xo=Xi; %Otherwise updates value
end
if(i==imax)
fprintf('Solution not obtained in %i iterations',imax)
Xs=('No Answer');
end
end