Converting symbolic expression into double.

28 ビュー (過去 30 日間)
mkeehan
mkeehan 2011 年 7 月 19 日
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 日
doc double
double(sym_expr)
and
doc subs

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 7 月 19 日
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 日
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