# Assign values to vector components using "Manipulate"

Posted 7 months ago
990 Views
|
6 Replies
|
5 Total Likes
|
 Dear Community, I am still struggling with beginner's problems but cannot find any solution in the net. The following works: Manipulate[{parameter1 = n, parameter1 = n, parameter1 = n} , {n, 0, 5}, {n, 0, 5}, {n, 0, 5}] However, all attempts to use a more generalized approach, i.e. to assign values input by "Manipulate" to the components of the vector: parameter1[i] for {i,1,k} fail (even if I assign a fixed value to k). Any help appreciated - and … apologies if this is Answer
6 Replies
Sort By:
Posted 7 months ago
 After a struggle I found that this seems to work: Manipulate[mpl[Table[parameter1[k] = n[k], {k, max}], Sequence @@ Table[{n[k], 0, 5}, {k, max}]] /. mpl -> Manipulate, {max, 1, 5, 1}] Answer
Posted 7 months ago
 Thank you very much for your swift support. However, your solution assigns the second vector to the first, but not ist value. I.e. afer selection parameter1=n but not the value/number chosen for n. Answer
Posted 7 months ago
 This is merely a beginner's problem When you define a variable for Manipulate it accepts a different name (eg. \$471 etc.) Whenever you try to generate such a name (using do, Table, Array etc.) (rather than write it explicitly) the generation works but not with the local name.So Manipulate[ Array[n, 5], Evaluate[Sequence @@ Array[{{n[#], 1}, 0, 5} &, 5]]] will display (rightfully) {n,n,n,n,n} to enforce the evaluation of these "generated variables" you need to use Evaluate[ ] Manipulate[ Evaluate[(Array[n, 5])], Evaluate[Sequence @@ Array[{{n[#], 1}, 0, 5} &, 5]]] However, this does not solve the second issue of assigning the value to the vector. I checked all the possibilities that I'm aware of. This happens since the assignment is applied immediately, therefore the right hand side of the assignment is not getting the values of n,,, (unless of course, u you use the names of the variables explicitly, and then you do not have a problem in the first place...) If I'll find a way , I'll publish it later Answer
Posted 7 months ago
 Thank you so far. Any solution for the second problem would be welcomed. Answer
Posted 7 months ago
 Well, I cracked that. Summary Avoid using n, n,... as variable names. n1, n2,... work for the assignment. n,n,... do not Achieve an additional level of evaluation "hold" using strings and ToExpression to get back the expression. Make the variables of Manipulate global using LocalVariables->False option Perform the assignment to the vector OUTSIDE Manipulate using Dynamic. All of this is required to achieve one goal - set the number of variables in a scalable manner, rather than typing is by hand So, the solution goes as follows  Clear[v]; Clear["Globaln*"]; (* The last Clear is required for repeated experiments, the first is not essential *) ivars = Sequence @@ Array[{Symbol["n" <> ToString[#]], 0, 1} &, 5] (*set of variables + their range for manipulate *) vars = Array[Symbol["n" <> ToString[#]] &, 5] (* the variables to be used in the body of manipulate and the assignment *) Finally the line {Manipulate[Evaluate@vars, Evaluate@ivars, LocalizeVariables -> False], Dynamic[v = vars]} does the work. I managed to clear them using strings and ToExpression so Mathematica developers probably are facing the same issue. I would expect a better sort of Hold` behavior, but this is what we have...Hope this helps.yehuda Answer
Posted 7 months ago
 Perfect!!!! Thank you very much. Answer