Message Boards Message Boards

Embed NDSolveValue function in Manipulate for CDF?

Posted 6 years ago

Hello, I'm usually writing Notebooks and sharing them with students. Our new students won't have Mathematica licenses anymore, so that I'm starting coding my notebooks in CDF compatible format. But I'm facing some troubles. For example, I'm unable to make this code working for CDF:

ClearAll["Global`*"];
tmax = 2 \[Pi];
{xsol, ysol} = 
  NDSolveValue[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, 
    y[0] == 0}, {x, y}, {t, 0, tmax}];

Manipulate[ 
 Graphics[Point[{xsol[t], ysol[t]}], PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}],
 {t, 0, tmax}
  ]

Could someone give me a hint how to solve this? Thanks!

2 Replies

Thank you very much. I think I still have things to learn with Mathematica. I tried to solve a more complicated code in the way you showed me, and my expressions are too poorly defined...

When you deploy to CDF, cells are not evaluated, only dynamic objects like the output of Manipulate. When that Manipulate output is evaluated by the CDF Player the variables xsol, ysol and tmax are undefined so it doesn't work. So you have to make everything self contained by hand:

With[{tmax = 2 \[Pi]}, 
 With[{solutions = NDSolveValue[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x, y}, {t, 0, tmax}]},
  Manipulate[
   Graphics[Point[{xsol[t], ysol[t]}], PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}],
   {t, 0, tmax},
   Initialization :> ({xsol, ysol} = solutions)]]]

Or just add a SaveDefinitions option to your Manipulate which is much easier (and dangerous).

POSTED BY: Gustavo Delfino
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract