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.

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}
]
share|improve this question
    
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). –  Yves Klett Jan 31 '13 at 15:57
    
@YvesKlett took care of it. –  rcollyer Jan 31 '13 at 15:57
1  
@rcollyer I was going for sustainable :-) –  Yves Klett Jan 31 '13 at 15:58
    
@YvesKlett I was going for quick, and a gold badge (eventually). :) –  rcollyer Jan 31 '13 at 15:58
    
@rcollyer wuzzat? Had I but known! –  Yves Klett Jan 31 '13 at 15:59
show 3 more comments

1 Answer

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}]
share|improve this answer
    
Thanks, didn't think of that. Much appreciated. –  David McHarg Jan 31 '13 at 19:31
add comment

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.