Search code examples
matlabsymbolic-math

Expressing a symbolic variable in terms of other specific variables


I have a pretty simple symbolic task to do in MATLAB, but I can't figure out how.

Assume for symbolic variables I have:

    a1=t2-t1
    a2=t3-t1
    a3=t4-t1

I also know that

 a4=t3-t2
 a5=t4-t2
 a6=t4-t3

How can I express a4, a5 and a6 in terms of (a1, a2, a3)?

For example: for a4 we should have:

a4=a2-a1

Solution

  • Let's start with

    syms t1 t2 t3 t4 a1 a2 a3 a4 a5 a6
    

    I'll have all six equalities set as equations, e.g., a1 == t2 - t1, a6 == t4 - t3

    From the six equations you have to eliminate the four t variables. Matlab has the function eliminate, that does exactly that:

    wot = eliminate(a1 == t2 - t2, a2 == t3 - t1, a4 == t3 - t1, a5 == t4 - t2, a6 == t4 - t3], [t1, t2, t3, t4])
    

    This function only works for rational equations, that are equivalent to polynomial equations, and the method used is that of Groebner bases. In general, the problem of eliminating variables from a set of equations is very difficult and there are no general algorithms for it, as there are no general algorithms for symbolically solving so many classes of equations.

    If this operation succeeds (and it does in this case), you get three equations (there's an implict == 0 for each expression), you may use to solve for the three variables of interest, a4, a5, a6:

    solve(wot, [a4, a5, a6])
    

    I have to add in the end the naive approach, that is the way one would go about it manually; it may work in some simple cases that are not rational equations, if they are simple enough. It works fine for your example equations, as they are linear and symmetric.

    The first three equations have 7 symbols, out of which we'd like to get rid of the four ts. Since there are only three equations, we can only get rid of three:

    sol1 = solve([a1 == t2 - t1, a2 == t3 - t1, a4 == t4 - t1], [t1, t2, t3])
    

    (I'd normally eliminate t2, t3 and t4, to leave t1, because of the symmetry, but in this case it works regardless of the choice of three ts out of four)

    Then substitute this solution, that still depends on t4, into the remaining three equations:

    subs([a4 == t4 - t1, a5 == t4 - t2, a6 == t4 - t3], sol1)
    

    possibly followed by the usual manipulation of the resulting expressions to get rid of the t4 variable, if indeed it can be eliminated (which is not the case in general). For the example equations this is not necessary.