How do you use the function eval?

4 views (last 30 days)
how would you plug in 2 variables which both contain a 2x1 matrix?
  3 Comments
Nikko Magsino
Nikko Magsino on 22 Nov 2017
yea that's what it says too on the function description but the question requires me to use eval.
here's what I've done so far:
syms x y
ellipse1 = (((x-1+r)^2)/(6^2))+((y^2)/(3^2))==1;
ellipse2 = (((x+2)^2)/(2^2))+(((y-5)^2)/(3^2))==1;
[xc yc] = solve(ellipse1,ellipse2,'MaxDegree',4);
Stephen23
Stephen23 on 22 Nov 2017
Edited: Stephen23 on 2 Sep 2019
@NIkko Magsino: Do NOT use eval for converting symbolic expressions to numeric, or for substituting into symbolic expressions. It is the wrong tool for the job, and whoever wrote that question gave bad advice.
Take a look at the list of Symbolic Toolbox functions:
Note that eval is not listed, but it includes the correct ways to convert to numeric, which is to use the functions double, single, int8, etc. They are clearly listed under the section title "Conversion Between Symbolic and Numeric".
This specific list of symbolic conversion functions is the same:
So whoever wrote that question is not only giving bad advice but has also never bothered to actually read the MATLAB documentation.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 22 Nov 2017
Do not use eval() to turn symbolic expressions into floating point numbers. Use double() if the symbolic expressions contain only constants and functions of constants; use vpa() if the symbolic expressions contain unresolved symbolic variables.
eval() applied to a symbolic variable is not a correct thing to do. Symbolic expressions are in a different programming language that is a hybrid between MATLAB and MuPAD, and if you eval() then MATLAB will attempt to resolve them as-if they are MATLAB code, which they are not. This can be important because some of the symbolic functions take different parameters or take parameters in a different order than the MATLAB function with the same name.
  2 Comments
Nikko Magsino
Nikko Magsino on 22 Nov 2017
but would it be possible use eval() here? my teacher wants me to use eval for whatever reason he has.
Walter Roberson
Walter Roberson on 22 Nov 2017
The question does not require that you use eval(). eval() is given as a hint. The hint is wrong. You can refer your teacher to me if they need further convincing that eval() of a symbolic expression is not a proper thing to do.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!