4
$\begingroup$

I have the following simple differential equation

ODE = DSolve[{x'[t] == 1, x[0] == 1}, x[t], t]

and I'd like to visualize how the solution x[t] behaves over time in space (which is just along the x-axis), so I tried the following

Manipulate[ListPlot[{{x[t] /. ODE, 0}},PlotRange->{{0,10},{-1,1}}], {t, 0, 10}]

but this gives blank output. Oddly the simple Plot command outputs expected result. I assume that the nature of these two commands is inherently different, or Manipulate is screwing something here. What would be the reason/would there be any word-around?

$\endgroup$
4
$\begingroup$

The problem here is the way the replacements are made. ODE is a replacement rule that looks like:

{{x[t] -> 1 + t}}

The Manipulate effectively tries to evaluate something like the following (for some value of t you selected):

DynamicModule[{t = 10}, ListPlot[{{x[t] /. ODE, 0}}]]

The problem here is that during the evaluation, x[t] first gets evaluated to x[10] and then the replacement rule x[t] -> 1 + t doesn't match x[10] any more.

You can fix this in two ways. The first way is to use this definition of ODE instead:

ODE = First @ DSolve[{x'[t] == 1, x[0] == 1}, x, t]
Manipulate[ListPlot[{{x[t] /. ODE, 0}}, PlotRange -> {{0,10},{-1,1}}], {t, 0, 10}]

{x -> Function[{t}, 1 + t]}

This works because the replacement rule now specifies that x is a function, rather than that x[t] is a symbolic expression.

The second (and in my opinion the superior) way is to use DSolveValue instead so you don't have to muck about with replacement rules at all:

ODE = DSolveValue[{x'[t] == 1, x[0] == 1}, x, t]
Manipulate[ListPlot[{{ODE[t], 0}}, PlotRange -> {{0,10},{-1,1}}], {t, 0, 30}]

Function[{t}, 1 + t]

Edit

You also remarked that Plot does work. This is because Plot uses Block to assign a value to t and in that case the value of t in x[t] and in ODE matches again. Compare:

Block[{t = 10}, x[t] /. ODE]
Module[{t = 10}, x[t] /. ODE]
| improve this answer | |
$\endgroup$
  • $\begingroup$ This clarified the problem I was having and solved the issue. $\endgroup$ – user28936 Nov 16 at 15:02
0
$\begingroup$

I am not sure this is what you want but...

ODE = DSolve[{x'[t] == 1, x[0] == 1}, x[t], t]

Manipulate[
 Plot[x[t] /. ODE // Evaluate, {t, 0, T}, 
  PlotRange -> {{0, 5}, {0, 5}}], {T, 1, 5}]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yeah, Plot works fine. But ListPlot doesn't work correctly with Manipulate. I want to see how the coordinate of x[t] moves in space over time. So my first approach was to plot the coordinate {x[t],0} along the x-axis and manipulate/animate time, which doesn't seem to work so simply. $\endgroup$ – user28936 Nov 16 at 14:46

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.