How can I make function expression automatically
1 次查看(过去 30 天)
显示 更早的评论
Hello Matlab experts,
I have a 5x1 symbolic named "Eq" with following expressions in it, with 5 unknown variables, T2, T3, T4, T5, T6.
- 0.5*T2^2 - 0.167*T2*T3 - 32.0*T2 - 0.0833*T3^2 + 1.5*T3 - 2610.0 == 0.0
- 0.0833*T2^2 - 0.167*T2*T3 + 0.5*T2 - 0.5*T3^2 - 0.167*T3*T4 - 2.0*T3 - 0.0833*T4^2 + 1.5*T4 == 0.0
- 0.0833*T3^2 - 0.167*T3*T4 + 0.5*T3 - 0.5*T4^2 - 0.167*T4*T5 - 2.0*T4 - 0.0833*T5^2 + 1.5*T5 == 0.0
- 0.0833*T4^2 - 0.167*T4*T5 + 0.5*T4 - 0.5*T5^2 - 0.167*T5*T6 - 2.0*T5 - 0.0833*T6^2 + 1.5*T6 == 0.0
- 0.0833*T5^2 - 0.167*T5*T6 + 0.5*T5 - 0.25*T6^2 - 0.5*T6 - 5.0 == 0.0
To use fsolve, I need to change them into the following expressions, with changing T2 -> T(1), T3 -> T(2), T4 -> T(3), T5 -> T(4), T6 -> T(5).
F(1) = - 0.5*T(1)^2 - 0.167*T(1)*T(2) - 32.0*T(1) - 0.0833*T(2)^2 + 1.5*T(2) - 2610.0;
F(2) = - 0.0833*T(1)^2 - 0.167*T(1)*T(2) + 0.5*T(1) - 0.5*T(2)^2 - 0.167*T(2)*T(3) - 2.0*T(2) - 0.0833*T(3)^2 + 1.5*T(3);
F(3) = - 0.0833*T(2)^2 - 0.167*T(2)*T(3) + 0.5*T(2) - 0.5*T(3)^2 - 0.167*T(3)*T(4) - 2.0*T(3) - 0.0833*T(4)^2 + 1.5*T(4);
F(4) = - 0.0833*T(3)^2 - 0.167*T(3)*T(4) + 0.5*T(3) - 0.5*T(4)^2 - 0.167*T(4)*T(5) - 2.0*T(4) - 0.0833*T(5)^2 + 1.5*T(5);
F(5) = - 0.0833*T(4)^2 - 0.167*T(4)*T(5) + 0.5*T(4) - 0.25*T(5)^2 - 0.5*T(5) - 5.0;
Is there any way to change without changing one by one manually?
0 个评论
采纳的回答
Walter Roberson
2020-12-21
F = matlabFunction(lhs(Eqn)-rhs(Eqn), 'vars', {[T2, T3, T4, T5, T6] })
6 个评论
Walter Roberson
2020-12-23
When you have == in there, and you attempt to evaluate the expression with numeric coefficients, then the result of the == will either be 1 (bit-for-bit equality of the two terms) or 0 (one or more bits is different between the two terms.) fsolve() is looking for a zero return from the expressions, and the very first time it evaluated and found that the two sides were not equal (because the proposed values were wrong) it would return 0, which fsolve would consider to mean success.
What you need instead is hints to fsolve as to which direction to move the trial points. fsolve() takes numeric derivative estimates based upon the trial location and the results it gets back from the function, and that gives it information about which direction to project the trial point towards, using a Newton-Raphson type approach. You want the function to give a quite negative or quite positive result where the trial point is too far on one side or the other of the actual balancing. When you have A==B then A-B == B-B -> A-B == 0 is true, and conversely the result is positive if you have too much A value and not enough B value and negative if you have too much B value and not enough A value, which is information that fsolve() can use for its mathematical projections.
The symbolic approach I used was just standard stepwise refinement. Solve one equation for one variable, substituted the solved variable into the remaining equations, solve one of the mutated equations for one variable, substitute the solved variable into the mutated equations, and so on, iterating until you get to the point where you have either solved everything analytically, or have demonstrated that there is a problem, or have reduced down to equations that you can solve numerically.
MATLAB should be able to solve the entire set of equations without stepwise refinement, but to be honest, the symbolic solver is weak on working with polynomials of higher degree and sometimes you have to focus its attention.
更多回答(1 个)
David Hill
2020-12-21
Did you look at the subs() function?
subs(Eq,T2,T(1));
subs(Eq,T3,T(2));
subs(Eq,T4,T(3));
subs(Eq,T5,T(4));
subs(Eq,T6,T(5));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!