0
$\begingroup$

I have a function with a long list of parameters, for example:

n = 20;
CoeffList = Table[Symbol["a" <> ToString[i]], {i, 1, n}];
DegreeList = Table[x^i, {i, 1, n}];
TermsList = Table[CoeffList[[i]]*DegreeList[[i]], {i, 1, n}];
polinom = Sum[TermsList[[i]], {i, 1, n}]

Result of polinom:

a1 x + a2 x^2 + a3 x^3 + a4 x^4 + a5 x^5 + a6 x^6 + a7 x^7 + a8 x^8 + 
 a9 x^9 + a10 x^10 + a11 x^11 + a12 x^12 + a13 x^13 + a14 x^14 + 
 a15 x^15 + a16 x^16 + a17 x^17 + a18 x^18 + a19 x^19 + a20 x^20

Now I want to create plot of this expression inside of Manipulate module, to play with parameters and look at the changes of value of the expression.

Manipulate[
 Plot[polinom, {x, -10, 10}],
 Control[{{a1, 1, "a1"}, -100, 1000, Appearance -> "Labeled"}],
 ... ..
  Control[{{a20, 1, "a20"}, -100, 1000, Appearance -> "Labeled"}],
 ]

How I can automize the process of adding controllers in such way, that I don't need put a lot of strings of code to create controllers for them and don't need change the code if I will increase n from 20 to 50?

$\endgroup$
2
  • $\begingroup$ It seems to me it is much easier and faster to copy/paste each slider line. They are all the same, just need to change the variable name. It will take 5 minutes to do for 50 sliders. The code you'd have to write to automate this might take 5 hrs to write the debug if it works and will be complicated to automate. I do not see the point. Also having 50 sliders in your Manipulate will make it very hard to use I would think, but that is separate issue. $\endgroup$ Commented Jun 9, 2024 at 12:18
  • $\begingroup$ It is not what I need. An example that I provided is just an example - my actual model is much more complicated, variables named differently and I change value n for amount of parameters quite often. I don't want to comment and uncomment lines all the time. $\endgroup$ Commented Jun 9, 2024 at 12:22

1 Answer 1

2
$\begingroup$

How I can automize the process of adding controllers

You can generate the controllers outside Manipulate using any method such as Table command and then use the result inside Manipulate. Each time you update your polynomial, just run the code again.

enter image description here

Code

ClearAll["Global`*"]
p = a1  x + a2  x^2 + a3  x^3 + a4  x^4 + a5  x^5 + a6  x^6 + 
   a7  x^7 + a8  x^8 + a9  x^9 + a10  x^10 + a11  x^11 + a12  x^12 + 
   a13  x^13 + a14  x^14 + a15  x^15 + a16  x^16 + a17  x^17 + 
   a18  x^18 + a19  x^19 + a20  x^20;
c = Select[CoefficientList[p, x], (# =!= 0) &];
controls = {{#, 1, ToString[#]}, -100, 1000, 
     Appearance -> "Labeled"} & /@ c;
Manipulate[
 a1  x + a2  x^2 + a3  x^3 + a4  x^4 + a5  x^5 + a6  x^6 + a7  x^7 + 
  a8  x^8 + a9  x^9 + a10  x^10 + a11  x^11 + a12  x^12 + a13  x^13 + 
  a14  x^14 + a15  x^15 + a16  x^16 + a17  x^17 + a18  x^18 + 
  a19  x^19 + a20  x^20;
 Plot[p, {x, 0, 1}],
 Evaluate[Sequence @@ controls],
 LocalizeVariables -> False,
 TrackedSymbols :> {Evaluate@c}
 ]

Important:

Have to copy/paste the polynomial p into inside Manipulate for this to work.

This is needed because all the coefficients a1,a2 need to be seen inside Manipulate.

So each time you update your polynomial p, run the same code above, but make sure to copy p and paste it inside Manipulate. This is the only Manual step needed. No time now to automate this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.