Converting symbolic expression into double.

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?

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 7 月 19 日

1 投票

doc double
double(sym_expr)
and
doc subs

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 7 月 19 日

1 投票

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 件のコメント

mkeehan
mkeehan 2011 年 7 月 19 日
So if one of my variables was a, would this be the right general idea?
a = 2
y = subs(a)*4;
Thank you so much!
Walter Roberson
Walter Roberson 2011 年 7 月 19 日
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
mkeehan 2011 年 7 月 19 日
Okay, I'm certain that I'm doing something wrong. I miss java, the for loops made more sense. I'm a little embarrassed by my failure to use it here, but I have to post the code because I'm incapable of getting this without it, probably.
iP = 0; % this is a cell in the dark
iSat = 3.917e-007;
rS = .41;
rsRange = .1:.01:1;
rSh = 268.5;
n = 1.699;
subtotal = (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(1)*rS)/n)-1) -(dcellCurrent(1)*rS + x)/rSh - dcellCurrent(1) = 0',x)-dcellVoltage(1))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(2)*rS)/n)-1) -(dcellCurrent(2)*rS + x)/rSh - dcellCurrent(2) = 0',x)-dcellVoltage(2))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(3)*rS)/n)-1) -(dcellCurrent(3)*rS + x)/rSh - dcellCurrent(3) = 0',x)-dcellVoltage(3))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(4)*rS)/n)-1) -(dcellCurrent(4)*rS + x)/rSh - dcellCurrent(4) = 0',x)-dcellVoltage(4))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(5)*rS)/n)-1) -(dcellCurrent(5)*rS + x)/rSh - dcellCurrent(5) = 0',x)-dcellVoltage(5))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(6)*rS)/n)-1) -(dcellCurrent(6)*rS + x)/rSh - dcellCurrent(6) = 0',x)-dcellVoltage(6))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(7)*rS)/n)-1) -(dcellCurrent(7)*rS + x)/rSh - dcellCurrent(7) = 0',x)-dcellVoltage(7))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(8)*rS)/n)-1) -(dcellCurrent(8)*rS + x)/rSh - dcellCurrent(8) = 0',x)-dcellVoltage(8))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(9)*rS)/n)-1) -(dcellCurrent(9)*rS + x)/rSh - dcellCurrent(9) = 0',x)-dcellVoltage(9))^2 ;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(10)*rS)/n)-1) -(dcellCurrent(10)*rS + x)/rSh - dcellCurrent(10) = 0',x)-dcellVoltage(10))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(11)*rS)/n)-1) -(dcellCurrent(11)*rS + x)/rSh - dcellCurrent(11) = 0',x)-dcellVoltage(11))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(12)*rS)/n)-1) -(dcellCurrent(12)*rS + x)/rSh - dcellCurrent(12) = 0',x)-dcellVoltage(12))^2;
subtotal = subtotal + (solve('iP - iSat * (exp(38.66976*(x+dcellCurrent(13)*rS)/n)-1) -(dcellCurrent(13)*rS + x)/rSh - dcellCurrent(13) = 0',x)-dcellVoltage(13))^2;
double(subs(subtotal))
---------------
The error message:
??? Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in ==> sym.sym>sym.double at 936
Xstr = mupadmex('symobj::double', S.s, 0);
Error in ==> transcendentalTesting at 32
double(subs(subtotal))
Sean de Wolski
Sean de Wolski 2011 年 7 月 19 日
You could always (It pains me to say it) just use eval on the string.
mkeehan
mkeehan 2011 年 7 月 19 日
While the pain part of your answer makes me a bit nervous, it does work! Thank you both of you! How do I go about giving reputation?
Walter Roberson
Walter Roberson 2011 年 7 月 19 日
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

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by