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

14 次查看（过去 30 天）
Hamdan Alkhoori2021-10-6

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 个评论显示隐藏 1更早的评论
Rena Berman 2021-10-18

### 采纳的回答

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 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

### Community Treasure Hunt

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

Start Hunting!