Clear Filters
Clear Filters

How can I make function expression automatically

1 view (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 21 Dec 2020
F = matlabFunction(lhs(Eqn)-rhs(Eqn), 'vars', {[T2, T3, T4, T5, T6] })
Walter Roberson
Walter Roberson on 23 Dec 2020
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.
Johnny Yoon
Johnny Yoon on 24 Dec 2020
Thank you very much for your wonderuful explantion and taking your time to do. I understand better now.

Sign in to comment.

More Answers (1)

David Hill
David Hill on 21 Dec 2020
Did you look at the subs() function?
  1 Comment
Johnny Yoon
Johnny Yoon on 22 Dec 2020
Thank you for your suggestion. Matlab consider T(1) as an element in T. So, it gives an error message "Unrecognized function or variable 'T'.".

Sign in to comment.



Community Treasure Hunt

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

Start Hunting!