# How to solve set of linear differential equation in Mathematica?

I have a set of linear differential equations and are written in this format
[X']=[Y]-[R]
where [P][Y]=[Q] My actual P matrix size is of 20x20 and Q is of 20x1 and Y is of 20x1. I have written this code in Matlab there it was very easy. I'm finding it very difficult to write it in Mathematica.
For example P, Q and R as follows What is the plot(x1,x2) and plot(t,x1). If you find this example meaningless kindly take your own P, Q and R functions and solve it. Kindly provide the Mathematica code.

• Please show us the text of code, not the screenshot of it. – xzczd Sep 20 at 6:44
• So where is the differential equation you are trying to solve? Note, in MA $df(x)/dx$ is written as D[f[x],x]. The equal sign in equations is ==. This = is an assignment operator(reference.wolfram.com/language/tutorial/…). – yarchik Sep 20 at 6:45
• Please, look in the Documentation (reference.wolfram.com/language/ref/NDSolve.html) on how the differential equations are written. This is another Tutorial reference.wolfram.com/language/tutorial/… – yarchik Sep 20 at 6:47
• You mentioned "x'" implies derivative with respect to time "t", but also introduced notations like x', dx and dy in your question, what exactly is the ODE system you want to solve? Please show us the equation in traditional math notation. – xzczd Sep 20 at 6:58
• Solving this using NDSolve should be straightforward, which part are you having difficulty? Have you read the document shown by @yarchik ? – xzczd Sep 20 at 11:33

We solve a system of ODEs: $$p(t)\cdot\dot{\vec{x}}(t)=\vec{q}(t)-p(t)\cdot\vec{r}(t)$$ with $$p(t)=\left(\begin{array}{cc}\sin(t)&\exp(t)\\1&\cos(t)\end{array}\right), \quad \vec{x}(t)=\left(\begin{array}{c}x_1(t)\\x_2(t)\end{array}\right),$$ $$\vec{r}(t)=\vec{x}(t)+\left(\begin{array}{c}\sin(t)\\\cos(t)\end{array}\right), \quad \vec q(t)=\left(\begin{array}{c}x_1(t)\sin(t)\\1+x_1(t)x_2(t)\end{array}\right).$$ where $$p(t)$$ is a matrix and $$\vec{x}(t),\vec{q}(t),\vec{r}(t)$$ are time-dependent vectors. Notice, that this form is equivalent to the original formulation, but avoids matrix inversion.

• A list of dependent variables vxand a list of initial conditions vx0.

vx=Array[x,2];
vx0={0,1};

• Build a matrix pand two vectors q and r.

p={{Sin[t],Exp[t]},{1,Cos[t]}};
q={x[1][t]Sin[t],1+x[2][t]x[1][t]};
r=Through[vx@t]+{Sin[t],Cos[t]};


Notice:

1. vx is equal to {x[1],x[2]},
2. Through[vx@t] is equal to {x[1][t],x[2][t]},
3. Through[vx@0] is equal to {x[1][0],x[2][0]}.

• Construct equations eqs and initial conditions ics. Notice that Equal is just the full form of ==, a dot . means matrix-vector (or matrix-matrix) multiplication, and D[Through[vx@t],t] is the time-derivative of the vector of dependent variables, that is $$\dot{\vec{x}}(t)=\left\{\dot{x}_1(t),\dot{x}_2(t)\right\}^T$$:

eqs=MapThread[Equal,{p.D[Through[vx@t],t],q-p.r}];

• Now solve and plot the first solution First[sol]. In fact, there is a unique solution:
sol=NDSolve[Join[eqs,ics],vx,{t,20}];

• Actually it's not necessary to use MapThread and Join, NDSolve can handle nested lists and equations like {…, …}=={…, …}, at least since v9. – xzczd Sep 20 at 12:32