I get the following error in Newton Ralphson code?Can any one predict the mistake?If i use RETURN instead of break at last(21 line),then error "Not enough input arguments" appears as shown in image.

1 回表示 (過去 30 日間)
f
function [x,iter]=newton(x0,f,fp)
% newton-raphson algorithm
N = 100; eps = 1.e-5; % define max. no. iterations and error
maxval = 10000.0; % define value for divergence
xx = x0;
while (N>0)
xn = xx-f(xx)/fp(xx);
if abs(f(xn))<eps
x=xn;iter=100-N;
return;
end;
if abs(f(xx))>maxval
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
break;
end;
N = N - 1;
xx = xn;
end;
error('No convergence');
break;
% end function

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 5 月 17 日
編集済み: Walter Roberson 2016 年 5 月 17 日
You cannot just run that code by giving its name newton . You need to supply the three parameters. For example,
newton(12.345, @(x) sin(x) + 3/4, @(x) cos(x))
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 5 月 18 日
You would not rewrite the code: you would give a command at the command line like I show.
You could write a second .m file that contained those commands, if you wanted.
Machine Learning Enthusiast
Machine Learning Enthusiast 2016 年 5 月 19 日
i want to ininiate the iterative procedure by following equation, where i represents the iteration number: xi+1 = xi - f(xi)/f'(xi). After each iteration the program should check to see if the convergence condition, namely, f(x i+1)<ε,

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by