1
$\begingroup$

I would like to know how can I obtained a loop of this system

Clear["Global`*"]

n0 = 3;

Nmax = 5;

A[1] = {{0.5218440349603428`, 1.`, 0.`, 0.`, 0.`}, {1.`, 
    0.03043455783984461`, 1.`, 0.`, 0.`}, {0.`, 
    1.`, -0.5733456379977422`, 1.`, 0.`}, {0.`, 0.`, 
    1.`, -0.1691687728719371`, 1.`}, {0.`, 0.`, 0.`, 
    1.`, -0.6766851294154084`}};

A[2] = {{0.5920373626109177`, 1.`, 0.`, 0.`, 0.`}, {1.`, 
    0.060631154827360145`, 1.`, 0.`, 0.`}, {0.`, 
    1.`, -0.02863887747946947`, 1.`, 0.`}, {0.`, 0.`, 
    1.`, -0.35285446053637504`, 1.`}, {0.`, 0.`, 0.`, 1.`, 
    0.36012246351093635`}};

A[3] = {{0.67202601044949`, 1.`, 0.`, 0.`, 
    0.`}, {1.`, -0.3920737805614185`, 1.`, 0.`, 0.`}, {0.`, 
    1.`, -0.05678745158627674`, 1.`, 0.`}, {0.`, 0.`, 1.`, 
    0.5330963738805732`, 1.`}, {0.`, 0.`, 0.`, 1.`, 
    0.11619190701387883`}};

\[Psi]ini = Table[KroneckerDelta[n0 - i], {i, 1, Nmax}];

usol[1] = 
  NDSolveValue[{I D[\[Psi][t], t] == 
     A[1].\[Psi][t], \[Psi][0] == \[Psi]ini}, \[Psi], {t, 0, 10}];

usol[2] = 
  NDSolveValue[{I D[\[Psi][t], t] == A[2].\[Psi][t], \[Psi][0] == 
     usol[1][10]}, \[Psi], {t, 10, 20}];

usol[3] = 
  NDSolveValue[{I D[\[Psi][t], t] == A[3].\[Psi][t], \[Psi][0] == 
     usol[2][20]}, \[Psi], {t, 20, 30}];

In this form works but I would like to do in loop. Actually, I would like too joined all the usol[i] and plot them.

$\endgroup$

1 Answer 1

2
$\begingroup$
$Version

"12.3.0 for Mac OS X x86 (64-bit) (May 10, 2021)"

Clear["Global`*"]

n0 = 3;
Nmax = 5;

A[1] = {{0.5218440349603428`, 1.`, 0.`, 0.`, 0.`}, {1.`, 0.03043455783984461`,
     1.`, 0.`, 0.`}, {0.`, 1.`, -0.5733456379977422`, 1.`, 0.`}, {0.`, 0.`, 
    1.`, -0.1691687728719371`, 1.`}, {0.`, 0.`, 0.`, 
    1.`, -0.6766851294154084`}};
A[2] = {{0.5920373626109177`, 1.`, 0.`, 0.`, 0.`}, {1.`, 
    0.060631154827360145`, 1.`, 0.`, 0.`}, {0.`, 1.`, -0.02863887747946947`, 
    1.`, 0.`}, {0.`, 0.`, 1.`, -0.35285446053637504`, 1.`}, {0.`, 0.`, 0.`, 
    1.`, 0.36012246351093635`}};
A[3] = {{0.67202601044949`, 1.`, 0.`, 0.`, 0.`}, {1.`, -0.3920737805614185`, 
    1.`, 0.`, 0.`}, {0.`, 1.`, -0.05678745158627674`, 1.`, 0.`}, {0.`, 0.`, 
    1.`, 0.5330963738805732`, 1.`}, {0.`, 0.`, 0.`, 1.`, 
    0.11619190701387883`}};

Replace ψini with usol[0][t] that is constant for all t.

usol[0][t_] = Table[KroneckerDelta[n0 - i], {i, 1, Nmax}];

Then,

(usol[#] = 
     NDSolveValue[{I D[ψ[t], t] == A[#] . ψ[t], ψ[0] == 
        usol[# - 1][10 (# - 1)]}, ψ, {t, 10 (# - 1), 10 #}]) & /@ 
  Range[3];

EDIT: Assuming that you want to plot the absolute value of the complex numbers,

Column[
 Table[
  ListLinePlot[
   Abs@Transpose@Table[usol[n][t],
      {t, 10 (n - 1), 10 n, 0.1}],
   DataRange -> {10 (n - 1), 10 n},
   PlotLabel ->
    Style[StringForm["Abs[usol[``]", n], 14, Bold],
   PlotLegends -> Automatic,
   ImageSize -> Medium],
  {n, 1, 3}]]

enter image description here

Legended[
 Show[
  Table[
   ListLinePlot[Abs@Transpose@Table[usol[n][t],
       {t, 10 (n - 1), 10 n, 0.1}],
    DataRange -> {10 (n - 1), 10 n}],
   {n, 1, 3}],
  PlotRange -> All,
  ImageSize -> Large],
 LineLegend[ColorData[97] /@ Range[5], Range[5]]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks @Bob Hanlon this is the loop which I need. But now I would like to plot usol[#] but the command "Plot" doesn´t work. How can I plot them? $\endgroup$ Commented May 29, 2021 at 19:47

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.