# How to use a self-defined function in the Manipulate function

This is what I want

Manipulate[ Plot[ Which[h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0], {h, 0, 8}],
{a, 5, 10}]


Is it possible to rewrite it as something like the following?

f[h_] := Which[ h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0];
Manipulate[ Plot[ f[h], {h, 0, 8}], {a, 5, 10}]


Seems it doesn't work for me. Any suggestion is appreciated.

• Notice that a has a different colour inside and out of the manipulate. one a is global and another is local. Manipulate[Dynamic@Plot[f[h], {h, 0, 8}], {a, 5, 10}, Initialization :> {f[h_] := Which[h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0]}] ...or alternatively make f a function of both h and a and then u don't need the Dynamic – Mike Honeychurch Jul 17 '14 at 22:51
• I realized what you were trying to do. This was my solution f[h_, a_] := Which[h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0]; Manipulate[Plot[f[h, a], {h, 0, 8}], {a, 5, 10}] but I see that @MikeHoneychurch has already mentioned it. – seismatica Jul 17 '14 at 23:10
• It works. Thanks a lot. – Noah Jul 17 '14 at 23:39

## 1 Answer

f[h_] := Which[h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0];
With[{g = f[h]},
Manipulate[Plot[g, {h, 0, 8}], {a, 5, 10}]]


also

f[h_] := Which[h < a, 1 - 1.5 h/a + 0.5 (h/a)^3, h >= a, 0];
Manipulate[Plot[f[h] /. a -> b, {h, 0, 8}], {b, 5, 10}]