Need help please with this program for Newton Raphson method
조회 수: 3 (최근 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
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 ?
답변 (1개)
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 Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!