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

조회 수: 2 (최근 30일)
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
Rik
Rik 2021년 10월 6일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 10월 6일
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

추가 답변 (1개)

the cyclist
the cyclist 2021년 10월 6일
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

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by