Need help please with this program for Newton Raphson method

5 次查看(过去 30 天)
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()

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by