Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a user defined function which is called many times in the code. I want to ask that when I am deploying the code toCDF, I need to define that user defined function also. I tried it using DynamicModule and With. But none is working. How should I give that user defined function so that it can be deployed into CDF.

share|improve this question
Check out the Initialization option. See this QA:…. – kale Aug 30 '12 at 12:49
No,I am calling the function and appending it to get the list. – Jennifer Aug 30 '12 at 13:08

Here is a small example. I saved it to CDF and it works as is in the CDF. It has small functions in the Initialization section that are used by the Dynamics inside the DynamicModule to run the simple simulation to illustrate the idea.

Notice that this is not much different from putting all your functions in the Initialization section of the Manipulate, since Manipulate is basically a DynamicModule. So same idea.

DynamicModule[{sol, angle, bob, r, time = 0, animationRate = 1, y},

 Dynamic@Grid[{{Trigger[Dynamic[time], {0, Infinity, 0.01}, 
      AppearanceElements -> {"PlayPauseButton", "ResetButton"}],
     Style["time (sec)", 10], Dynamic[time]},
      Show[Graphics[{{Dashed, Gray, Thin, Circle[{0, 0}, 1]}, {Red, 
          Thick, Line[{{0, 0}, bob}]},
         {Blue, PointSize[0.1], Point[bob]}},
        ImagePadding -> 10], ImageSize -> 300], SpanFromLeft}
    }, Frame -> True, Alignment -> Left],

 Initialization :> (
   sol := 
    First@NDSolve[{y''[t] + 0.1 y'[t] + Sin[y[t]] == 1.5 Cos[t], 
       y[0] == Pi/4, y'[0] == 0}, y, {t, time, time + 1}, 
   bob := {Sin[(y /. sol)[time]], -Cos[(y /. sol)[time]]};

   ndsolveOptions = {MaxSteps -> Infinity,
     Method -> {"StiffnessSwitching", 
       Method -> {"ExplicitRungeKutta", Automatic}},
     AccuracyGoal -> 10, PrecisionGoal -> 10}

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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