I have this code in matlab but it does not work alone. I think I need a script for it.

2 vues (au cours des 30 derniers jours)
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 commentaires
Rik
Rik le 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Oct 2021
f = @(x) tan(x) - 5*cos(x)
f = function_handle with value:
@(x)tan(x)-5*cos(x)
df = matlabFunction(diff(sym(f)))
df = function_handle with value:
@(x)sin(x).*5.0+tan(x).^2+1.0
x0 = -1
x0 = -1
Tol = .0001
Tol = 1.0000e-04
MaxIter = 100
MaxIter = 100
X = Newton_method(f, df, x0, Tol, MaxIter)
step| x | f(x) ----|------------|------------- 1 | -6.44732999| -5.09842737 2 | 17.78565182| -4.22818795 3 |-15.21185170| 4.93846493 4 |-10.66898221| -1.34819199 5 |-10.57569237| -0.20172506 6 |-10.55663155| -0.00505942 7 |-10.55612877| -0.00000325 The sequence is convergent!
X = -10.5561
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

Plus de réponses (1)

the cyclist
the cyclist le 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)
step| x | f(x) ----|------------|------------- 1 | 1.66666667| 1.77777778 2 | 1.13333333| 0.28444444 3 | 1.00784314| 0.01574779 4 | 1.00003052| 0.00006104 5 | 1.00000000| 0.00000000 The sequence is convergent!
ans = 1
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

Catégories

En savoir plus sur Networks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by