MATLAB: Solve symbolic system of equations when specifying solutions

equationsymbolicSymbolic Math Toolbox

Probably not the best question title, but didn't come to think of a better description.
Is it possible to let Matlab solve a system of equations for the case where I want to use a specific definition on some of the solutions?
E.g. for a 2-by-2 system Ax = b I can, by hand, derive b1/x1 = a11+(a12*a21)/((Z2)-a22) with Z2=b2/x2 and b2/x2 = a22+(a12*a21)/((Z1)-a11) with Z1=b1/x1 (ignore that I'm going in a circle, I just need to find expressions for bm/xm when specifying the ratio bn/xn for all n different from m)
Ideally I'd like to use Matlab to do this for a 3-by-3 system as the algebra becomes a bit hairy. As I don't have so much experience with the symbolic tool box I thought I'd try with a 2-by-2 system to begin with as it should be easier, but so far I haven't managed to get the correct results.
Any inputs are greatly appreciated.

Best Answer

  • >> A = sym('a',[3 3]);
    >> b = sym('b',[3 1]);
    >> x = A\b
    x =
    (a1_2*a2_3*b3 - a1_3*a2_2*b3 - a1_2*a3_3*b2 + a1_3*a3_2*b2 + a2_2*a3_3*b1 - a2_3*a3_2*b1)/(a1_1*a2_2*a3_3 - a1_1*a2_3*a3_2 - a1_2*a2_1*a3_3 + a1_2*a2_3*a3_1 + a1_3*a2_1*a3_2 - a1_3*a2_2*a3_1)
    -(a1_1*a2_3*b3 - a1_3*a2_1*b3 - a1_1*a3_3*b2 + a1_3*a3_1*b2 + a2_1*a3_3*b1 - a2_3*a3_1*b1)/(a1_1*a2_2*a3_3 - a1_1*a2_3*a3_2 - a1_2*a2_1*a3_3 + a1_2*a2_3*a3_1 + a1_3*a2_1*a3_2 - a1_3*a2_2*a3_1)
    (a1_1*a2_2*b3 - a1_2*a2_1*b3 - a1_1*a3_2*b2 + a1_2*a3_1*b2 + a2_1*a3_2*b1 - a2_2*a3_1*b1)/(a1_1*a2_2*a3_3 - a1_1*a2_3*a3_2 - a1_2*a2_1*a3_3 + a1_2*a2_3*a3_1 + a1_3*a2_1*a3_2 - a1_3*a2_2*a3_1)
    Now, I'm not sure which substitutions would really help here; the ones you used in the 2*2 case you can get this way:
    >> Z = sym('Z',[3 1]);
    >> xx = sym('x',[3 1]);
    >> simplify(subs(x(1)/b(1), b, Z.*xx))
    ans =
    (Z3*a1_2*a2_3*x3 - Z3*a1_3*a2_2*x3 - Z2*a1_2*a3_3*x2 + Z2*a1_3*a3_2*x2 + Z1*a2_2*a3_3*x1 - Z1*a2_3*a3_2*x1)/(Z1*x1*(a1_1*a2_2*a3_3 - a1_1*a2_3*a3_2 - a1_2*a2_1*a3_3 + a1_2*a2_3*a3_1 + a1_3*a2_1*a3_2 - a1_3*a2_2*a3_1))