2

I have this code:

Clear[f]
f = Function[x, x^k/(1 + x^k)];
Manipulate[Plot[f[x], {x, 0, 5}], {k, 1, 10}]

But nothing draws as I move the slider.

Am I missing something?

VividD
  • 3,660
  • 4
  • 26
  • 42
David
  • 14,883
  • 4
  • 44
  • 117

3 Answers3

5

Manipulate has the attribute HoldAll and therefore f is not evaluated to your function. Therefore the expression with k is not visible for Manipulate.

Clear[f]
f = Function[x, x^k/(1 + x^k)];
With[{f = f},
 Manipulate[Plot[f[x], {x, 0, 5}], {k, 1, 10}]
]

Mathematica graphics

If you want to know, why With is one possible solution, you could check the following thread:

What are the use cases for different scoping constructs?

halirutan
  • 112,764
  • 7
  • 263
  • 474
4

Try

Clear[f]
f = Function[{x, k}, x^k/(1 + x^k)];
Manipulate[Plot[f[x, k], {x, 0, 5}], {k, 1, 10}]

To see what is happening, do

Clear[f]
f = Function[{x, k}, x^k/(1 + x^k)];
Manipulate[f[x, k], {k, 1, 10}]

and

Clear[g]
g = Function[x, x^k/(1 + x^k)];
Manipulate[g[x], {k, 1, 10}]
acl
  • 19,834
  • 3
  • 66
  • 91
1

Another option is to use Replace within the Manipulate

Clear[f]
f = Function[x, x^k/(1 + x^k)];
Manipulate[Plot[f[x] /. k -> k1, {x, 0, 5}], {k1, 1, 10}]
bobthechemist
  • 19,693
  • 4
  • 52
  • 138