In: Advanced Math
Write a program to compute the root of the function f(x) = x3 + 2 x2 + 10 x - 20 by Newton method ( x0 =2 ). Stop computation when the successive values differ by not more than 0.5 * 10-5 . Evaluate f(x) and f '(x) using nested multiplication. The output should contain:
(1) A table showing at each step the value of the root , the value of the function,and the error based upon successive approximation values and percentage error for each method.
(2) A plot showing the variation of percentage error with iteration number for each method on the same graph so as to compare the method.
We have solved the given problem using MATLAB code. (Code attached).
MATLAB code:
function newton()
fprintf('\n');
prompt1='Enter the Initial guess x0: ';
prompt2='Enter the accuracy percentage: ';
x0= input(prompt1);
tol= input(prompt2);
f = @(x) x.^3+2*x.^2+10*x-20;
dfdx= @(x) 3*x.^2+4*x+10;
error=1e10;
x_old=x0;
A(1,:)=[0 x_old f(x_old) 0 0];
fprintf('\n\n\nIteration Root Function Value Absolute error
Percentage error\n');
fprintf('--------- ----------- ---------------- ----------------
-----------------\n');
fprintf('%3d %12.8f %12.8f\n',A(1,1),A(1,2),A(1,3));
n=1;
while error>tol
n=n+1;
x_new=x_old-f(x_old)/dfdx(x_old);
error=abs(x_new-x_old);
A(n,:)=[n-1 x_new f(x_new) abs(x_new-x_old)
abs(x_new-x_old)/x_old];
fprintf('%3d %12.8f %12.8f %12.8f
%12.8f\n',A(n,1),A(n,2),A(n,3),A(n,4),A(n,5));
x_old=x_new;
end
fprintf('\n\n');
figure(1)
plot(A(2:end,1),A(2:end,5))
xlabel('Iterations')
ylabel('Percentage error')
title('Iterations vs Percentage error graph')
end