Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
1  
Check out the Initialization option. See this QA: mathematica.stackexchange.com/questions/9476/…. –  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
add comment

1 Answer 1

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}, 
      animationRate,
      AppearanceElements -> {"PlayPauseButton", "ResetButton"}],
     Style["time (sec)", 10], Dynamic[time]},
    {Dynamic@
      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}, 
      Sequence@ndsolveOptions];
   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
add comment

Your Answer

 
discard

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.