Converting symbolic expression into double.
Show older comments
When I perform a series of functions, the result is:
1.009168*rS + 0.25*rS*dcellCurrent(1) + 0.157*rS*dcellCurrent(2) + 0.057*rS*dcellCurrent(3) - 0.002*rS*dcellCurrent(4) - 0.093*rS*dcellCurrent(5) - 0.145*rS*dcellCurrent(6) - 0.197*rS*dcellCurrent(7) - 0.232*rS*dcellCurrent(8) - 0.313*rS*dcellCurrent(9) ... *exp((120843*iSat*rSh)/(3125*n)))/(3125*n*exp((120843*rSh*dcellCurrent(13))/(3125*n))))
The expression is extremely long, I cut out a good bit of its innards so it would be viewable here. When I copy and paste the expression that it shows into the command window, the result is this number: 13.076812258420913
I know that I can just copy and paste it, but I would like to use logical expressions in an if statement, and I need to convert the expression in code because I would not like to do it by hand. How should I go about converting?
Accepted Answer
More Answers (1)
Walter Roberson
on 19 Jul 2011
You should use subs() to substitute the current values of the variables in to the expression, and you should use double() on the result to convert it from a symbolic number to a double precision value.
6 Comments
mkeehan
on 19 Jul 2011
Walter Roberson
on 19 Jul 2011
If your symbolic expression was S then
double(subs(S))
It _is_ possible to substitute for individual variables, but as you have a number of variables you will find the above easiest.
mkeehan
on 19 Jul 2011
Sean de Wolski
on 19 Jul 2011
You could always (It pains me to say it) just use eval on the string.
mkeehan
on 19 Jul 2011
Walter Roberson
on 19 Jul 2011
You do not appear to have defined dcellCurrent.
Anyhow:
syms x dcC
S = simplify(solve(iP - iSat * (exp(38.66976*(x+dcC*rS)/n)-1) -(dcC*rS + x)/rSh - dcC));
subtotal = 0;
for K = 1:length(dcC)
subtotal = subtotal + (double(subs(S,dcC,dcellCurrent(K))-dcellVoltage(K)).^2;
end
Categories
Find more on Conversion 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!