필터 지우기
필터 지우기

Converting symbolic expression into double.

조회 수: 9 (최근 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