In MATLAB 2020a, how can I convert symfun to double?

I have a symfun like this:
>> p
p(t) =
[ 0, -(800*pi^2*exp(10*log((489*pi)/50 + 5*sin((11*pi)/250)) - 10*log(10*pi))*(9*cos((11*pi)/250) + (489*pi*sin((11*pi)/250))/500 - 5*cos((11*pi)/250)^2 - 4))/((489*pi)/50 + 5*sin((11*pi)/250))^2, -(800*pi^2*exp(10*log((239*pi)/25 + 5*sin((11*pi)/125)) - 10*log(10*pi))*(9*cos((11*pi)/125) + (239*pi*sin((11*pi)/125))/250 - 5*cos((11*pi)/125)^2 - 4))/((239*pi)/25 + 5*sin((11*pi)/125))^2, -(800*pi^2*exp(10*log((467*pi)/50 + 5*sin((33*pi)/250)) - 10*log(10*pi))*(9*cos((33*pi)/250) + (467*pi*sin((33*pi)/250))/500 - 5*cos((33*pi)/250)^2 - 4))/((467*pi)/50 + 5*sin((33*pi)/250))^2, -(800*pi^2*exp(10*log((228*pi)/25 + 5*sin((22*pi)/125)) - 10*log(10*pi))*(9*cos((22*pi)/125) + (114*pi*sin((22*pi)/125))/125 - 5*cos((22*pi)/125)^2 - 4))/((228*pi)/25 + 5*sin((22*pi)/125))^2, -(800*pi^2*exp(10*log((89*pi)/10 + 5*sin((11*pi)/50)) - 10*log(10*pi))*(9*cos((11*pi)/50) + (89*pi*sin((11*pi)/50))/100 - 5*cos((11*pi)/50)^2 - 4))/((89*pi)/10 + 5*sin((11*pi)/50))^2, -(800*pi^2*exp(10*log((217*pi)/25 + 5*sin((33*pi)/125)) - 10*log(10*pi))*(9*cos((33*pi)/125) + (217*pi*sin((33*pi)/125))/250 - 5*cos((33*pi)/125)^2 - 4))/((217*pi)/25 + 5*sin((33*pi)/125))^2, ............
but when I try to convert it to doule I get(this used to work fine in MATLAB 2018b)
>> pp=double(p)
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 698)
Xstr = mupadmex('symobj::double', S.s, 0);

 Réponse acceptée

You're not specifying a value for t. If any of the elements of the vector p returns when evaluated are functions of t, there's no way to convert that component to a double without specifying a value for t.
syms t p(t)
p(t) = sin(t);
double(p) % this will throw the error you received
p(pi/2) % this will return a sym that contains no symbolic variable
double(p(pi/2)) % this will return a double

4 commentaires

Thanks! Actrually p(t) doesn't have t in it so it's more like
p(t) = [0 1 2 3];
It works like you said
>> syms t p(t)
p(t) = [0 1 2 3];
>> p
p(t) =
[ 0, 1, 2, 3]
>> p(1)
ans =
[ 0, 1, 2, 3]
>> double(p(1))
ans =
0 1 2 3
but in my case, still
>> double(p(1))
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 698)
Xstr = mupadmex('symobj::double', S.s, 0);
is it because I wrote this for readability at the beginning of .mlx file? (If not I will get a series of digits instead of π when doing some symbolic calculations)
syms pi
Once you define a symbolic variable named pi, it is no longer π but is just an identifier like x or a.
>> syms pi
>> sin(pi)
ans =
sin(pi)
If you want your expressions to be computed in terms of the symbolic π you could define a variable containing that symbolic value and work with that:
>> clear pi % My previous section of code defined pi as symbolic
>> P = sym(pi); % The previous line allows this to be the built-in numeric pi
>> sin(P)
ans =
0
Or you could define your symbolic expressions in terms of some other variable and subs the value of pi for that variable at the end. While this "some other variable" could in fact be your syms pi variable, IMO that could easily lead to confusion about whether you're operating symbolically or numerically. Using syms PI for your symbolic variable is one option as long as you remember that PI is the symbol while pi is the number.
>> syms q
>> f = sin(q)
f =
sin(q)
>> subs(f, q, pi)
ans =
0
% Using PI
>> syms PI
>> f = sin(PI)
f =
sin(PI)
>> subs(f, PI, pi)
ans =
0
a a
a a le 6 Sep 2020
Great answer! Thanks a lot!
One other thing I forgot to mention. If you're working symbolically because sin(pi) is not exactly 0 and you need to avoid the roundoff error:
>> y = sin(pi)
y =
1.22464679914735e-16
you may be able to avoid the need to work symbolically by using the sinpi function.
>> y = sinpi(1) % sin(1*pi)
y =
0

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by