2
$\begingroup$

I would like to write a procedure with the signature:

Foo[vars, params]

which produces a Manipulate object. The procedure will dress up the vars into sliders of the form:

{{a, a0, "DisplayName"}, min, max, Appearance->"Labeled"}

and will produce a Plot, depending on both the params and vars. (The default value a0 and the other slider properties are generated in a particular manner from a.)

The issue I'm running into is that Manipulate requires all of its vars to be explicitly listed. I looked at the related question Proper way to handle free variables in manipulate/plot? to no avail (I couldn't get With to do what I want).

So how does one procedurally generate the commands of Manipulate? I am constrained to use Manipulate as opposed to Dynamic or other more advanced features.


Concrete Example:

Manipulate[
    Plot[
        c0 + c1 x + c2 x^2,
        {x,0,1}
    ],
    {{c0, 0},-1, 1},
    {{c1, 1}, 0, 2},
    {{c2, 2}, 1, 3},
]

Then the goal would be to replace the 2 with an n, i.e. to write a function Poly[n] which returns a manipulatable polynomial with coefficients c_i centered around i.

$\endgroup$
4
  • $\begingroup$ "The default value a0 and the other slider properties are generated in a particular manner from a." This is quite vague. Without some idea of what you need it is hard to provide a recommendation. $\endgroup$
    – Mr.Wizard
    Commented Jan 30, 2015 at 7:04
  • $\begingroup$ Okay I'll write up a more concrete mwe. $\endgroup$
    – pre-kidney
    Commented Jan 30, 2015 at 7:07
  • $\begingroup$ I do not think it is possible to do what you want. i.e. call a function to generate the controls, since the controls have to be set before evaluation. The closest you can do, is use With to help reduce duplicate code. Please see injecting-list-of-controls-into-manipulate which is pretty much asking the same thing, and the discussion and links there. $\endgroup$
    – Nasser
    Commented Jan 30, 2015 at 7:10
  • $\begingroup$ @pre-kidney Please see the linked Q&A in Nasser's comment; do you think this question is a duplicate of that one, and if not, why not? $\endgroup$
    – Mr.Wizard
    Commented Jan 30, 2015 at 7:15

1 Answer 1

2
$\begingroup$

Please tell me if this does what you want:

poly[n_Integer?Positive] :=
 {
   Array[{{\[FormalC][#], #}, # - 1, # + 1} &, n + 1, 0],
   Sum[\[FormalC][i] \[FormalX]^i, {i, 0, n}]
 } /. {{var__}, expr_} :>
        Manipulate[Plot[expr, {\[FormalX], 0, 1}], var]

poly[4]

enter image description here

$\endgroup$
13
  • $\begingroup$ This approach seems like it will work well for me, but I'm running into a couple hurdles. First of all, my Plot function relies on several other procedures, and I am having issues porting it to work with your code. I will add it to an edit of the question. $\endgroup$
    – pre-kidney
    Commented Jan 30, 2015 at 9:42
  • $\begingroup$ I thought I understood the foo /. bar :> baz construction but after some fiddling it appears I was mistaken. Is it the bar :> baz that specifies the lazy replacement rule, which is then applied to foo? $\endgroup$
    – pre-kidney
    Commented Jan 30, 2015 at 9:52
  • $\begingroup$ The Manipulate is continuously running. I am on V 10.02. typing poly[4] shows the right side is solid black all the time, which means it is running all the time. To see this more clearly, I added DateString[] call, poly[n_Integer?Positive] := {Array[{{\[FormalC][#], #}, # - 1, # + 1} &, n + 1, 0], Sum[\[FormalC][i] \[FormalX]^i, {i, 0, n}]} /. {{var__}, expr_} :> Manipulate[Row[{DateString[], Plot[expr, {\[FormalX], 0, 1}]}], var] and the clock is updating all the time. $\endgroup$
    – Nasser
    Commented Jan 30, 2015 at 10:04
  • $\begingroup$ @pre-kidney I'm heading for bed so I'll have to address the update tomorrow. Regarding the second point please see (1937) and (8399) which hopefully give a foundation for my use of replacement here. If not I'll try to explain tomorrow. $\endgroup$
    – Mr.Wizard
    Commented Jan 30, 2015 at 10:05
  • 1
    $\begingroup$ @pre-kidney I've been busy today. The code from the first version of my answer with manual Hold and ReleaseHold does not seem to have that problem. I intend to come back to this but I don't have time now. Sorry. $\endgroup$
    – Mr.Wizard
    Commented Jan 31, 2015 at 1:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.