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}

]

=================

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

=================

1 Answer

1

=================

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}]

Thanks, didn’t think of that. Much appreciated.

– David McHarg

Jan 31 ’13 at 19:31