2
$\begingroup$

All, the following code graphs 10 different solutions for a first order ODE and allows the user to modify certain equation parameters to see how the graph is affected:

Manipulate[
(*Find 10 different solutions for the ODE*)
eqn = {a y''[t] == b y[t] + d, y'[0] == c};
sol = DSolve[eqn, {y[t]}, {t}];
func[t_] = y[t] /. sol[[1]];
t[x_] = Table[func[t] /. C[1] -> j, {j, 1, 10}];

(*Display graph*)
Plot[Evaluate[t[x]], {t, -tmax, tmax}], 

(*Variables to parameterize*)
{a, 1, 5, 1}, {b, -1, 1, 1}, {c, -10, 10, 1}, 
{d, 0, 100, 10}, {tmax, 1, 10, 1},

(*Aesthetic labeling stuff*)
Delimiter,
Dynamic[Style[Row[{"Input: ", a , " y''(t) = ", 
b, " y(t) + ", d, ", y'(0) = ", c}], 12]],
Delimiter,
Dynamic[Style[Row[{"Output: ", func[t]}], 12]],

(*Misc. Manipulate stuff*)
ContinuousAction -> True, SaveDefinitions -> True, 
TrackedSymbols :> {a, b, c, tmax, d}]

The problem that arises is the equation that is displayed after the "Output:" label. If you are patient enough, and move around the sliders (I have been varying parameter d), you will occasionally see the equation display as something like:

enter image description here

When it should be:

enter image description here

There are also some funky little things I have noticed like the slider "dragger" in picture 1 is still highlighted even after I release it.

Are there any glaring things that I am doing incorrectly that could be causing these symptoms? Is there anyone else that can reproduce the problem?

One other thing - I cannot make this problem happen if I use the stepper controls instead of using my mouse to change the value of the slider.

$\endgroup$
5
$\begingroup$

The use of t as an iterator variable inside the Plot command is, I think, leaking out into the Dynamic containing your "Output" line on occasion. In general, your overuse of the variable t is confusing at a minimum, and potentially error-prone. I made two changes to your code. First, I changed t[x_] to funcs[x_], which shouldn't affect functionality, but does make it a lot easier to figure out what's going on. Second, I replaced the iterator variable used by Plot. Here's the code:

Manipulate[
 (*Find 10 different solutions for the ODE*)
 eqn = {a y''[t] == b y[t] + d, y'[0] == c};
 sol = DSolve[eqn, {y[t]}, {t}];
 func[t_] = y[t] /. sol[[1]];
 funcs[x_] = Table[func[t] /. C[1] -> j, {j, 1, 10}];

 (*Display graph*)
 Plot[Evaluate[funcs[x] /. t -> val], {val, -tmax, tmax}],

 (*Variables to parameterize*)
 {a, 1, 5, 1}, {b, -1, 1, 1}, {c, -10, 10, 1},
 {d, 0, 100, 10}, {tmax, 1, 10, 1},

 (*Aesthetic labeling stuff*)
 Delimiter, 
 Dynamic[Style[
   Row[{"Input: ", a, " y''(t) = ", b, " y(t) + ", d, ", y'(0) = ", 
     c}], 12]], Delimiter, 
 Dynamic[Style[Row[{"Output: ", func[t]}], 12]],

 (*Misc. Manipulate stuff*)
 ContinuousAction -> True, SaveDefinitions -> True,
 TrackedSymbols :> {a, b, c, tmax, d}]

I couldn't reproduce your problem, so I can't guarantee this fixes it, but I think it is likely to.

$\endgroup$
  • $\begingroup$ What can I say, "t" is my favorite character in the alphabet! In all seriousness, with the changes you proposed, the problem is no longer evident. Thanks! $\endgroup$ – tjm167us Aug 28 '13 at 18:33
  • $\begingroup$ Any idea why the slider "dragger" stays highlighted when it should no longer be (see picture 1 of the original post)? This occurs if you click on the slider away from the dragger, and it remains highlighted until you grab the dragger and move it again... $\endgroup$ – tjm167us Aug 28 '13 at 18:41
  • $\begingroup$ I believe you did not properly localize x, y, and t, and further that you should have done so as your code will be considered an authoritative example. $\endgroup$ – Mr.Wizard Aug 28 '13 at 20:17
  • $\begingroup$ @Mr.Wizard Localizing t would require throwing multiple Blocks into the code to preserve the output form (which would not be preserved by DynamicModule, etc.). I agree that this would be the proper thing to do, but I didn't want to complexify the answer by focusing on points the author didn't ask. Nonetheless, I will point out for posterity that Mr.Wizard is right in saying this is not the code as I would have written it. $\endgroup$ – John Fultz Aug 31 '13 at 16:24
  • $\begingroup$ @John lol -- okay, good enough :-) $\endgroup$ – Mr.Wizard Sep 1 '13 at 1:50

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.