Search code examples
variablesmatlabequationsymbolic-math

Matlab: find specific variable from symbolic expression and/or system


I have a system of three symbolic equations:

(1) Ua  = (Un*Ga -ia -(U2 -Ug2)*G3)/Ga;
(2) U2  = (Ug2*G3 -(Uc2m - Ua)*Ca*Fs)/(Ca*Fs + G3);
(3) Ug2 = ((Ua -Uc2m)*Ca*Fs* G3 -ig2*(Ca*Fs + G3)) / ((G3 + Gg2)*(Ca*Fs + G3) - G3*G3);

After substitutions of (3) inside (2) and (1), and then (2) inside (1), I have (1) as follows:

Ua  = (Un*Ga -ia -((((Ua -Uc2m)*Ca*Fs* G3 -ig2*(Ca*Fs + G3)) / ((G3 + Gg2)*(Ca*Fs + G3) - G3*G3)*G3 -(Uc2m - Ua)*Ca*Fs)/(Ca*Fs + G3) -((Ua -Uc2m)*Ca*Fs* G3 -ig2*(Ca*Fs + G3)) / ((G3 + Gg2)*(Ca*Fs + G3) - G3*G3))*G3)/Ga;

With Ua appearing both on the left and on the right member. Is there a way (with Matlab or any other tool) to:

  1. Simplify the expression having only Ua on the left
  2. Starting from the original system of three symbolic equations, simplify all of them automatically specifying what symbols consitute the three variables (that will appear on the left), and consider the rest as parameters to leave on the right.

Solution

  • I'm not sure all of @RodyOldenhuis's hard work is required in this case. It's simply a matter of using solve correctly. When you provide N equations, it's usually best to ask for it to solve for N variables either of your choosing of its own (by not specifying any variable names):

    syms Ua Ug2 U2 ia Un Ga G3 Uc2m Ca Fs ig2 Gg2
    eqs = [Ua == (Un*Ga -ia -(U2 -Ug2)*G3)/Ga
           U2 == (Ug2*G3 -(Uc2m -Ua)*Ca*Fs)/(Ca*Fs + G3)
           Ug2 == ((Ua -Uc2m)*Ca*Fs*G3 -ig2*(Ca*Fs + G3)) / ((G3 + Gg2)*(Ca*Fs + G3) -G3*G3)];
    
    s = solve(eqs,Ua,U2,Ug2)
    

    Then s.Ua returns

    -(G3*Gg2*ia - G3*Ga*Gg2*Un + Ca*Fs*G3*ia + Ca*Fs*G3*ig2 + Ca*Fs*Gg2*ia - Ca*Fs*G3*Gg2*Uc2m - Ca*Fs*G3*Ga*Un - Ca*Fs*Ga*Gg2*Un)/(G3*Ga*Gg2 + Ca*Fs*G3*Ga + Ca*Fs*G3*Gg2 + Ca*Fs*Ga*Gg2)
    

    Don't make the mistake of passing in Ua, U2, and Ug2 into solve as a vector (unless you're using R2015a+, I believe).

    This works using the old string-based technique as well (the cell array could be replace by individual strings inputs if desired):

    eqs = {'Ua = (Un*Ga -ia -(U2 -Ug2)*G3)/Ga'
           'U2 = (Ug2*G3 -(Uc2m -Ua)*Ca*Fs)/(Ca*Fs + G3)'
           'Ug2 = ((Ua -Uc2m)*Ca*Fs*G3 -ig2*(Ca*Fs + G3)) / ((G3 + Gg2)*(Ca*Fs + G3) -G3*G3)'};
    
    s = solve(eqs{:},'Ua','U2','Ug2')
    

    This works in R2013a and R2015a. I don't know if older versions can handle this or not.