3
$\begingroup$

I have been tried to do two things with the solutions from NDSolveValue

  • plot the norm of the solutions of a differential equation system versus time.
  • plot one component of the solutions of a differential equation system versus time.

but I have been having difficulty it setting up the right syntax to do so.

The problem seems to be (for plotting the norms of the solutions), that Mathematica takes the norm of all the solutions, or tries to find the norm of a function rather than the value of the function at a certain time.

I have created a minimum working example from the original code. The major change is that in the original code set is a random set of $n$ points. The examples are my best guess for the correct syntax for the problems listed above. For context I have included a 3D parametric plot which works as intended.

If you have any questions please don't be afraid to ask.

Minimum Example

(*Simulation Parameters*)
Clear[i, P, B]
Clear[f]
f[P_, B_] := 1/2 P + 10 B/(1 + B);
tmax = 20;
A = {{1/20, 1/4, 1/50}, {1/4, 1/26, 1/40}};
set = {{1.1, 11.2, 0.2}, {5.6, 4.3, 7.8}, {2.3, 3.4, 3.4}};

(*ODE System*)
ODEsys = {i'[t] == f[P[t], B[t]] - i[t],
   P'[t] == 
    P[t] (1 - A[[1, 1]] P[t] - A[[1, 2]] B[t] - A[[1, 3]] i[t]),
   B'[t] == B[t] (1 - A[[2, 2]] B[t] - A[[2, 3]] i[t])};

(* Simulation *)
With[{ttmax = tmax}, 
  sol = ParametricNDSolveValue[{ODEsys, {P[0] == init1, B[0] == init2,
       i[0] == init0}}, {P, B, i}, {t, 0, ttmax}, {init1, init2, 
     init0}]];

(* Plots I am having trouble with *)
(* Cannot plot the first component of multiple solutions. *)
ParametricPlot[{t, 
  Evaluate[Through /@ (sol[#[[1]], #[[2]], #[[3]]][t] & /@ set)][[All,
     1]]}, {t, 0, tmax}, PlotRange -> All]

(* Takes the norm of all solutions. Does not plot the norms of the \
three different solutions. *)
ParametricPlot[{t, 
  Norm[Evaluate[
    Through /@ (sol[#[[1]], #[[2]], #[[3]]][t] & /@ set)]]}, {t, 0, 
  tmax}, PlotRange -> All]

(* This plot works as attended. *)
trajectoriesPlot = 
 ParametricPlot3D[
  Evaluate[Through /@ (sol[#[[1]], #[[2]], #[[3]]][t] & /@ set)], {t, 
   0, tmax}, PlotRange -> All]
$\endgroup$
  • $\begingroup$ I am looking for the default Euclidean norm e.g. $||(P_1,B_1,I_3)||=\sqrt{P_1^2+B_1^2+I_3^2}$ not $||(P_1,B_1,I_3)||=|P_1|+|B_1|+|I_3| or anything else. Abs[] does not give the magnitude of a vector. $\endgroup$ – AzJ Feb 5 at 22:20
4
$\begingroup$
  • plot the norm of the solutions of a differential equation system versus time.
Plot[Evaluate@(Norm[Through[sol[## & @@ #][t]]] & /@ set), {t, 0, tmax}, 
 PlotRange -> All, AspectRatio -> 1, ImageSize -> Large, 
 PlotLegends -> Placed[ToString /@ set, Top], 
 PlotLabel -> (Norm[{P[t], B[t], i[t]}])]

enter image description here

  • plot one component of the solutions of a differential equation system versus time.

Row[ParametricPlot[Evaluate@Thread[{t, (Through[sol[## & @@ #][t]] & /@ set)[[All, #]]}], 
    {t, 0, tmax}, PlotRange -> All, 
    PlotLegends -> Placed[set[[All, #]], Top] , AspectRatio -> 1, 
    ImageSize -> 300, PlotLabel -> ({P[t], B[t], i[t]}[[#]])] & /@ {1,
    2, 3}, Spacer[5]]

enter image description here

Alternatively, you can use Plot:

Row[Plot[Evaluate@(Through[sol[## & @@ #][t]] & /@ set)[[All, #]], {t, 0, tmax}, 
    PlotRange -> All, AspectRatio -> 1, ImageSize -> 300, 
    PlotLegends -> Placed[set[[All, #]], Top] , 
    PlotLabel -> ({P[t], B[t], i[t]}[[#]])] & /@ {1, 2, 3}, Spacer[5]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ I really like this solution exactly what I was looking for and more. $\endgroup$ – AzJ Feb 5 at 22:52
2
$\begingroup$
sol = ParametricNDSolveValue[{ODEsys, {P[0] == init1, B[0] == init2,i[0] == init0}}, {P[t], B[t], i[t]}, {t, 0, tmax}, {init1, init2,init0}]

plot of solutions:

Plot[Table[# &[ Apply[sol, set[[i]]]]  , {i, 1, Length[set]}] , {t, 0,tmax}, PlotRange -> {0, Automatic}]

plot of euclidean norm:

Plot[Table[Sqrt[ #.#] &[ Apply[sol, set[[i]]] ], {i, 1, Length[set]}] , {t, 0,tmax}, PlotRange -> {0, Automatic}]
$\endgroup$
  • $\begingroup$ This does not answer my question for my reasons. First something like Plot[Table[Map[#[t] &, Apply[sol, set[[i]]]], {i, 2, 2}], {t, 0,tmax}] does not plot all of the second components of all the solutions. It plots all the components of the second solution. Next I meant Euclidean Norm, not some other norm (I thought it was self evident as that is the default used by Mathematica when given a vector). Playing around with your code snippet I haven't been able to get the desired result $\endgroup$ – AzJ Feb 5 at 22:00
  • $\begingroup$ My plot shows 3x3 solutions as you asked for! $\endgroup$ – Ulrich Neumann Feb 5 at 22:07
  • $\begingroup$ Sorry my terminology may have confused you. For the context of my problem as I am solving a system of ODEs one function (for example $P(t)$) is a component of the solution $(P(t),B(t),I(t))$. $\endgroup$ – AzJ Feb 5 at 22:12
  • $\begingroup$ If I have three solutions $(P_1,B_1,I_1)$,$(P_2,B_2,I_2)$,$(P_3,B_3,I_3)$ (with the difference being they start at different initial conditions), I am looking for plots of the following (as examples): $P_1,P_2,P_3$ versus time, and $||(P_1,B_1,I_3)||=\sqrt{P_1^2+B_1^2+I_3^2}$ versus time. $\endgroup$ – AzJ Feb 5 at 22:17
  • $\begingroup$ Thanks for your corrected answer $\endgroup$ – AzJ Feb 5 at 22:51

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.