MATLAB Answers

How can I make function expression automatically

39 views (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] })
  6 Comments
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?
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));
  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.

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by