Need help please with this program for Newton Raphson method

5 ビュー (過去 30 日間)
bingo
bingo 2013 年 11 月 14 日
コメント済み: bingo 2013 年 11 月 15 日
Hi, i've got this program. For some equations it works well, for others not. It gives me this:
??? Error using ==> inline.subsref at 17
Too many inputs to inline function.
and
Error in ==> Untitled8b at 18
fpxo=jf(xo(1),xo(2));
I know maybe it's for the use of inline, pheraps i should have used anonymous function but i couldn't do it.
this is the code(with an example working equations):
clear all
xo=[5;5] ;
syms x1 x2
fname=[(10*x1^2+2*x1-14*x1*x2-3*x2-2+x2)*0+x1;
3*(x1^3)*x2+5*x1^2-x2^3+7*x2-220];
fprima=jacobian(fname);
epsilon=1.e-10;
maxiter = 30;
iter = 1;
f=inline(fname,'x1','x2');
jf=inline(fprima);
error=norm(f(xo(1),xo(2)),2);
fprintf('error=%12.8f\n', error);
while error >= epsilon
fxo=f(xo(1),xo(2));
fpxo=jf(xo(1),xo(2));
x1=xo-inv(fpxo)*fxo;
fx1=f(x1(1),x1(2));
error =norm((fx1),2);%abs(x1-xo);
fprintf(' Iter %2d raiz x=(%14.9f,%14.9f) f(x)=(%14.9f,%14.9f)\n',....
iter,x1(1),x1(2),fx1(1),fx1(2));
if iter > maxiter
fprintf(' Numero di iterazioni massime superato \n');
return;
end
xo=x1;
iter=iter+1;
end
if for example i put an equation like : 0.1+0.45*sin(x1) or x1+x2 doesn't work. Why?
  2 件のコメント
Walter Roberson
Walter Roberson 2013 年 11 月 14 日
Are you sure that in your
(10*x1^2+2*x1-14*x1*x2-3*x2-2+x2)*0+x1;
that you want to be multiplying the long initial expression by 0 ?
bingo
bingo 2013 年 11 月 15 日
sorry just ignore that, i was only trying some equations. it isn't that the problem.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 11 月 14 日
inline() is going to be going away in a future release. Recode using anonymous functions.
You might want to look at matlabFunction()
  1 件のコメント
bingo
bingo 2013 年 11 月 15 日
can you make an example for this code? sorry but i'am new to matlab.

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

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by