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 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?

share|improve this question
    
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 –  Rojo May 18 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 at 5:01

2 Answers 2

up vote 3 down vote accepted

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"}]
 ]
share|improve this answer
1  
very helpful answer. thanks a lot –  Algohi May 20 at 1:20
1  
@rojo -great piece of code –  eldo May 21 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.

share|improve this answer
    
thanks but i want each coefficient to be variable not fixed numbers. –  Algohi May 19 at 5:03

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.