# symbols in a function to “take on” the values of controls with the same names in Manipulate

(** I want the symbols a1... in f1... to "take on" the values of the controls with the same names in Manipulate[Plot[f1[3,"a"],{x,1,3}],{a1,0,1},{a2,0,1},{a3,0,1}]. Here's what I've tried- **)

f1[n_, c_String] := Table[Symbol[c <> ToString[i]] x^i, {i, n}]

f1[3, "a"]


(** Out[202]={a1 x, a2 x^2, a3 x^3} **)

(** Works: cut & paste the Out[202] string {a1 x,a2 x^2,a3 x^3} into Plot[...] **)

Manipulate[Plot[{a1 x, a2 x^2, a3 x^3}, {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3,0, 1}]


(** Works: put Out[202] string %202 into Plot[...] **)

Manipulate[Plot[%202, a3 x^3}, {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3,0, 1}]


(** Blank plot: put the function f1[3,"a"] into Plot[...] **)

Manipulate[Plot[f1[3, "a"], {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3, 0, 1}]


(** Blank plot: variations on put the function f1[3,"a"] into Plot[...] **)

Manipulate[Plot[{f1[3, "a"]}, {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3, 0, 1}]

Manipulate[Plot[Evaluate[f1[3, "a"]], {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3,0, 1}]

Manipulate[Plot[Evaluate[{f1[3, "a"]}], {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3,0, 1}]


Thanks

• Manipulate[Plot[%202, a3 x^3}, {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3,0, 1}] contains a syntax error. – bbgodfrey Jun 2 at 21:32
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Jun 3 at 13:44

This might be what you're looking for:

f[n_, c_]:=With[
{
expr = Table[Symbol[c<>ToString[i]] x^i,{i,n}],
controls = Sequence@@Table[{Symbol[c<>ToString[i]], 0, 1}, {i, n}]
},
Manipulate[Plot[expr, {x, 1, 3}], controls]
]


f[3, "a"] will then give you a manipulate with a1, a2, a3.

• @mk_newbie A simpler solution using the same idea is With[{arg = f1[3, "a"]}, Manipulate[Plot[arg, {x, 1, 3}], {a1, 0, 1}, {a2, 0, 1}, {a3, 0, 1}]]. The issue is described in the "Possible Issues" section of the Manipulate documentation, "Manipulate only "notices" explicit visible parameters:". – bbgodfrey Jun 2 at 21:54