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.

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.

share|improve this question
    
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 at 22:51
    
Thank you very much! –  Noah Jul 17 at 22:56
1  
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 at 23:10
    
It works. Thanks a lot. –  Noah Jul 17 at 23:39

1 Answer 1

up vote 0 down vote accepted
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}]
share|improve this answer

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.