2
$\begingroup$

First, I would like to solve a coupled differential equations. For example:

A0 = {1, 1}; (* initial condition *)
T = 1;  (* time T *)
sol=NDSolve[{a'[t]==a[t]+b[t],b'[t]==a[t]-b[t],{a[0],b[0]}==A0},{a,b},{t,0,T}];
{a[T], b[T]} /. sol[[1]] (* output *)

Then the solution at T is {4.91478, 2.17818}.

Next, I would like to scan the initial condition A0. For example, I would like

A0={Sin[theta],Cos[theta]}

Scan theta from 0 to 2Pi by steps Pi/10, and calculate all the output a[T] and b[T] in a table or saved in other formats. It would be best that the data can be save in this form {{theta_1, a[T]_1,b[T]_1},{theta_2, a[T]_2,b[T]_2}...}. The final result can be plotted, for example, plot "theta - a[T]".

How to achieve this?

$\endgroup$

2 Answers 2

3
$\begingroup$

Use ParametricNDSolve/ParametricNDSolveValue instead:

pf = ParametricNDSolveValue[
    {a'[t] == {{1, 1}, {1, -1}} . a[t], a[0]=={a0,b0}},
    a,
    {t, 0, 1},
    {a0, b0}
];

(Note, I also converted your equation into vector form). Check that it agrees with your initial A0:

pf[1, 1][1]

{4.91478, 2.17818}

Table output for varying A0:

Table[Flatten[{θ, pf[Sin[θ], Cos[θ]][1]}], {θ, 0, 2 π, π/10}]

{{0, 1.3683, 0.809885}, {π/10, 2.39725, 1.19307}, {π/5, 3.19155, 1.45948}, {(3 π)/10, 3.67343, 1.58302}, {(2 π)/5, 3.79573, 1.5516}, {π/2, 3.54648, 1.3683}, {(3 π)/5, 2.95008, 1.05106}, {( 7 π)/10, 2.0649, 0.630939}, {(4 π)/5, 0.977593, 0.149055}, {( 9 π)/10, -0.205406, -0.347419}, {π, -1.3683, -0.809885}, {( 11 π)/10, -2.39725, -1.19307}, {(6 π)/5, -3.19155, -1.45948}, {( 13 π)/10, -3.67343, -1.58302}, {(7 π)/5, -3.79573, -1.5516}, {( 3 π)/2, -3.54648, -1.3683}, {(8 π)/5, -2.95008, -1.05106}, {( 17 π)/10, -2.0649, -0.630939}, {(9 π)/5, -0.977593, -0.149055}, {( 19 π)/10, 0.205406, 0.347419}, {2 π, 1.3683, 0.809885}}

If you want a plot, you can just use:

Plot[pf[Sin[θ], Cos[θ]][1], {θ, 0, 2 π}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Can I now construct a set of trajectories for different initial conditions using ParametricPlot3D? $\endgroup$
    – ayr
    Commented Jun 4, 2021 at 7:58
2
$\begingroup$
Table[{theta, {a[T], b[T]} /. NDSolve[{a'[t] == a[t] + b[t], b'[t] == a[t] - b[t], {a[0], b[0]} == {Sin[theta], Cos[theta]}}, {a, b}, {t, 0, T}][[1]]}, {theta, 0., 2 \[Pi], \[Pi]/10}]

That, or, you know, your system can be solved analytically with DSolve.

$\endgroup$
1
  • $\begingroup$ Yes. Thank you. I just use this as an example. $\endgroup$
    – Qi Zhong
    Commented Feb 5, 2018 at 18:20

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.