Newont's Method: How to display error alongside guesses for the root?

1 view (last 30 days)
It's probably fairly simple, but could anyone show me what code I'd need to plug into my function so as to display all the iterations of x AND the errors that go along with each of them? I'm still new to programming and especially in MATLAB. Any assistance would be much appreciated!
function x = Newton(f, fp, x, nmax, e)
% f is an inline function which we apply Newton's method on
% fp is an inline function that is the derivative of function f
% x is the initial guess of the root
% nmax is the total number of iterations done
% e is the error used to control convergence
fprintf('x(0) = %10g \n', x)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < e
fprintf('Converged! \n')
return
end
end
  1 Comment
Zachary Grant
Zachary Grant on 7 Oct 2015
>> f=inline('x^3-2*x^2+x-3');
>> fp=inline('3*x^2-4*x+1');
>> e=1.0e-15;
>> nmax=10;
>> x=Newton(f,fp,x,nmax,e)
x(0) = 1.58937e-16
x(1) = 3
x(2) = 2.4375
x(3) = 2.21303
x(4) = 2.17555
x(5) = 2.17456
x(6) = 2.17456
x(7) = 2.17456
x(8) = 2.17456
Converged!
x =
2.1746
Is there anyway to have it displayed in a table type fashion?

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!