Using equationsToMatrix( ) with symbolic variables representing derivatives

12 views (last 30 days)
Currently trying to put a system of equations into matrix form. There is a function y1 (function of x(t)) that is differentiated twice, and then a function y2 (function of z(t)) differentiated once. With these derivations, the chain rule is being applied, so the derivatives of the x and z variables are becoming present. For this example problem, after being differentiated, they should be put into a matrix as follows:
[A][x_ddot; z_dot] = [b]
where [A] is a 1x2 matrix of coefficients to x_ddot and z_dot, and [b] is the rest of the equation (function of other variables/derivatives of variables). Here is the current code:
syms t x(t) z(t) % define symbolic variables
y1 = x^3 % expression for y1 where x is a function of time
y1dot = diff(y1,t) % differentiation of y1
y1ddot = diff(y1dot,t) % differentiation of y1_ddot
eqn1 = 2*y1ddot == 4 % equation 1
y2 = z^4 % expression for y2 where z is a function of time
y2dot = diff(y2,t) % differentiation of y2
eqn2 = 3*y2dot == 17 % equation 2
eqns = [eqn1; eqn2] % system of equations to put into matrix form
vars = [sym(diff(x,2)); sym(diff(z,1))] % array of variables to sort each equation by in symbolic form
[A,b] = equationsToMatrix(eqns,vars) % equationsToMatrix expression
The expected output should look like this:
[6*x^2, 12z^3]*[x_ddot; z_dot] = [4-12*x*x_dot^2; 17]
I'm currently getting an error saying "Second argument must be a vector of symbolic variables." Is there a way to properly have the equationsToMatrix command use derivatives (like x_ddot and z_dot) to sort the matrices by? The syntax I used of sym(diff(x,2)) seemed to work for the built in isolate function, but not for thie equationsToMatrix.

Answers (1)

Nipun
Nipun on 15 May 2024
Hi Matthew,
I understand that you are trying to represent the higher order differential equations in a matrix form. Based on the given expected output, it seems like you want to separate the double differentiation of "x" and single differentiation of "z" variable.
I recommend using the "subs" function in MATLAB to substitute the required variables with new symbolic variables and then separating the equation coefficients and constants. For more information on "subs" function and the supported arguments, refer to the following MathWorks documentation: https://in.mathworks.com/help/symbolic/subs.html
In the below code, I substitute "xddot" and "zdot" with "A" and "B" respectively.
syms A B t x(t) z(t) % define symbolic variables
y1 = x^3 % expression for y1 where x is a function of time
y1dot = diff(y1,t) % differentiation of y1
y1ddot = diff(y1dot,t) % differentiation of y1_ddot
eqn1 = 2*y1ddot == 4 % equation 1
y2 = z^4 % expression for y2 where z is a function of time
y2dot = diff(y2,t) % differentiation of y2
eqn2 = 3*y2dot == 17 % equation 2
eqns = [eqn1; eqn2] % system of equations to put into matrix form
vars = [sym(diff(x,2)); sym(diff(z,1))] % array of variables to sort each equation by in symbolic form
new_eqns = subs(eqns, vars, [A ; B])
new_vars = subs(vars, vars, [A ; B])
[A,b] = equationsToMatrix(new_eqns',new_vars') % equationsToMatrix expression
y1(t) =
y1dot(t) =
y1ddot(t) =
eqn1(t) =
y2(t) =
y2dot(t) =
eqn2(t) =
eqns(t) =
vars(t) =
new_eqns(t) =
new_vars(t) =
Hope this helps.
Regards,
Nipun

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!