Message Boards Message Boards

CDF Player and global function definitions

I am trying to embed interactive graphics in a CDF file

Manipulate[Plot[q*Sin[t] + (1 - q)*Sin[2*t], {t, 0, 2*Pi}], {q, 0, 1}]

works

ff[q_][t_] = q*Sin[t] + (1 - q)*Sin[2*t]
Manipulate[Plot[ff[q][t], {t, 0, 2*Pi}], {q, 0, 1}]

does not work

Module[{}, fff[q_][t_] = q*Sin[t] + (1 - q)*Sin[2*t];
   Manipulate[Plot[fff[q][t], {t, 0, 2*Pi}], {q, 0, 1}]]

does not work either

Is there a way to use function definitions for parametrized functions and then plot such functions with a slider for the parameter in CDF files?

POSTED BY: Erich Neuwirth

There are two directions that you can go. If you wish to define your function ff outside of the Manipulate then you should use the option

SaveDefinitions->True

as in,

ff[q_, t_] := q*Sin[t] + (1 - q)*Sin[2*t]
Manipulate[Plot[ff[q, t], {t, 0, 2*Pi}], {q, 0, 1}, 
 SaveDefinitions -> True]

Note that I slightly changed your expression for ff to (a) have both variables with in a single argument structure (you could go either way) and (b) to make its definition a delayed evaluation (:=) rather than an immediate evaluation (=).

The alternative approach is to include the definition of ff in the Initialization option to Manipulate as in:

Manipulate[Plot[ff[q, t], {t, 0, 2*Pi}], {q, 0, 1}, 
 Initialization :> {ff[q_, t_] := q*Sin[t] + (1 - q)*Sin[2*t]}]

Note that the Initialization option is expressed as a delayed rule.

POSTED BY: David Reiss
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