Matlab gives me a formula when i want a value

63 views (last 30 days)
I'm trying to get Matlab to show me a value instead of a formula. I use this code:
clc; clear;
y = 0.000000000258;
T = 1273;
TM = 2163;
TMO = -0.5;
G2 = 126000;
A = 1.2E-29;
b = 0.000000000258;
x = 2/3*b;
Eps = 0.00487903650119246;
ny = 0.3;
Wi = Wifun
Which uses a function in a separate .m file which is stated below:
function Wi = Wifun(y,T,TM,TMO,G2,A,b,x,Eps,ny)
syms G2;syms T;syms TM;syms TMO;syms ny;syms A;syms Eps;syms b;syms x;syms y;
G = G2.*(1+((T-300)./TM).*TMO);
Ei = -(1/2.*pi).*((1+ny)./(1-ny)).*G.*A.*Eps;
Wi = -(2/3).*b.*x.*Ei.*(1./(x.^2+y.^2));
When I run the above code it gives me this:
Wi =
-(pi*A*Eps*G2*b*x*((TMO*(T - 300))/TM + 1)*(ny + 1))/(3*(x^2 + y^2)*(ny - 1))
But I would like Matlab to calculate that expression for me without having to enter it manually. When entering it manually I get the answer 5.1323e-027. Is there a way to do this?

Accepted Answer

Walter Roberson
Walter Roberson on 29 Apr 2011
I would not recommend using eval. Instead,
double(subs(Wi))

More Answers (2)

Jon
Jon on 29 Apr 2011
What's the difference between subs and eval? And why do you use double and what exactly does double do?
  1 Comment
Walter Roberson
Walter Roberson on 29 Apr 2011
subs() works at the symbolic level. In the form being used, it looks inside the symbolic expression for all variable names, and looks in the current workspace for variables of the same name, and substitutes the values of the variables it found in to the symbolic expression. The value of the expression is then calculated symbolically. The double() then converts the resulting symbolic number in to a double precision number suitable for direct numeric use.
When you use subs(), you get the full power and accuracy of symbolic computations. For example, you could use a dirac delta, or hypergeometric function, or symbolic integration, all of which are defined for symbolic expressions but not for numeric expressions.
When you use eval(), the symbolic expression would be converted to a character string (such as is displayed) and then the MATLAB parser and full runtime would be applied to calculate the meaning of the character string, using normal MATLAB double precision (or appropriate MATLAB numeric type). This process involves MATLAB using its JIT analyzer and so on. It is a bit "heavy handed" computationally, is restricted to floating point precision. It would generate an error if any of the variables are not defined instead of producing a symbolic solution. (If you use subs() and any of the variables are not defined, a symbolic solution would be produced which could be further used -- but applying double() to a symbolic expression which has unresolved names would give an error.)
Using eval() also opens things up to security problems and unintended interactions.

Sign in to comment.


Paulo Silva
Paulo Silva on 28 Apr 2011

Community Treasure Hunt

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

Start Hunting!