1
$\begingroup$

Suppose I numerically solve a differential equation by using

sol = ParametricNDSolve[{y'[x] == b y[x], y[0] == 1}, y, {x, 0, 0.3}, {b}]

And then I want to plot $y[x]$ for $b=0.1,0.2,0.3$ on intervals $[0,b]$ in the same graph. How can that be achieved? Or more generally, plots on, e.g., $[0,0.2],[0,0.4],[0,0.6]$ respectively?

I use Evaluate$[\cdots]$ to enable the coloring, as

Plot[Evaluate[Table[y[b][x] /. sol, {b, 0.1, 0.3, 0.1}]], {x, 0, 0.3}]

and I would like to keep the coloring. multiple function plot on different intervals

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Feb 1 '16 at 11:06
  • $\begingroup$ Your Plot code for y[b]'[x] doesn't match the graph, which looks like y[b][x]. $\endgroup$ – Chris K Feb 1 '16 at 15:13
  • $\begingroup$ @ChrisK, Thanks, I didn't notice that. Now it is corrected. $\endgroup$ – Glenn Feb 1 '16 at 16:57
4
$\begingroup$

You can use a custom Piecewise function for the plotting,

Plot[Evaluate[
  Table[Piecewise[{{y[b][x], x <= b}}, Null] /. 
    sol, {b, {0.3, 0.2, 0.1}}]], {x, 0, 0.3}]

enter image description here

$\endgroup$
  • $\begingroup$ clever solution! +1) $\endgroup$ – user36273 Feb 1 '16 at 15:35
3
$\begingroup$

How about changing the domain {x,0,b} and adding an "ExtrapolationHandler" to your NDSolve, as:

sol = ParametricNDSolve[{y'[x] == b y[x], y[0] == 1}, y, {x, 0, b}, {b}, 
  "ExtrapolationHandler" -> {Indeterminate&, "WarningMessage"->False}]

Plot[Evaluate[Table[y[b][x] /. sol, {b, 0.1, 0.3, 0.1}]], {x, 0, 0.3}]

enter image description here

$\endgroup$
2
$\begingroup$
sol = ParametricNDSolve[{y'[x] == b y[x], y[0] == 1}, y, {x, 0, 0.3}, {b}]
Show@Table[Plot[y[b][x] /. sol, {x, 0, 0.6}], {b, {0.6, 0.4, 0.2}}]

enter image description here

supplement

You can plot it according to your needs.

Show@Table[Plot[y[b][x] /. sol, {x, 0, b}], {b, {0.3, 0.2, 0.1}}]

enter image description here

Plot[Evaluate[Table[y[b][x] /. sol, {b, {0.3, 0.2, 0.1}}]], {x, 0, 
  0.3}, PlotRange -> All, PlotLegends -> {0.3, 0.2, 0.1}]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for the answer. But I want to plot $y[x]$ for $b=0.1$ on interval $[0, 0.1]$, and for $b=0.2$ on $[0, 0.2]$, and so on, not on the same interval or domain. $\endgroup$ – Glenn Feb 1 '16 at 11:22
  • $\begingroup$ You can simply replace {x, 0, 0.6} with {x, 0, b}. $\endgroup$ – b.gates.you.know.what Feb 1 '16 at 12:30
  • $\begingroup$ Oh, that works. But I forgot to mention that I also need to do the coloring. And Show[] seems not to be able to do that. $\endgroup$ – Glenn Feb 1 '16 at 13:13

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.