How to Convert symbolic variables to numeric
53 views (last 30 days)
Show older comments
Greetings!
How to convert a symbolic expression into a numerical expression?

best regards,
Walid.
Answers (1)
Steven Lord
on 25 Jan 2023
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)
See Also
Categories
Find more on Get Started with Symbolic Math Toolbox 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!