Group Abstract Group Abstract

Message Boards Message Boards

0
|
75 Views
|
6 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to code Control changes?

Posted 2 days ago

I have a small DynamicModule that displays a slider for x and the result of evaluating a function f[x]. The following code works as desired:

DynamicModule[{f, x = 5},
 f[x_] := {x, Sqrt[x]};
 Column[{
   Slider[Dynamic@x, {0, 25, 1}],
   Dynamic@f[x]
   }]
 ]

enter image description here

Now I want to store the results of f[x] in two variables arg and val for later use. The following code works, but it throws a Set::shape error during the assignment of the result, and the variables arg and val are not set.

DynamicModule[{f, x = 5, arg, val},
 f[x_] := {x, Sqrt[x]};
 Column[{
   Slider[Dynamic@x, {0, 25, 1}],
   {arg, val} = Dynamic@f[x],
   Row[{"{arg,val} = ", Dynamic@{arg, val}}]
   }]
 ]

enter image description here

The reason for the error is that Dynamic@f[x] is an object and not a list.

How can I fix this problem? Perhaps with something like Dynamic[f[x], someFunction] instead of {arg,val}=Dynamic[f[x]], where someFunction assigns arg and val?

POSTED BY: Werner Geiger
6 Replies
Posted 21 hours ago
POSTED BY: Simon Lowe
Posted 20 minutes ago

Hi Simon, I can see your post in my email notifications, but not here in the community. I don't know why.

You are correct with your comments on “Dynamic[x, (x = #; {arg, val} = f[x]) &]”.

David Reiss has already taught me this solution.

Thanks, Simon.

POSTED BY: Werner Geiger

Or using a multi argument Dynamic like this

DynamicModule[{f, x, arg, val},
 f[x_] := {x, Sqrt[x]};
 x = 0;
 {arg, val} = f[x];

 Column[
  {Slider[
    Dynamic[x,
     (
       x = #;
       {arg, val} = f[x]
       ) &
     ],
    {0, 25, 1}
    ],
   Row[{"{arg,val} = ", Dynamic@{arg, val}}]}]
 ]
POSTED BY: David Reiss
Posted 22 hours ago

This works. Thanks David.

POSTED BY: Werner Geiger

This variant seems to work:

DynamicModule[{f, x = 5, arg, val},
 f[x_] := {x, Sqrt[x]};
 Column[{Slider[Dynamic@x, {0, 25, 1}],
   Dynamic[{arg, val} = f[x]],
   Row[{"{arg,val} = ", Dynamic@{arg, val}}]}]]
POSTED BY: Gianluca Gorni
Posted 22 hours ago

This works. Thanks Gianluca.

(I think I tried this before. No idea why I thought it would not work).

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard