1

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.

Stephen
  • 1,156
  • 5
  • 12

1 Answers1

2

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}];)]
Karsten7
  • 27,448
  • 5
  • 73
  • 134