1
$\begingroup$

How can I define a user defined function within DynamicModule? For example, if

h[x_] := x^2

I want to find h[2].

$\endgroup$
  • 2
    $\begingroup$ This question is not clear to me; can you give an extended example, please? $\endgroup$ – Mr.Wizard Oct 9 '12 at 20:18
  • $\begingroup$ @Mr.Wizard: Some what like this. DynamicModule[{h[x_] := x^2}, h[2]] so that it can be deployed intp CDF. $\endgroup$ – Jennifer Oct 9 '12 at 20:35
1
$\begingroup$

I don't have experience deploying to CDF (version 7), but I don't see anything to stop you from doing:

DynamicModule[{h},
 h[x_] := x^2;
 h[2]
]

4

If this fails perhaps you could explain in what way.

$\endgroup$
  • $\begingroup$ It works but if there are more than 1 user defined functions $\endgroup$ – Jennifer Oct 9 '12 at 20:55
  • $\begingroup$ @Jennifer then add them just like the first: DynamicModule[{h, i, j} ... `h[x_] := x^2; i[x_] := Sqrt @ x; j[x_] := Sin[x^4]; -- perhaps I still don't understand? $\endgroup$ – Mr.Wizard Oct 9 '12 at 20:59
  • $\begingroup$ It shows error for this DynamicModule[{h, i, j}, h[x_] := x^2; i[x_] := Sqrt@x; j[x_] := Sin[x^4]; h[2], i[3], j[5]] $\endgroup$ – Jennifer Oct 9 '12 at 21:03
  • 2
    $\begingroup$ @Jennifer you need to watch your syntax more closely; you accidentally are passing additional arguments to DynamicModule. Try this instead: DynamicModule[{h, i, j}, h[x_] := x^2; i[x_] := Sqrt@x; j[x_] := Sin[x^4]; {h[2], i[3], j[5]} ] $\endgroup$ – Mr.Wizard Oct 9 '12 at 21:04
  • $\begingroup$ Oh yes. Thanks. Now working $\endgroup$ – Jennifer Oct 9 '12 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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