是否可以使用 Dynamic 的第二个参数在 Manipulate 中设置控制变量? [英] is it possible to use second argument of Dynamic in setting up control variables inside Manipulate?

查看:17
本文介绍了是否可以使用 Dynamic 的第二个参数在 Manipulate 中设置控制变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用语法来做我想做的事,我现在不确定是否可行.

I can't get the syntax to do what I want, and I am now not sure if it is even possible.

小评论:可以这样做:

{Slider[Dynamic[b], {-2 Pi, 2 Pi}], Dynamic[Sin[b]]}

现在每次滑块移动时,'b' 都会改变,并且它的 Sin[] 会自动打印

and now each time the slider moves, 'b' changes, and its Sin[] is automatically printed

但是假设我想直接在滑块所在的位置进行计算(Sin[])并且只显示 Sin[] 的最终结果,那么我可以像这样使用 Dynamic 的第二个参数:

But suppose I want to do the computation (Sin[]) directly where the slider is and only show the final result of Sin[], then I can use the second argument of Dynamic like this:

{Slider[Dynamic[b, (b = #; a = Sin[b]; #) &], {-2 Pi, 2 Pi}], 
 Dynamic[a]}

现在我想使用 Manipulate,并做同样的事情.我可以像上面的第一个例子那样做:

Now I want to use Manipulate, and do the same thing. I can do the same as the first example above like this:

Manipulate[
 Sin[b],
 Control[{{b, 0, "b="}, -2 Pi, 2 Pi, ControlType -> Slider}]
 ]

在上面,Manipulate 负责处理 'Dynamic' 的内容,并在 'b' 改变时更新 Sin[b].

In the above, Manipulate took care of the 'Dynamic' stuff, and updated Sin[b] whenever 'b' changes.

现在,我想看看我是否可以使用 Manipulate 来做第二种情况,所以我可以写:

Now, I want to see if I can do the second case using Manipulate, so I can write:

Manipulate[
 a,
 Control[{{b, 0, "b="}, -2 Pi, 2 Pi, ControlType -> Slider}] (*where to insert Sin[b]?*)
 ]

('a' 必须初始化为某个值以进行初始显示).

('a' has to initialized to some value for initial display).

但我无法弄清楚如何在上面的 'b' 中使用第二个参数.无法弄清楚语法,现在确定是否可能?

But I am not able to figure how to use the second argument for 'b' in the above. Can't figure the syntax, and now sure if it possible?

当然不能只写

Manipulate[
 a,
 {Slider[Dynamic[b, (b = #; a = Sin[b]; #) &], {-2 Pi, 2 Pi}]}
 ]

这甚至不是 Manipulate 控件的有效语法.

This is not even valid syntax of Manipulate controls.

问题是:是否可以在设置 Manipulate 控件时使用 Dynamic 的第二个参数?

Question is: Is it possible to use second argument of Dynamic in setting up Manipulate controls?

我问的原因是,它可以更容易地在控制变量发生变化的地方本地化"计算,并且只在其他地方显示最终结果.在某种程度上类似于本地回调函数,其中与每个控件的更改相关的计算就位于控件所在的位置旁边.

The reason I am asking, is that it could make it easier to 'localize' computation right there, where the control variable changes, and only show the final result elsewhere. Like a local callback function in a way, where computation related to changes for each control sits right next to where the control is.

谢谢

2011 年 9 月 16 日更新

好吧,经过一个月的努力,我终于启动并运行了 Mathematica CDF.

Well, after a month of struggle, finally I have the Mathematica CDF up and running.

感谢 Simon 在这里展示的技巧的帮助,以及在我做这个 CDF 时回答我问题的其他人(Leonid、Heike、Wreach、Belisarius 和其他人).

Thanks to the help of the trick shown here by Simon, and others who answered my questions while I was doing this CDF (Leonid, Heike, WReach, Belisarius and others).

我在这里学到的一些技巧帮助完成了这个演示,这是一个链接 如果有人想尝试一下.

The few tricks I learned here helped finish this demonstration, here is a link if someone wants to try it.

这个 CDF 的设计与我之前所做的一切都不同.它基于有限状态机.整个 CDF 中只有一个跟踪符号.使用dynamics的第二个参数,记录事件名称,在运行有限状态机的Manipulate的主要表达式中,它有5个状态可以进入,有8个事件.根据当前状态和刚刚发生的当前事件,它切换到新状态(或可以保持相同状态,具体取决于),并且主显示被更新.不需要触发器.状态机以您允许的速度运行,仅由时间步长控制.

The design of this CDF is different from everything I've done before. It is based on finite state machine. There is only ONE tracked symbol in the whole CDF. Using the second argument of dynamics, the event name is recorded, and in the main expression of Manipulate, which runs the finite state machines, it has 5 states it can be in, and there are 8 events. Depending on the current state and the current event that just happened, it switches to new state (or can stay in the same state, depending), and the main display is updated. No trigger needed. The state machine runs as fast as you allow it, controlled only by the time step size.

这极大地简化了逻辑,并使处理更高级的 UI 和相互依赖的逻辑成为可能,因为现在一切都以良好受控的方式运行,并且所有逻辑都集中在一个地方.

This simplifies the logic greatly, and makes it possible to handle make much more advanced UI and inter-dependent logic, since everything now runs in a well controlled way, and all the logic is in one place.

如果不能使用动力学的第二个参数设置事件,整个方法就不可能实现.

Without being able to set the event using the second argument of dynamics, this whole approach would not have been possible.

我需要写一个关于这个方法的注释以使其更清楚.

I need to write a note on this method to make it more clear.

所以,只想感谢这里的每一个人.我现在正在使用这种方法完成另一个 CDF,即三重摆模拟.

So, just wanted to thank everyone here. I am now finishing another CDF using this method, a triple pendulum simulation.

推荐答案

并非 Manipulate 的第一个参数之后的所有参数都必须是 Control 对象.您可以在其中放置任何您喜欢的东西 - 包括完全自定义的动态控件.那么,像

Not all of the arguments of Manipulate after the first one have to be Control objects. You can put anything you like there - including completely customized dynamic controls. So, how about something like

Manipulate[a, {a, None}, {b, None}, 
 Row[{"b= ",Slider[Dynamic[b, (b = #; a = Sin[b]; #)&], {-2 Pi, 2 Pi}]}]]

其中 {a, None}{b, None} 确保变量是本地的,但不是绝对必要的.

Where the {a, None} and {b, None} ensure that the variables are local, but aren't strictly necessary.

这篇关于是否可以使用 Dynamic 的第二个参数在 Manipulate 中设置控制变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆