Main Content

massMatrixForm

Extract mass matrix and right side of semilinear system of differential algebraic equations

Description

[M,F] = massMatrixForm(eqs,vars) returns the mass matrix M and the right side of equations F of a semilinear system of first-order differential algebraic equations (DAEs). Algebraic equations in eqs that do not contain any derivatives of the variables in vars correspond to empty rows of the mass matrix M.

The mass matrix M and the right side of equations F refer to this form.

M(t,x(t))x˙(t)=F(t,x(t)).

example

Examples

collapse all

Convert a semilinear system of differential algebraic equations to mass matrix form.

Create the following system of differential algebraic equations. Here, the functions x(t) and y(t) represent state variables of the system. The system also contains symbolic parameters alpha and beta, and the function f(t,x,y). Specify the equations and variables as two symbolic vectors: equations as a vector of symbolic equations, and variables as a vector of symbolic function calls.

syms x(t) y(t) f(t,x,y) alpha beta
eqs = [x(t) - diff(x(t),t) == alpha*f(t,x(t),y(t)); ...
       y(t) + diff(y(t),t) == beta*f(t,x(t),y(t))]
eqs = 

(x(t)-t x(t)=αf(t,x(t),y(t))t y(t)+y(t)=βf(t,x(t),y(t)))

vars = [x(t),y(t)];

Find the mass matrix form of this system.

[M,F] = massMatrixForm(eqs,vars)
M = 

(-1001)

F = 

(αf(t,x(t),y(t))-x(t)βf(t,x(t),y(t))-y(t))

Solve this system using the numerical solver ode15s. Before you use ode15s, assign the following values to symbolic parameters of the system: alpha = 0.01, beta = 0.02, f(t,x,y) = x*y. Also, replace the state variables x(t) and y(t) by variables Y1 and Y2, that are acceptable by matlabFunction.

syms Y1 Y2
M = subs(M,[vars,alpha,beta,f],[Y1,Y2,0.01,0.02,@(t,x,y) x*y]);
F = subs(F,[vars,alpha,beta,f],[Y1,Y2,0.01,0.02,@(t,x,y) x*y]);

Create the following function handles MM and FF. You can use these function handles as input arguments for odeset and ode15s. These functions require state variables to be specified as column vectors.

MM = matlabFunction(M,Vars={t,[Y1; Y2]});
FF = matlabFunction(F,Vars={t,[Y1; Y2]});

Solve the system using ode15s. Specify the initial values for the variables Y1 and Y2 as 20 and 10. Plot the solutions over the time interval [0 10].

opt = odeset(Mass=MM);
ode15s(FF,[0 15],[50; 20],opt)

Figure contains an axes object. The axes object contains 4 objects of type line.

Create the following system of differential algebraic equations. Here, the symbolic functions x1(t), x2(t), and x3(t) represent state variables of the system. The system also contains symbolic parameters a and b.

syms x1(t) x2(t) x3(t) a b
eqs = [a*diff(x1,t) == x1*x3- x2;
       diff(x2,t) == b*x1 - 1;
       0 == x1 + x2 + x3]
eqs(t) = 

(at x1(t)=x1(t)x3(t)-x2(t)t x2(t)=bx1(t)-10=x1(t)+x2(t)+x3(t))

Find the state variables of the system of equations. Often, these variables are functions of time only. To programmatically determine the state variables, use findSymType by specifying symfunOf as t.

vars = findSymType(eqs,"symfunOf",t)
vars = (x1(t)x2(t)x3(t))

Find the mass matrix form of this system.

[M,F] = massMatrixForm(eqs,vars)
M = 

(a00010000)

F = 

(x1(t)x3(t)-x2(t)bx1(t)-1x1(t)+x2(t)+x3(t))

Next, assign the values of a and b as 2 and 1, respectively. Also, replace the state variables by variables Y1, Y2, and so on, that are acceptable by matlabFunction. You can use sym to programmatically generate the variables Y1, Y2, and so on.

numVars = numel(vars);
vecY = sym("Y",[1,numVars]);
M = subs(M,[a b vars],[2 1 vecY]);
F = subs(F,[a b vars],[2 1 vecY]);

Create the following function handles MM and FF. You use these function handles as input arguments for odeset and ode15s. These functions require state variables to be specified as column vectors. Because the symbolic variables in vecY are complex, use the nonconjugate transpose to convert vecY to a column vector.

MM = matlabFunction(M,Vars={t,vecY.'});
FF = matlabFunction(F,Vars={t,vecY.'});

Solve the system using ode15s. Specify the initial values for the variables Y1, Y2, and Y3 as 1, 1, and -2, respectively. Plot the solutions over the time interval [0 15].

opt = odeset(Mass=MM);
ode15s(FF,[0 15],[1; 1; -2],opt)

Figure contains an axes object. The axes object contains 6 objects of type line.

Input Arguments

collapse all

System of semilinear first-order DAEs, specified as a vector of symbolic equations or expressions.

State variables, specified as a vector of symbolic functions or function calls, such as x(t).

Example: [x(t),y(t)] or [x(t);y(t)]

Output Arguments

collapse all

Mass matrix of the system, returned as a symbolic matrix. The number of rows is the number of equations in eqs, and the number of columns is the number of variables in vars.

Right sides of equations, returned as a column vector of symbolic expressions. The number of elements in this vector is equal to the number of equations in eqs.

Version History

Introduced in R2014b