开发者

Mathematica manipulate variables that are already defined

Is it possible to use Mathematica's manipulate to change variables that have already been declared?

Example:

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

The basic idea is that 开发者_如何学GoI want to make a plot with a certain changable value but declare it outside of manipulate.

Any ideas?


One option is to use Dynamic[] and LocalizeVariables -> False.

Example:

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 *)
}

Evaluating "changeme" after the Manipulate action will retain the last Manipulate value.

HTH!


If you want anything reasonably complicated or flexible, it is best to use Dynamic and DynamicModule instead of Manipulate. The only exception is if you're writing a demonstration.

For example - a very basic way of doing what you want is (in fact you don't even need the Row and Slider if you want to just change changeme by hand.)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜