1
$\begingroup$

I'm having an output problem with the following code :

fl := NDSolve[{
    q''[t] == -(p'[t]^2 - (3/4) (p[t]^2 - 4)^2) q[t],
    q[t] p''[t] == - 3 q'[t] p'[t] - q[t] 3 (p[t]^2 - 4) p[t],

    q[0] == 1, q'[0] == 1, p[0] == 1, p'[0] == 0

    }, {q, p}, {t, -50, 50},
    Method -> "StiffnessSwitching"
]

T1 := (q /. fl)[[1]][[1]][[1]][[1]]
T2 := (q /. fl)[[1]][[1]][[1]][[2]]
Evaluate[p[T1] /. fl]
Evaluate[p[T2] /. fl]

Plot[Evaluate[p[t] /. fl], {t, T1, T2}, PlotRange -> All, Frame -> True]

The plot appears to show only the main part of the curve, but some parts are missing on its extremities. The evaluation at min time T1 and max time T2 shows that the curve should continue up to approx p[T1] = 16 and p[T2] = 15.68. The plot ends the curve at approx values p = 8.5 and p = 9.1. The PlotRange -> All directive doesn't seem to help here. Why ? How to show the full curve up to its extremities ?

$\endgroup$
3
  • 1
    $\begingroup$ [[1]][[1]][[1]][[1]] can be replaced with [[1,1,1,1]] $\endgroup$
    – Feyre
    Dec 24 '16 at 19:41
  • $\begingroup$ @Feyre, thanks for the trick. It works, but doesn't change my output problem. $\endgroup$
    – Cham
    Dec 24 '16 at 19:43
  • 1
    $\begingroup$ I found an older question on the same topic that already covers both ListLinePlot and ParametricPlot so I am marking this question as a duplicate. $\endgroup$
    – Mr.Wizard
    Dec 24 '16 at 20:17
2
$\begingroup$

I just found a simple trick : to use ParametricPlot, instead of Plot :

ParametricPlot[Evaluate[{t, p[t]} /. fl], {t, T1, T2}, PlotRange -> All, Frame -> True]

Now, is there a drawback with this solution ?

$\endgroup$
2
  • 1
    $\begingroup$ I'd add , AspectRatio -> 1/GoldenRatio $\endgroup$
    – Feyre
    Dec 24 '16 at 20:06
  • 1
    $\begingroup$ Off hand I don't know why ParametricPlot includes the end points and Plot does not. Clearly that helps here but conceivably that behavior may change between versions. Manual points (Table etc.) has the advantage of never changing. (And +1 for sharing your solution of course.) $\endgroup$
    – Mr.Wizard
    Dec 24 '16 at 20:06
1
$\begingroup$

The slope approaching T1 and T2 is extreme. By default the plot mesh doesn't get close enough to these values to show the limits. I recommend using ListLinePlot on a series of directly calculated points:

foo = First[p[t] /. fl];

ListLinePlot[
 Table[{t, foo}, {t, T1, T2, (T2 - T1)/1000}]
 , PlotRange -> All
 , Frame -> True
]

enter image description here

$\endgroup$
3
  • $\begingroup$ Ufortunately, that would complicate a lot my full code (which was simplified to show the problem here). So there is no other way to get the full curve, using a standard Plot, with some options ? $\endgroup$
    – Cham
    Dec 24 '16 at 20:00
  • $\begingroup$ @Cham If you describe why this would complicate your full code I might be able to suggest an improvement. I'll see if I can force Plot to give you want you want however. $\endgroup$
    – Mr.Wizard
    Dec 24 '16 at 20:02
  • $\begingroup$ I just found a simple trick : use ParametricPlot ! I'll post an answer ! ;-) $\endgroup$
    – Cham
    Dec 24 '16 at 20:02

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