1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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[1], coef[2], etc $\endgroup$ – Rojo May 18 '14 at 13:29
  • $\begingroup$ @Rojo i tried some of your ideas but i could not make it. can you write down your suggestions? $\endgroup$ – Algohi May 19 '14 at 5:01
3
$\begingroup$

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"}]
 ]
$\endgroup$
  • 1
    $\begingroup$ very helpful answer. thanks a lot $\endgroup$ – Algohi May 20 '14 at 1:20
  • 1
    $\begingroup$ @rojo -great piece of code $\endgroup$ – eldo May 21 '14 at 1:03
1
$\begingroup$

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.

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

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.