Using the solution of a differential equation in two separate graphic commands in Mathematica

I ran into a problem while trying to use an answer from NDSolve in two separate graph commands. To illustrate the problem, I use a simple differential equation and only one plot command. If I write something like this:

{Plot[x[t], {t, 0, 10}], x[4]} /. NDSolve[{x'[s] == - x[s], x[0] == 1}, x, {s, 0, 10}] 

He solves the equation and calculates x [4] without any problems, but the graph becomes empty and I have no idea why.

In my real problem, my equation is a rather complicated system for several functions, and instead of x [4] I draw a parametric graph of the solved functions. Ultimately, I intend to include all of this in the Manipulate operator, so I don’t want the NDSolve operator to appear more than once (takes too much time), and I can’t just calculate it in advance (since it has many parameters).


Edit: I would like to clarify and expand my question: what I really want to do is to include in my statement Manipulate the construction expression as follows:

 Manipulate[{Plot[x[t], {t, 0, 10}], x[4]} /. NDSolve[{x'[s] == - a*x[s], x[0] == 1}, x, {s, 0, 10}] ,{{a,1},0,5}] 

Since only the Manipulate operator gives a value for parameter a, I cannot calculate the answer to NDSolve in advance . In addition, since my actual system of equations is very complex and non-linear, I cannot use the symbolic function DSolve .

Sorry if it wasn’t clear before.

+6
wolfram-mathematica plot differential-equations
source share
2 answers

Your problem is that Plot [] does some funny things to make the construction more convenient, and one of the things that it does is simply not plot things that it cannot be numerically evaluated. So, in the expression you posted,

 Plot[x[t], {t, 0, 10}] 

just goes ahead and evaluates before making a rule change with a solution from NDSolve, creating an empty area graphic object. This graphic does not contain a reference to x, so there is nothing to replace it with.

You want to make sure that the replacement is done before the graphics. If you also want to make sure that the substitution can be done in several places, you want to save the solution in a variable.

 sol = NDSolve[{x'[s] == - x[s], x[0] == 1}, x, {s, 0, 10}]; {Plot[Evaluate[x[t] /. sol], {t, 0, 10}], x[4] /. sol} 

An estimate of [] on the graph ensures that Mathematica performs only one change once, and not once, for each point in the graph. It is not important to simply replace a rule like this, but it is a good habit to use it if you ever want to build something more complex.


To make this work in Manipulate, a simple way is to use With [], which is one of Schematic Mathematica's constructs; this is the one to use when you just want to replace something without using it as a variable that you can mutate.

For example,

 Manipulate[ With[{sol = NDSolve[{x'[s] == - x[s], x[0] == 1}, x, {s, 0, 10}]}, {Plot[x[t] /. sol // Evaluate, {t, 0, 10}, PlotRange -> {0, 1}], x[4] /. sol}], {{a, 1}, {0, 5}}] 

Use the PlotRange parameter to fix the y axis; otherwise, things will jump in an ugly way, like the meaning of change. When you perform more complex actions with Manipulate, there are a number of options for controlling the speed of updates, which may be important if your ODE is complicated enough to solve for a while.

+8
source share

Meanwhile, I found another way to do this. It is less elegant, but it uses only one replacement, so I thought I would post it here too.

The idea is to use the Hold on the Story so that it is not evaluated, does not perform a rule change, and then ReleaseHold , just before Manipulating .

 Manipulate[ReleaseHold[ Hold[ {Plot[x[t], {t, 0, 10}, PlotRange -> {0, 1}], x[4]} ] /.NDSolve[{x'[s] == -ax[s], x[0] == 1}, x, {s, 0, 10}] ], {{a, 1}, 0, 5}] 
+2
source share

All Articles