0
$\begingroup$

So, I have solved a coupled differential equation using NDsolve here:

enter image description here

I want the functions x[t] and y[t] for some specific parameter range, say for t>0 or t<0, and store it in some array perhaps for some computation. Can anyone tell me how this can be done?

Furthermore, How can I use different plot styles in Parametric plot for parameter ranges t>0 and t<0, How to go about doing this?

Thank you.

$\endgroup$
2
  • 3
    $\begingroup$ Please post the code,not the picture. $\endgroup$
    – cvgmt
    Commented Jul 26, 2021 at 12:27
  • $\begingroup$ Try data=Table[{t, x[t], y[t]},{t, -1.7, 1.6}]; Export["ode.dat", data] $\endgroup$
    – yarchik
    Commented Jul 26, 2021 at 12:45

1 Answer 1

2
$\begingroup$

I'll use the Lotka-Volterra model as an example since I can't copy your code.

First way: Use Show to get the forward t>0 and backward t<0 solutions to be different colors.

{xsol, ysol} = NDSolveValue[{
   x'[t] == x[t] - 2 x[t] y[t],
   y'[t] == x[t] y[t] - y[t],
   x[0] == y[0] == 1},
  {x, y}, {t, -20, 20}]


Show[{
  ParametricPlot[{xsol[t], ysol[t]}, {t, -4, 0}, PlotStyle -> Red, 
   PlotRange -> {{0, 2.5}, {0, 1.5}},
   Prolog -> {PointSize[Large], Blue, Point[{1, 1}]}],
  ParametricPlot[{xsol[t], ysol[t]}, {t, 0, 4}, PlotStyle -> Green]}]

Using Show

Second way: Have a forward and a backward NDSolve.

Forward solution

{xsolf, ysolf} = NDSolveValue[{
   x'[t] == x[t] - 2 x[t] y[t],
   y'[t] == x[t] y[t] - y[t],
   x[0] == y[0] == 1},
  {x, y}, {t, 0, 20}]

Backward solution

{xsolb, ysolb} = NDSolveValue[{
   x'[t] == -(x[t] - 2 x[t] y[t]),
   y'[t] == -(x[t] y[t] - y[t]),
   x[0] == y[0] == 1},
  {x, y}, {t, 0, 20}]

Notice that the backward solve has the same initial conditions and time range as the forward solve, but that the equations are negated.

This lets you use only a single ParametricPlot for both forward and backward solutions.

ParametricPlot[{{xsolf[t], ysolf[t]}, {xsolb[t], ysolb[t]}}, {t, 0, 4}, 
  PlotStyle -> {Red, Green}, 
  Prolog -> {PointSize[Large], Blue, Point[{1, 1}]}]

enter image description here

I've also gone with NDSolveValue instead of the regular NDSolve since you asked to store the data in a table which you can do pretty easily with

xfData = Table[xsolf[t], {t, 0, 5, .1}]

If you use the second way, remember that for the backward solve time is "negative time" so you might need to do something like

xbData = Table[xsolb[t], {t, 10, 0, -.1}]
$\endgroup$

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.