# Manipulate which creates and deletes Manipulators dynamically

I want to plot a function in a Manipulate in which the number of terms varies and a Manipulator for the coefficient of each term is created or deleted as the terms increase and decrease.

For example, when I have three terms, I should get the equivalent of

Manipulate[
Plot[a0 + a1 x + a2 x^2, {x, 0, 10}],
{a0, 1, 10},
{a1, 1, 10},
{a2, 1, 10}]


I want to let the number of terms vary and but still control each coefficient with a Manipulator. I tried to do it in the following way:

Manipulate[
Plot[Total[ToExpression["a" <> ToString[#]] x^# & /@ Range[0, n]], {x, 0, 10}],
{n, 1, 10, 1},
Dynamic[Sequence @@ ({ToExpression["a" <> ToString[#]], 1, 10} & /@ Range[1, n])]]


It did not work.

Can anybody suggest what I should do to make it work?

• You can use a control for the number of terms and then a dynamic list of controls for each of them. But, instead of using a symbol for each, just use coef, coef, etc – Rojo May 18 '14 at 13:29
• @Rojo i tried some of your ideas but i could not make it. can you write down your suggestions? – Algohi May 19 '14 at 5:01

Here's an idea

DynamicModule[{coef},
_coef = 0;
Manipulate[
Column[{
Grid@Partition[#, 2, 2, 1, {}] &[
Array[
Manipulator[Dynamic[coef[#]], {-4, 4},
Appearance -> "Labeled"] &,
order + 1, 0]
] // Dynamic,
Dynamic@Plot[
Sum[coef[i] \[FormalX]^i, {i, 0, order}], {\[FormalX], 0, 10},
ImageSize -> Medium, PlotRange -> {-ran, ran}]
}],
{{order, 1, "Order"}, 0, 10, 1, Appearance -> "Labeled"},
{{ran, 10, "Plot Range"}, 0.1, 100, Appearance -> "Labeled"}]
]

• very helpful answer. thanks a lot – Algohi May 20 '14 at 1:20
• @rojo -great piece of code – eldo May 21 '14 at 1:03

Consider this:

    Manipulate[
foo = Total@Map[#*x^# &, Range[0, n]];
Plot[foo, {x, 0, 3}],
Grid[
{{
Slider[Dynamic@n, {0, 10, 1}],
Dynamic[foo, {n, 1, 10, 1}]
}}
],
TrackedSymbols -> n]


Could be further improved, this was only a quick shot.

• thanks but i want each coefficient to be variable not fixed numbers. – Algohi May 19 '14 at 5:03