0
$\begingroup$

The following code prints the the value of the solution $x(t)$ to an ODE at a user defined time $t_f$. I want the value (0.350232) to appear within the Panel and not below the Panel as the following image. I want to format the Button/Print option to display $x_f=0$.350232 within the Panel. Thanks for your help.

enter image description here

Panel@DynamicModule[{sol, t0 = 0, x0 = 0, xf = 1},
sol = NDSolve[{y'[t] == t^2 + y[t]^2, y[t0] == x0}, y, {t, t0, tf}, 
Method -> "ExplicitRungeKutta"];
x[t_] = y[t] /. First[sol];
Column[{
Row[{Spacer[150], Text["Title"]}],
Row[{
  Control[{{t0, Null, Style[Subscript[t, 0]]}, ImageSize -> 60}], 
  Spacer[10],
  Control[{{x0, Null, Style[Subscript[x, 0]]}, ImageSize -> 60}], 
  Spacer[10], 
  Control[{{tf, Null, Style[Subscript[t, f]]}, ImageSize -> 60}], 
  Spacer[40],
  Button[Style["Evaluate \!\(\*SubscriptBox[\(x\), \(f\)]\)"], 
   Print@Evaluate[x[tf] /. sol]]}]
  }],
  SaveDefinitions -> True]
$\endgroup$

1 Answer 1

4
$\begingroup$

You might try something like this. The approach is somewhat different than yours, but I think it gives both cleaner code and a GUI that is easier to use.

Clear[y]
Panel @
  DynamicModule[{yF, t0 = 0, y0 = 0, tf = 1},
    Dynamic @ 
      Column[
        {Row[{Spacer[150], Text["Title"]}],
         Row[
           {Control[{{t0, t0, Style[Subscript[t, 0]]}, ImageSize -> 60}], 
            Spacer[10],
            Control[{{y0, y0, Style[Subscript[y, 0]]}, ImageSize -> 60}], 
            Spacer[10],
            Control[{{tf, tf, Style[Subscript[t, f]]}, ImageSize -> 60}]],
         Panel[
           yF =
             NDSolveValue[{y'[t] == t^2 + y[t]^2, y[t0] == y0}, y, {t, t0, tf},
               Method -> "ExplicitRungeKutta"];
           Column[
             {Plot[yF[t], {t, t0, tf}, ImageSize -> Medium], 
              yF[tf]}],
             Background -> White]}]]

panel

Of course, if you are determined to use Print, the same approach will still work.

Clear[y]
Panel @
  DynamicModule[{yF, t0 = 0, y0 = 0, tf = 1},
    Dynamic @
      Column[
        {Row[{Spacer[150], Text["Title"]}],
         Row[
          {Control[{{t0, t0, Style[Subscript[t, 0]]}, ImageSize -> 60}], 
           Spacer[10],
           Control[{{y0, y0, Style[Subscript[y, 0]]}, ImageSize -> 60}], 
           Spacer[10],
           Control[{{tf, tf, Style[Subscript[t, f]]}, ImageSize -> 60}], 
           Spacer[40],
           Button["Evaluate ",
             Print[
               yF =
                 NDSolveValue[{y'[t] == t^2 + y[t]^2, y[t0] == y0}, y, {t, t0, tf},
                   Method -> "ExplicitRungeKutta"];
               Column[
                 {Plot[yF[t], {t, t0, tf}, ImageSize -> Medium],
                  yF[tf]},
                 Right]],
             Method -> "Queued"]}]}]]

print

$\endgroup$
2
  • $\begingroup$ Thanks for the suggestion. I want a simpler format.and don't need the plot. - just an output like I stated in my question: x_f=value. $\endgroup$
    – Stephen
    Jun 24, 2018 at 19:09
  • $\begingroup$ @Stephen. So simplify one of these two examples so it only puts out yF[tf]. That shouldn't be hard. $\endgroup$
    – m_goldberg
    Jun 25, 2018 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.