3
$\begingroup$

I would like to numerically solve (with NDSolve ) the following system of ordinary differential equations, for different, fixed $M$,
$$\begin{array}{c} y_{1}'(t)=Fy_{2}(t)\\ y_{2}'(t)=2Fy_{3}(t)\\ \vdots\\ y_{M-1}'(t)=(M-1)Fy_{M}(t)\\ (M-1)!F^{M-1}y_{M}'(t)=y_{1}(t) \end{array}$$

with initial conditions $y_{k}(0)=\left(x_{0}\right)^{k}$, $k=1,2,\ldots,M$ and $F$ is some fixed constant.

Is there a slick way to generate the $M$ equations above and the initial conditions, so that they can be passed to ``NDSolve?

**Edit: ** To be precise I don't need exactly the solution of the above ODE but something similar. So unfortunately @polfosol answr does not work for me. I am really looking for a way to generate several equations of the kind depicted above.

$\endgroup$
2
  • $\begingroup$ I did try but I got a very weird error message, something like: Your post appear to have code that is not properly indented. I'm familiar with MathJax $\endgroup$
    – lcv
    Jan 24, 2023 at 18:46
  • $\begingroup$ This is the ODE \begin{array}{c} y_{1}'(t)=Fy_{2}(t)\\ y_{2}'(t)=2Fy_{3}(t)\\ \vdots\\ y_{M-1}'(t)=(M-1)Fy_{M}(t)\\ (M-1)!F^{M-1}y_{M}'(t)=y_{1}(t) \end{array} $\endgroup$
    – lcv
    Jan 24, 2023 at 18:47

2 Answers 2

2
$\begingroup$

Edit: Corrected typos from 1/24/23, ran through NDSolveValue and checked plots

Edit 2: changed code to make easier to plot all with a legend or a selected solution

You can use subscripted variables:

M = 4;
F = 2;
Subscript[x, 0] = 1;
theDepVars = {};
innerEqnSet = Table[
   AppendTo[theDepVars, Subscript[y, k][t]];
   Subscript[y, k]'[t] == k F Subscript[y, k + 1][t],
   {k, 1, M - 1}];
eqnM = (M - 1)! F^(M - 1) Subscript[y, M]'[t] == Subscript[y, 1][t];
AppendTo[theDepVars, Subscript[y, M][t]];
eqnMInitVal = Subscript[y, M][0] == Subscript[x, 0]^M;
innerEqnInitVals = 
  Table[Subscript[y, k][0] == Subscript[x, 0]^k, {k, 1, M - 1}];
myIVP = Join[innerEqnSet, {eqnM}, innerEqnInitVals, {eqnMInitVal}]
theDepVars
theSols = NDSolveValue[myIVP, theDepVars, {t, 0, 1}]

Plot[theSols, {t, 0, 1}, 
 PlotLegends -> Placed[theDepVars, {0.29, 0.7}]]

Or:

Plot[theSols[[1]], {t, 0, 1}]

to plot $y_1$ and so forth. Also easier to use Ctrl-underscore key to enter a subscripted variable to look like $y_1$ in the notebook for example rather than all the text above.

enter image description here

$\endgroup$
3
  • $\begingroup$ Great! (+1) I think this is what I was looking for. Let me take a look at it tomorrow morning. $\endgroup$
    – lcv
    Jan 24, 2023 at 23:36
  • $\begingroup$ @lcv: Had a few typos in post from 1/24/23. I corrected them and verified system ran ok through NDSolveValue. $\endgroup$
    – josh
    Jan 25, 2023 at 10:26
  • $\begingroup$ Works like a treat. $\endgroup$
    – lcv
    Jan 25, 2023 at 15:56
3
$\begingroup$

If you don't insist on using NDSolve, there is an (IMO simpler) workaround for your specific problem. Since your ODE is linear, it has an analytic solution. Let $y=[\matrix{y_1&y_2&\cdots&y_M}]^T$, then: $$y'(t)=Ay(t),\qquad y(0)=y_0$$ where $$A=\left[\matrix{0&1&0&\cdots&0\\0&0&2&\cdots&0\\ \vdots&\vdots&\vdots&\ddots&\vdots\\ 0&0&0&\cdots&M-1\\ \frac1{(M-1)!F^M}&0&0&\cdots&0}\right]F,\qquad y_0=\left[\matrix{x_0\\x_0^2\\\vdots\\x_0^M}\right]$$ The solution to this linear ODE system is given by $$y(t)=\exp(At)y(0)$$ Then you can use MatrixExp function to calculate $\exp(At)$.

$\endgroup$
1
  • $\begingroup$ Thank very much you but I really need the NDSolve version. The example I posted is a baby version of my actual problem. I should have realized that it could lead to such solution. $\endgroup$
    – lcv
    Jan 24, 2023 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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