Need help please with this program for Newton Raphson method

2 vues (au cours des 30 derniers jours)
bingo
bingo le 14 Nov 2013
Commenté : bingo le 15 Nov 2013
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 commentaires
Walter Roberson
Walter Roberson le 14 Nov 2013
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 le 15 Nov 2013
sorry just ignore that, i was only trying some equations. it isn't that the problem.

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 14 Nov 2013
inline() is going to be going away in a future release. Recode using anonymous functions.
You might want to look at matlabFunction()
  1 commentaire
bingo
bingo le 15 Nov 2013
can you make an example for this code? sorry but i'am new to matlab.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by