1

I am new to Mathematica and reading my way through a few books. Playing around with Manipulate and I noticed the following does not work:

y[x_]:=x^n;
plot := Plot[y[x],{x,0,10}, AspectRatio->1];
Manipulate[plot,{n,1,5}]

I expected the above to work as I am using SetDelayed in my function definition so not sure if I understand why this doesn't work.

However the following does work.

Manipulate[
 Module[{y,x,plot},
  y[x_]:=x^n;
  plot := Plot[y[x],{x,0,10}, AspectRatio->1];
  Show[plot] 
 ],
 {n,1,5}
]
Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
David McHarg
  • 1,643
  • 1
  • 15
  • 28

1 Answers1

6

Your problem stems from the n in y[x_] := x^n; not being in the same scope as the n in your Manipulate and, therefore, not being the same variable.

The following modified version works as you were expecting:

plot[n_] := Plot[x^n, {x, 0, 10}, AspectRatio -> 1];
Manipulate[plot[n], {n, 1, 5}]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257