How can I convert a symbolic value to a numeric

I'm working with functions and symbolic variables so I need to get the numerical value of one of them.

Answers (1)

If you want to replace a symbolic variable with a numeric value in a symbolic expression, then use subs()
You can always vpa() a symbolic expression to evaluate constant functions and convert most rational values into symbolic floating point values. (Exponents of expressions containing variables are not converted to floating point -- that has a different meaning.)
If your symbolic expression does not contain any undefined symbolic variables, then most of the time you can convert it to standard double precision by using double() on it. That will sometimes fail if the expression contains integrals that are divergent or badly behaved. It will always fail if there are undefined symbolic variables.
If the task is something like plotting, then there are three approaches:
  • use fplot() . This does a lot of work in the background to try to detect non-smooth areas and try to detect discontinuities, so it can end up taking a lot of computation.
  • subs() in numeric values for the variables and double() the result to get numeric values
  • use matlabFunction to create a numeric anonymous function, and apply that function to the x values to get numeric results.

Tags

Asked:

on 13 Nov 2017

Answered:

on 13 Nov 2017

Community Treasure Hunt

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

Start Hunting!