0
$\begingroup$

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 enter image description here 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.

$\endgroup$
  • 1
    $\begingroup$ Please show us the text of code, not the screenshot of it. $\endgroup$ – xzczd Sep 20 at 6:44
  • 1
    $\begingroup$ 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/…). $\endgroup$ – yarchik Sep 20 at 6:45
  • 1
    $\begingroup$ 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/… $\endgroup$ – yarchik Sep 20 at 6:47
  • $\begingroup$ 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. $\endgroup$ – xzczd Sep 20 at 6:58
  • $\begingroup$ Solving this using NDSolve should be straightforward, which part are you having difficulty? Have you read the document shown by @yarchik ? $\endgroup$ – xzczd Sep 20 at 11:33
2
$\begingroup$

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}];
ics=MapThread[Equal,{Through[vx@0],vx0}];
  • Now solve and plot the first solution First[sol]. In fact, there is a unique solution:

sol=NDSolve[Join[eqs,ics],vx,{t,20}];
Plot[Through[vx@t]/.First[sol],{t,0,20},Evaluated->True]

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Actually it's not necessary to use MapThread and Join, NDSolve can handle nested lists and equations like {…, …}=={…, …}, at least since v9. $\endgroup$ – xzczd Sep 20 at 12:32
  • $\begingroup$ @xzczd Yes, that is even more terse. $\endgroup$ – yarchik Sep 20 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.