Mathematica操纵已定义的变量

时间:2010-10-08 08:36:32

标签: wolfram-mathematica

是否可以使用Mathematica的操作来更改已经声明的变量?

示例:

changeme = 8;
p = SomeSortOfPlot[changeme];
manipulate[Show[p],{changeme,1,10}]

基本的想法是,我想制作一个具有某个可变值的情节,但在操纵之外宣布它。

有什么想法吗?

2 个答案:

答案 0 :(得分:6)

一种选择是使用Dynamic []和LocalizeVariables - >假

示例:

changeme = 8;
p[x_] := Plot[Sin[t], {t, 1, x}];

{
 Manipulate[p[changeme], {changeme, 2, 9}, LocalizeVariables -> False], 
 Dynamic[changeme]   (* This line is NOT needed, inserted just to see the value *)
}

在Manipulate操作后评估“changeme”将保留最后一个Manipulate值。

HTH!

答案 1 :(得分:2)

如果您想要任何相当复杂或灵活的内容,最好使用DynamicDynamicModule代替Manipulate。唯一的例外是如果你写的是demonstration

例如 - 做你想做的事的一个非常基本的方式 (事实上​​,如果您想手动更改Row,则甚至不需要Sliderchangeme。)

changeme=8;
p[x_]:=Plot[Sin[t],{t,1,x}];
Row[{"x \[Element] (1, ",Dynamic[changeme],")  ",Slider[Dynamic[changeme],{2,9}]}]
Dynamic[p[changeme]]