Empty plot in nested manipulate?

Hello,

I’m trying to use nested “Manipulate” (as explained in the “Advanced Manipulate Functionnality” page of the Mathematica tutorial) to plot a polynomial of arbitrary order n. The corresponding function appears to be well-defined and controllable inside the “Manipulate” environment, but I can’t find a way to plot it!

f[a_, n_, x_] := Sum[a[i] x^i, {i, 1, n}]
Manipulate[
 With[{value = {f[a, n, x], Plot[f[a, n, x], {x, -3, 3}]},
   controls = Sequence @@ Table[{a[i], 0, 1}, {i, 1, n}]},
   Manipulate[value, controls]], {n, 1, 3, 1}]

Is there any obvious reason for this code not to work?
Thanks for your ideas!

Regards

Your code doesn’t work because your local variables aren’t lining up as you expect. Also, I don’t think Manipulate likes it when the control variables depend on other control variables. When you have such dependencies, you can workaround this by making your own controls explicitly (look at all of the forms of controls at Control Objects—Wolfram Documentation).

For your case, you might be able to avoid the nested Manipulate by doing something like this:

Manipulate[
 Grid[
  {{Grid[{HoldForm[a[#]], Slider[Dynamic[a[#]], {0, 1}]} & /@ 
      Range[n]]},
   {f[a, n, x]}}],
 {n, 1, 3, 1}]

This isn’t polished. There are some weird effects trying to interact with the sliders, and there’s no initialization for each a[n], but maybe this will give you a path to follow.

This seems to work:

f[a_, n_, x_] := Sum[a[i] x^i, {i, 1, n}];
innerManipulate[n_Integer] := 
  With[{cntrls = Table[{{a[i], 0}, 0, 1}, {i, 1, n}]},
   Manipulate @@ {{f[a, n, x], 
      Graphics[Line@Table[{x, f[a, n, x]}, {x, -3., 3., .1}], 
       Axes -> True]}, Sequence @@ cntrls}];
Manipulate[innerManipulate[n], {n, 1, 5, 1}]

but I wonder why I have to resort to Graphics and Line instead of Plot.

Hello Eric, hello Gianluca,

Thanks a lot for your help - I realize I’m not really comfortable with Mathematica advanced syntax!
I was able to run your codes and to adapt yours Gianluca to my actual problem: manipulating Fourier series.

f[a_, b_, n_, x_] := Sum[a[i] Cos[i x] + b[i] Sin[i x], {i, 1, n}]
innerManipulate[n_Integer] := 
  With[{cntrls = 
     Flatten[Table[{{{a[i], 0}, -1, 1}, {{b[i], 0}, -1, 1}}, {i, 1, 
        n}], 1]}, 
   Manipulate @@ {Graphics[
      Line@Table[{x, f[a, b, n, x]}, {x, -2 \[Pi], 
         2 \[Pi], .1}], Axes -> True], Sequence @@ cntrls}];
Manipulate[innerManipulate[n], {n, 1, 5, 1}]

Eric I don’t know why I wasn’t able to adapt your code, it looks like something gets wrong sometimes with initialization…

I’ll now try to use Slider2D for Fourier coefficients so that it looks nicer - but I feel like it’s going to be more than just a quick cosmetic change :smiley:

Thank you again!

Here is an attempt with a[i] a vector instead of a scalar:

f[a_, n_, x_] := Sum[a[i] . { Cos[i x], Sin[i x]}, {i, 1, n}]
innerManipulate[n_Integer] := 
  Manipulate @@ {Graphics[
     Line@Table[{x, f[a, n, x]}, {x, -2 \[Pi], 2 \[Pi], .1}], 
     Axes -> True], 
    Sequence @@ 
     Table[{{a[i], {0, 0}}, {-1, -1}, {1, 1}}, {i, 1, n}]};
Manipulate[innerManipulate[n], {n, 1, 5, 1, SetterBar}]

Thank you so much Gianluca, that is exactly what I was looking for / struggling with :slight_smile: