2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Mike Honeychurch Jul 17 '14 at 22:51
  • 1
    $\begingroup$ 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. $\endgroup$ – seismatica Jul 17 '14 at 23:10
  • $\begingroup$ It works. Thanks a lot. $\endgroup$ – Noah Jul 17 '14 at 23:39
1
$\begingroup$
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}]
| improve this answer | |
$\endgroup$

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.