Problem with Newton-Rapshon code

조회 수: 2 (최근 30일)
Diego Sanchez
Diego Sanchez 2019년 2월 6일
편집: Areej Varamban Kallan 2019년 2월 7일
So I have this simple code:
function y=divsin(fun,xo,tol)
dfun=diff(sym(fun));
f=subs(fun,xo);
df=subs(dfun,xo);
c=1;
while abs(f)>tol
a=xo-f/df;
f=subs(fun,a);
df=subs(dfun,a);
xo=a;
c=c+1;
end
c
a
I need help to find out my mistake. This is what I get when I execute it:
divsin('x^2-2',1,0.0001)
Error using sym>convertChar (line 1459)
Character vectors and strings in the first argument can only specify a variable or
number. To evaluate character vectors and strings representing symbolic expressions, use
'str2sym'.
Error in sym>tomupad (line 1225)
S = convertChar(x);
Error in sym (line 214)
S.s = tomupad(x);
Error in divsin (line 3)
dfun=diff(sym(fun));

답변 (1개)

Areej Varamban Kallan
Areej Varamban Kallan 2019년 2월 7일
편집: Areej Varamban Kallan 2019년 2월 7일
Hi Diego,
In your code, you are passing a character array to 'sym', this is not correct. Add the following line after function definition.
fun = evalin(symengine,fun);
Change the defintion of dfun to:
dfun = diff(fun);

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by