Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 I want to make a plot with a certain changable value but declare it outside of manipulate.

Any ideas?

like image 562
knpwrs Avatar asked Feb 27 '23 02:02

knpwrs


2 Answers

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!

like image 137
Dr. belisarius Avatar answered Apr 06 '23 07:04

Dr. belisarius


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]]
like image 39
Simon Avatar answered Apr 06 '23 08:04

Simon