Group Abstract Group Abstract

Message Boards Message Boards

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

Assign values to vector components using "Manipulate"

Posted 5 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 5 years ago
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