1
$\begingroup$

By clicking at a point on the slope field for an ode, a solution is plotted through that point. Additional clicks produce more plots; the plots are saved in graph and the points in pt. The following code is a slimmed down version.

Manipulate[
 ClickPane[
  Show[
   Plot[graph, {t, -2, 2}, PlotRange -> {{-2, 2}, {-2, 2}}],
   sf@dx[ode],
   Graphics[{PointSize[Large], Point[pt]}]],

  (AppendTo[graph, sol[dx[ode], #]];
   AppendTo[pt, #]) &],

 Style["Enter f(t,x)"], {{ode, x^2 - t, "dx/dt = "}},

 (*
 TrackingFunction\[Rule](pt=#;{t0,x0}=pt;&);
 {{t0,0},InputField,TrackingFunction\[Rule](t0=#;pt[[1]]=t0;&)},
 {{x0,0},InputField,TrackingFunction\[Rule](x0=#;pt[[2]]=x0;&)},
 *)

 Button["Delete all solutions", graph = {}; pt = {}, 
 ImageSize -> {Automatic, 20}],

 Initialization :> (
   graph = {}; pt = {};

   dx[de_] := ode;
   sf[ode_] := 
    VectorPlot[{1, ode}, {t, -2, 2}, {x, -2, 2}, VectorPoints -> 17, 
    VectorScale -> {0.03, Automatic, None}, 
    VectorStyle -> {{Red, Arrowheads[0]}}, ImagePadding -> 1, 
    PerformanceGoal -> "Speed"];

   sol[dx_, {t0_, x0_}] := 
   y[t] /. First@
   NDSolve[{y'[t] == dx /. {x -> y[t]}, y[t0] == x0, 
   WhenEvent[Abs[y[t]] > 2.5 , "StopIntegration"]}, y, {t, -2, 2},
   "ExtrapolationHandler" -> {Indeterminate &, 
     "WarningMessage" -> False}];)
 ]

I want to accomplish the same by InputField of the initial point as well. All these plots are to be appended in graph and the the inputted initial point {t0,x0} appended to pt. In particular, graph and pt may contain plots and initial points from either ClickPane entries or InputField entries. I thought that TrackingFunction (see Karsten 7) might make this possible; if it does, I haven't coded it properly. (See the commented lines in the code.) Is this possible or is there another way? I realize that my implementation of the commented code is incorrect.

$\endgroup$
2
$\begingroup$

A quick fix of your code:

Manipulate[
 ClickPane[
  Show[Plot[graph, {t, -2, 2}, PlotRange -> {{-2, 2}, {-2, 2}}], sf@dx[ode], 
   Graphics[{PointSize[Large], Point[pt]}]], 
  (AppendTo[graph, sol[dx[ode], #]]; AppendTo[pt, #]; t0x0 = #) &], 
 Style["Enter f(t,x)"], {{ode, x^2 - t, "dx/dt = "}}, 
 {{t0x0, {0, 0}, "{t0, x0}"}, InputField, 
  TrackingFunction -> (t0x0 = #; AppendTo[graph, sol[dx[ode], #]]; AppendTo[pt, #]; &)}, 
 Button["Delete all solutions", graph = {}; pt = {}, ImageSize -> {Automatic, 20}], 
 Initialization :> (graph = {}; pt = {};
   dx[de_] := ode;
   sf[ode_] := 
    VectorPlot[{1, ode}, {t, -2, 2}, {x, -2, 2}, VectorPoints -> 17, 
     VectorScale -> {0.03, Automatic, None}, 
     VectorStyle -> {{Red, Arrowheads[0]}}, ImagePadding -> 1, 
     PerformanceGoal -> "Speed"];
   sol[dx_, {t0_, x0_}] := 
    y[t] /. First@
      NDSolve[{y'[t] == dx /. {x -> y[t]}, y[t0] == x0, 
        WhenEvent[Abs[y[t]] > 2.5, "StopIntegration"]}, y, {t, -2, 2},
        "ExtrapolationHandler" -> {Indeterminate &, 
         "WarningMessage" -> False}];)]

Update based on the comments:

Manipulate[
 ClickPane[
  Show[Plot[graph, {t, -2, 2}, PlotRange -> {{-2, 2}, {-2, 2}}], 
   sf@dx[ode], 
   Graphics[{PointSize[Large], Point[pt]}]], (AppendTo[graph, 
     sol[dx[ode], #]]; AppendTo[pt, #]; t0x0 = #; {t0, x0} = #) &],
 Style["Enter f(t,x)"],
 {{ode, x^2 - t, "dx/dt = "}, InputField},
 {{t0, 0}, InputField, 
  TrackingFunction -> (t0 = #; AppendTo[graph, sol[dx[ode], {#, x0}]];
     AppendTo[pt, {#, x0}]; t0x0 = {#, x0}; &)},
 {{x0, 0}, InputField, 
  TrackingFunction -> (x0 = #; AppendTo[graph, sol[dx[ode], {t0, #}]];
     AppendTo[pt, {t0, #}]; t0x0 = {t0, #}; &)},
 {{t0x0, {0, 0}, "{t0, x0}"}, InputField, 
  TrackingFunction -> (t0x0 = #; AppendTo[graph, sol[dx[ode], #]];
     AppendTo[pt, #]; {t0, x0} = #; &)}, 
 Button["Delete all solutions", graph = {}; pt = {}, 
  ImageSize -> {Automatic, 20}], 
 Initialization :> (graph = {}; pt = {};
   dx[de_] := ode;
   sf[ode_] := 
    VectorPlot[{1, ode}, {t, -2, 2}, {x, -2, 2}, VectorPoints -> 17, 
     VectorScale -> {0.03, Automatic, None}, 
     VectorStyle -> {{Red, Arrowheads[0]}}, ImagePadding -> 1, 
     PerformanceGoal -> "Speed"];
   sol[dx_, {t0_, x0_}] := 
    y[t] /. First@
      NDSolve[{y'[t] == dx /. {x -> y[t]}, y[t0] == x0, 
        WhenEvent[Abs[y[t]] > 2.5, "StopIntegration"]}, y, {t, -2, 2},
        "ExtrapolationHandler" -> {Indeterminate &, 
         "WarningMessage" -> False}];)]

Or

Manipulate[
 ClickPane[
  Show[Plot[graph, {t, -2, 2}, PlotRange -> {{-2, 2}, {-2, 2}}], 
   sf@dx[ode], 
   Graphics[{PointSize[Large], Point[pt]}]], (AppendTo[graph, 
     sol[dx[ode], #]]; AppendTo[pt, #]; {t0, x0} = #) &],
 Style["Enter f(t,x)"],
 {{ode, x^2 - t, "dx/dt = "}, InputField},
 {{t0, 0}, InputField, 
  TrackingFunction -> (t0 = #; AppendTo[graph, sol[dx[ode], {#, x0}]];
     AppendTo[pt, {#, x0}]; &)},
 {{x0, 0}, InputField, 
  TrackingFunction -> (x0 = #; AppendTo[graph, sol[dx[ode], {t0, #}]];
     AppendTo[pt, {t0, #}]; &)}, 
 Button["Delete all solutions", graph = {}; pt = {}, 
  ImageSize -> {Automatic, 20}], 
 Initialization :> (graph = {}; pt = {};
   dx[de_] := ode;
   sf[ode_] := 
    VectorPlot[{1, ode}, {t, -2, 2}, {x, -2, 2}, VectorPoints -> 17, 
     VectorScale -> {0.03, Automatic, None}, 
     VectorStyle -> {{Red, Arrowheads[0]}}, ImagePadding -> 1, 
     PerformanceGoal -> "Speed"];
   sol[dx_, {t0_, x0_}] := 
    y[t] /. 
     First@NDSolve[{y'[t] == dx /. {x -> y[t]}, y[t0] == x0, 
        WhenEvent[Abs[y[t]] > 2.5, "StopIntegration"]}, y, {t, -2, 2},
        "ExtrapolationHandler" -> {Indeterminate &, 
         "WarningMessage" -> False}];)]
$\endgroup$
  • $\begingroup$ 7; Thank you for the fix - so elegant! I have tried to modify your code to provide separate InputFields for t0 and x0 but have not been successful. In fact, I really don't understand how TrackingFunction works. Can you help me? $\endgroup$ – Stephen Nov 26 '15 at 13:27
  • $\begingroup$ @Stephen Did you check the documentation (including the Details section)? TrackingFunction works like the second argument of Dynamic. $\endgroup$ – Karsten 7. Nov 26 '15 at 13:36
  • $\begingroup$ 7: A new pt with the new t0 $\endgroup$ – Stephen Nov 26 '15 at 13:42
  • $\begingroup$ 7: If only t0 is changed, then x0 remains the same. $\endgroup$ – Stephen Nov 26 '15 at 13:49
  • $\begingroup$ Let us continue this discussion in chat. $\endgroup$ – Stephen Nov 26 '15 at 13:52

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.