fileID = fopen ('Newton-Raphson Method', 'w');
syms x
f = x.^3-x.^2+2.*x-2.4;
f_prime = diff(f);
error_tolerance = 0.001;
x_nod = 0.6;
fprintf(fileID, 'Newton Raphson Method \n')
fprintf(fileID, '%-6s%-14.3s%-8.4s\n','n','I','Error')
for n = 1:50
f_nod = vpa(subs(f,x,x_nod));
f_nod_der = vpa(subs(f_prime,x,x_nod));
y = x_nod-f_nod/f_nod_der;
error = abs(y-x_nod);
fprintf(fileID, '%-6d%-14.3f%-8.4f%%\n',n,y,error)
if error < error_tolerance
break
end
x_nod = y;
end
fprintf (fileID, '\nThe root is: %0.3f \n', y);
fprintf (fileID, 'The Error is: %0.4e \n', error);
fprintf (fileID, 'Number of Iterations: %d \n', n);