Ashish Gupta
Ashish Gupta

Reputation: 185

Why does Matlab not evaluate a symbolic expression when using subs()?

This is the code along with the output.

>> syms x
>> y=-(x-2*sin(x))/(1-2*cos(x))

y =

    (x - 2*sin(x))/(2*cos(x) - 1)

>> x=1.9

x =

    1.9000

>> subs(y)

ans =

  -(2*sin(19/10) - 19/10)/(2*cos(19/10) - 1)

I can't figure out why this absurd answer is coming. I am not able to fix it.

So far I have tried taking y also as a symbolic variable and using int(ans) , with no success. I have also tried storing ans in a non-symbolic variable, but the same output shows up every time.

Upvotes: 2

Views: 1010

Answers (3)

eval(subs(...)) also work. I don't know what is better to use eval either double, my teacher uses double

Upvotes: 0

ViG
ViG

Reputation: 1868

subs will substitute the value in your expression, but it will stay symbolic and will not be computed. In order to get the value numerically you need eval instead of subs.

Upvotes: 0

gnovice
gnovice

Reputation: 125874

You need to use double on the result to evaluate the symbolic expression and get a numeric value:

>> out = double(subs(y))

out =

  -0.004494059516242

Upvotes: 4

Related Questions