Message Boards Message Boards

0
|
5887 Views
|
6 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Assign values to vector components using "Manipulate"

Posted 4 years ago

Dear Community, I am still struggling with beginner's problems but cannot find any solution in the net. The following works:

Manipulate[{parameter1[1] = n[1], parameter1[2] = n[2], 
  parameter1[3] = n[3]} , {n[1], 0, 5}, {n[2], 0, 5}, {n[3], 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

POSTED BY: Andrej Pustisek
6 Replies

Perfect!!!! Thank you very much.

POSTED BY: Andrej Pustisek

Well, I cracked that. Summary

  1. Avoid using n[1], n[2],... as variable names. n1, n2,... work for the assignment. n[1],n[2],... do not
  2. Achieve an additional level of evaluation "hold" using strings and ToExpression to get back the expression.
  3. Make the variables of Manipulate global using LocalVariables->False option
  4. 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["Global`n*"];
(* 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

Thank you so far. Any solution for the second problem would be welcomed.

POSTED BY: Andrej Pustisek
Posted 4 years 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[1],n[2],n[3],n[4],n[5]}

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[1],,, (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

POSTED BY: Updating Name

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[4]=n[4] but not the value/number chosen for n[4].

POSTED BY: Andrej Pustisek

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}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract