Matrix form of system of equations with symbolic time dependant values

I have a system of equations with symbolic time dependant values:
t = sym('t');
q1(t)= sym('q1(t)');
q2(t) = sym('q2(t)');
x = diff(q1(t)*q2(t), t)
y = diff(q1(t)+q2(t)^2,t)
with the following output:
x =
q1(t)*diff(q2(t), t) + q2(t)*diff(q1(t), t)
y =
2*q2(t)*diff(q2(t), t) + diff(q1(t), t)
Is it possible to obtain the matrix form of these equations without doing it manually? something similar to this:
[q2(t), q1(t); 1, 2*q2(t)]*[diff(q1(t), t); diff(q2(t), t)]
Unfortunately, equationsToMatrix() doesn't seem to work in this case.
This example is relatively easy, but the equations I am working on are more complex and I would need some sort of automation. Any advise on working with symbolic math will be really appreciated.

 Accepted Answer

You might have to work at the MuPAD level to do this efficiently.
Basically you would need to identify the diff() expressions and then substitute variables for them. Once you have them in the form of variables, you can ask for coefficients associated with the variable and use those to populate the matrix of coefficients you are building up.
If I were doing it in Maple I would probably use indets(x,specfunc(diff)) to identify the unique diff() expressions, but the indets() of the symbolic engine (MuPAD) is considerably less flexible in its type matching.

1 Comment

Thank you very much Walter, it worked.
I used the Example 4 of subsex() documentation, as you suggested.
In order to copy the variables and expressions back and forth between MuPAD and Matlab I used the setVar() and getVar() functions.
I used equationsToMatrix() in order to get the matrix.
Now that I know about MuPAD, I am considering pushing more symbolic computations into it before jumping into numerical computations in Matlab.
Thanks a lot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!