Newton's method function output.
Show older comments
Hello,
I have a little problem with Newton's method. I have the code and it is working but I would like to get the results such that I have the teration number n and the value of x_n according to it. Also I need the error and f(x_n) but I am only getting the x_n values on their own. The code is as follows
function [x,n,err] = newton( f, dx, x0, tol, ITMAX )
format long
f= inline(f);
dx = inline(dx);
x(1) = x0 - (f(x0)/dx(x0));
err(1) = abs(x(1)-x0);
fx=feval(f,x);
for n=2:ITMAX
if (err(n-1) >= tol) & (n <= ITMAX)
x(n) = x(n-1) - (f(x(n-1))/dx(x(n-1)));
err(n) = abs(x(n)-x(n-1));
n = n+1;
else
break
end
end
end
It would be nice if I could get the result in a table like this
disp('_________________________________________________________________________________ ')
disp(' n x_n f(x_n) error ')
disp('_________________________________________________________________________________ ')
fprintf('\n')
However I am not quite sure how to use fprint command. Any help would be appreciated. Thank you.
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!