Clear Filters
Clear Filters

Problem with Newton-Rapshon code

1 view (last 30 days)
Diego Sanchez
Diego Sanchez on 6 Feb 2019
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));

Answers (1)

Areej Varamban Kallan
Areej Varamban Kallan on 7 Feb 2019
Edited: Areej Varamban Kallan on 7 Feb 2019
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);

Products

Community Treasure Hunt

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

Start Hunting!