1
$\begingroup$

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}
]
$\endgroup$
  • $\begingroup$ Please have a look at the markdown page and format your question to allow for easy reading (Indenting code segments by four spaces already helps a lot). $\endgroup$ – Yves Klett Jan 31 '13 at 15:57
  • $\begingroup$ @YvesKlett took care of it. $\endgroup$ – rcollyer Jan 31 '13 at 15:57
  • 1
    $\begingroup$ @rcollyer I was going for sustainable :-) $\endgroup$ – Yves Klett Jan 31 '13 at 15:58
  • $\begingroup$ @YvesKlett I was going for quick, and a gold badge (eventually). :) $\endgroup$ – rcollyer Jan 31 '13 at 15:58
  • $\begingroup$ @rcollyer wuzzat? Had I but known! $\endgroup$ – Yves Klett Jan 31 '13 at 15:59
6
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ Thanks, didn't think of that. Much appreciated. $\endgroup$ – David McHarg Jan 31 '13 at 19:31

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.