How to Convert symbolic variables to numeric
Show older comments
Greetings!
How to convert a symbolic expression into a numerical expression?

best regards,
Walid.
3 Comments
Dyuman Joshi
on 25 Jan 2023
Edited: Dyuman Joshi
on 25 Jan 2023
The general way is to use a numeric data type
%random example
y = [(sqrt(sym(5))+1)/2 sym(sin(pi/3)*cos(pi/4)) exp(sym(pi))]
z = double(y)
Guendouz walid
on 25 Jan 2023
Star Strider
on 25 Jan 2023
Answers (1)
If it contains no symbolic variable use double.
two = sym(2);
sqrt2 = sqrt(two)
x = double(sqrt2)
If it does contain a symbolic variable you can approximate the numeric parts with vpa. You could also use vpa even if it doesn't contain a symbolic variable.
z = vpa(sqrt2)
syms y
polynomialInY = y.^2 + sqrt2*y - 1
vpa(polynomialInY)
In this case trying to convert polynomialInY to a double array will error. You could subs a value for y into that expression (to eliminate the symbolic variable) then convert the result.
double(polynomialInY)
5 Comments
Dyuman Joshi
on 25 Jan 2023
@Steven Lord, what would you call sym(sqrt(2)) as? Symbolic constant?
I don't know if there's a standard term for that, I'd probably just call them a number stored as a symbolic expression. But symbolic constant seems like a reasonable description as well.
But I'd be careful with expressions of that type. The sym function by default will "figure out" if you have sqrt(n) for small n that it should represent it as the square root of n. But for larger n it doesn't.
x1 = sym(sqrt(123456783))
But if you swap the order we're taking the square root of a symbolic integer value.
x2 = sqrt(sym(123456783))
Dyuman Joshi
on 25 Jan 2023
Thanks for the info, Steven!
I found more useful info regarding this from documentation of sym -
"Use sym on subexpressions instead of the entire expression for better accuracy. Using sym on entire expressions is inaccurate because MATLAB® first converts the expression to a floating-point number, which loses accuracy. sym cannot always recover this lost accuracy."
Meaning, x2 should be preferred/used over x1.
It is safest to convert every number to sym() inside a symbolic expression.
In the case of a symbolic expression raised to an integer power, MATLAB can be counted on to work out itself that the power needs to be converted to symbolic... Likewise, symbolic expression divided by an integer is reliably converted. But in both cases, only if the expression is already symbolic.
sym((sym(138)/sym(490))^sym(10)) %safest
sym((sym(138)/sym(490))^10) %pretty safe
sym((sym(138)/490)^10) %pretty safe
sym((138/490)^10) %unsafe !
You have to be careful in converting scientific notation
sym(6.02139280e-23) %unsafe !
sym(6.02139280)*10^-23 %less unsafe but still inaccurate
sym(602139280)*sym(10)^-31 %safe
sym(602139280)*10^-23 %looks safe but is not really
The 10^-23 in the last example is not internally being handled by 10^-23 being exactly represented as would be the case for sym(10)^-23 . Instead, the 10^-23 is being handled by sym() examining the number being passed in and doing a continued fraction analysis to determine whether it is "sufficiently close to" a "nice number".
The description for the 'r' value for the flag input argument to the sym function lists the forms that it recognizes for "modest-sized integers p and q". This value for the flag input is the default which is why:
x = sym(sqrt(2))
works to give
. 2 counts as "modest-sized".
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!