I have this code in matlab but it does not work alone. I think I need a script for it.
7 views (last 30 days)
Show older comments
Hamdan Alkhoori
on 6 Oct 2021
Commented: Rena Berman
on 18 Oct 2021
function x = Newton_method(f,df,x0,Tol, MaxIter )
%
% NEWTON Newton's Method
% Newton's method for finding successively better approximations to the
% zeroes of a real-valued function.
%
% Inputs:
% f - solve f(x)=0
% df - derivative of f(x)
% x0 - initial guess
% Tol - stopping tolerance
% MaxIter - maximum number of iterations
%
% Output:% x - a root of f(x)=0
%
nit=0; %number of iterations
disp('step| x | f(x) ')
disp('----|------------|-------------')
while 1
x = x0 - f(x0)/df(x0);
nit=nit+1;
if nit>MaxIter
disp('Maximum number of iterations is reached!')
break
end
if abs(x-x0)<Tol
disp('The sequence is convergent!')
break
end
x0=x;
fprintf('%3i |%12.8f|%12.8f\n',nit,x,f(x))
end
2 Comments
Rik
on 6 Oct 2021
Why did you edit away your question? Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
Accepted Answer
Walter Roberson
on 6 Oct 2021
f = @(x) tan(x) - 5*cos(x)
df = matlabFunction(diff(sym(f)))
x0 = -1
Tol = .0001
MaxIter = 100
X = Newton_method(f, df, x0, Tol, MaxIter)
function x = Newton_method(f,df,x0,Tol, MaxIter )
%
% NEWTON Newton's Method
% Newton's method for finding successively better approximations to the
% zeroes of a real-valued function.
%
% Inputs:
% f - solve f(x)=0
% df - derivative of f(x)
% x0 - initial guess
% Tol - stopping tolerance
% MaxIter - maximum number of iterations
%
% Output:% x - a root of f(x)=0
%
nit=0; %number of iterations
disp('step| x | f(x) ')
disp('----|------------|-------------')
while 1
x = x0 - f(x0)/df(x0);
nit=nit+1;
if nit>MaxIter
disp('Maximum number of iterations is reached!')
break
end
if abs(x-x0)<Tol
disp('The sequence is convergent!')
break
end
x0=x;
fprintf('%3i |%12.8f|%12.8f\n',nit,x,f(x))
end
end
0 Comments
More Answers (1)
the cyclist
on 6 Oct 2021
Works for me. Here is an example
f = @(x) x.^2 - 1;
df = @(x) 2*x;
Newton_method(f,df,3,1.e-6,500)
function x = Newton_method(f,df,x0,Tol, MaxIter )
%
% NEWTON Newton's Method
% Newton's method for finding successively better approximations to the
% zeroes of a real-valued function.
%
% Inputs:
% f - solve f(x)=0
% df - derivative of f(x)
% x0 - initial guess
% Tol - stopping tolerance
% MaxIter - maximum number of iterations
%
% Output:% x - a root of f(x)=0
%
nit=0; %number of iterations
disp('step| x | f(x) ')
disp('----|------------|-------------')
while 1
x = x0 - f(x0)/df(x0);
nit=nit+1;
if nit>MaxIter
disp('Maximum number of iterations is reached!')
break
end
if abs(x-x0)<Tol
disp('The sequence is convergent!')
break
end
x0=x;
fprintf('%3i |%12.8f|%12.8f\n',nit,x,f(x))
end
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!