2
$\begingroup$

I developed the following manipulate code to be a limited knockoff of Polking's dfield. I export the code to Enterprise CDF so that one may type a first-order ODE into the input box and create a slope field and solutions through any point by clicking on the slope field. I have benefited from many MMA SE followers who have offered me good answers to my coding questions.

Because non-smooth functions as Abs[z], Sign[z], UnitStep[z], and Piecewise[ ....] do not display at all when typed into the entry box of the Enterprise CDF, I have found a workaround by defining, for example,

u[z_] := UnitStep[z];
abs[z_] := Abs[z];)

in the Initialization section. When writing u[t] in the entry box, I get the desired slope field and solutions.

My issue with this workaround is that upon typing u[t], for example, in the entry box and thereafter hitting the Enter key, the expression u[t] is replaced by UnitStep[t]. I find this undesirable both aesthetically and functionally. So I ask, is there a way to insure that the original expression u[t] remains in the entry box after entering it? One does not need to have an Enterprise version of MMA to test this as the issue is apparent from the nb code itself. Here is the code.

Manipulate[
 ClickPane[
  Show[
   Plot[g, {t, tmin, tmax}, PlotRange -> {{tmin, tmax}, {xmin, xmax}},
   PlotStyle -> Directive[Black], Frame -> True, ImageSize -> 500, 
   Axes -> None, AspectRatio -> 1, 
   PlotLabel -> Dynamic[MousePosition["Graphics"]]],
   sf[dx[diffEq, A, B]],
   Graphics[{PointSize[Large], Point[sp]}]],

  (AppendTo[g, sol[dx[diffEq, A, B], #]];
   AppendTo[sp, #]) &],

 Style["Enter f(t,x)"],
 {{diffEq, x^2 + a t + b, "dx/dt = "}, ImageSize -> 165}, 

 {{A, 1, "a"}, -4, 4, 0.01, Appearance -> "Labeled", 
   ControlPlacement -> Bottom}, {{B, -1, "b"}, -4, 4, 0.01, 
   Appearance -> "Labeled", ControlPlacement -> Bottom}, {{g, {}}, 
   ControlType -> None},
 {{sp, {}}, ControlType -> None},

 Row[{Control[{{tmin, -2, "tMin = "}, ImageSize -> 40}], Spacer[40], 
    Control[{{tmax, 2, "tMax = "}, ImageSize -> 40}]}], 
 Row[{Control[{{xmin, -2, "xMin = "}, ImageSize -> 40}], Spacer[40], 
    Control[{{xmax, 2, "xMax = "}, ImageSize -> 40}]}],

Button["clear", g = {}; sp = {}, ImageSize -> {Automatic, 20}],

Initialization :> (

   Clear[sol, sf, dx];
   g = {};
   sp = {};
   dx[de_, a0_, b0_] := diffEq /. {a -> a0, b -> b0};
   sf[dx_] := 
   VectorPlot[{1, dx}, {t, tmin, tmax}, {x, xmin, xmax}, 
      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 Max[{Abs[xmin], Abs[xmax]}], 
      "StopIntegration"]}, y, {t, tmin, tmax}, 
      Method -> "StiffnessSwitching", 
      "ExtrapolationHandler" -> {Indeterminate &, 
      "WarningMessage" -> False}];
   u[z_] := UnitStep[z];
   abs[z_] := Abs[z];)
   ]

I have reached out to MMA SE and WRI to resolve the inability of Enterprise CDF to accept non-smooth functions as inputs. The former offered some suggestions , none of which were particularly helpful.and a support engineer at WRI informed me that my issue was forwarded to "the developers."

Can any someone in the MMA SE community explain how to code the outcome I want? I appreciate all the advice and solutions I have received from the Mathematica mavens at MMA SE for various parts of my dfield knockoff.

$\endgroup$
2
$\begingroup$

Yes, i tried to run your code in Player Pro preview mode and it is true that these Abs, UnitStep are not accepted in the input menu ... ?

Your workaround works but if you don't want the aliases to be replaced with their real function name here is another workaround:

Instead of defining u[z]:= and abs[z]:=... just do

dx[de_, a0_, b0_] := de /. {a -> a0, b -> b0, u -> UnitStep, abs -> Abs}

It works for me.

$\endgroup$
  • $\begingroup$ @SqareOne What a simple solution. You've made my day $\endgroup$ – Stephen Oct 7 '15 at 2:41

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.