In: Advanced Math
How can I code this problem in MATLAB:
a) Find the approximations to within 10-4 to all real zeros of the following polynomials using Newton's method.?
f(x)=x3 - 2*x2- 5.
b) Find approximations to within 10-5 to all the
zeros of each of the following polynomials by first
finding the real zeros using Newton’s method and then reducing to
polynomials of lower degree to
determine any complex zeros.
f(x)=x4 + 5x3 - 9*x2 - 85*x - 136.
%Matlab code for finding roots using Newton methods
clear all
close all
%function for which root have to find
fun=@(x) x.^3 - 2.*x.^2- 5;
fprintf('For the function f(x)=\n')
disp(fun)
xx=linspace(-2,4);
%initial guess
x0=10;
%error limit
n=10^-4;
[root]=newton_method(fun,x0,n);
fprintf('\tRoot of the function using Newton method is
%f.\n',root)
%function for which root have to find
fun=@(x) x.^4 + 5.*x.^3- 9.*x.^2 -85.*x -136;
fprintf('\nFor the function f(x)=\n')
disp(fun)
xx=linspace(-2,4);
%initial guess
x0=10;
%error limit
n=10^-4;
[root1]=newton_method(fun,x0,n);
fprintf('\t1st Root of the function using Newton method is
%f.\n',root1)
%initial guess
x0=-10;
%error limit
n=10^-4;
[root2]=newton_method(fun,x0,n);
fprintf('\t2nd Root of the function using Newton method is
%f.\n',root2)
fprintf('\tNow complex root occured for the equation is ')
syms x
p = x.^4 + 5.*x.^3- 9.*x.^2 -85.*x -136;
p2 = (x-root1)*(x-root2);
[q, r] = quorem(p, p2);
disp(vpa(q,2))
fprintf('So that the complex roots are \n')
fprintf('\t-2.5 +1.32287565553229i \n\t-2.5
-1.32287565553229i\n\n')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Newton Method
function [root]=newton_method(fun,x0,n)
syms x
g1(x) =diff(fun,x); %1st Derivative of this
function
xx=x0;
%initial guess]
%Loop for all intial guesses
maxit=10000;
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%