# Dynamic variables inside Manipulate

I have a problem while playing with a Dynamic Slider inside a Manipulate. The code looks like this:

Manipulate[
Module[{efl, swath, fov, d1, R1, R2, α},

efl = (1 10^-3) (h p)/gsd;

swath = (1 10^-3) np gsd;

fov = (1/Degree) 2 ArcTan[swath/(2 h)];

d1 = FindRoot[
1.0 - (1 10^-3) (λ h)/(
x grd) (1 + ((x p)/(λ efl))^1.35)^(1/1.35), {x,
1 10^-3}, MaxIterations -> 100][[1, 2]];

R1 = -2 (Δ efl)/((-Δ + e) - efl);

R2 = 2.0 ((-Δ + e) (R1/2.0 - Δ))/(R1/
2.0 - 2.0 Δ + e);

Grid[{{"α",
Dynamic@Slider[Dynamic[α], {-(fov/2), fov/2, fov/10},
AutoAction -> False],
Dynamic[α]}}]

],

Grid[{
{Row[{Control[{{λ, 0.1, "Wavelenght [μm]"},
Range[0.1, 1.0, 0.1]}], Spacer,
Control[{{h, 400, "Altitude [Km]"}, Range[400, 700, 50] }],
Spacer,
Control[{{gsd, 0.3, "GSD [m]"}, Range[0.3, 5.0, 0.1] }],
Spacer,
Control[{{p, 2, "Pixel Pitch [μm]"},
Range[2.0, 15.0, 1.0]}], Spacer,
Control[{{np, 2000, "\!$$\*SubscriptBox[\(N$$, $$p$$]\) [U.A]"},
Range[2000, 12000, 1000] }]}], Spacer},

{},

{Grid[{{"GRD",
Dynamic@Slider[
Dynamic[grd], {gsd (1 + (1 10^-3 (λ h)/(
12.0 gsd))^1.35)^(1/1.35) , 10 gsd},
AutoAction -> False], Dynamic[grd]}}]},

{},

{Row[{Control[{{Δ, -0.5, "Dist. P-S [m]"},
Range[-0.5, -0.1, 0.02]}], Spacer,
Control[{{e0, 0.01, "Focus Distance [m]"},
Range[0.01, 0.1, 0.01]}]}]}}], SaveDefinitions -> True]


The problem is in the definition of α. With Manipulate I can control the basic parameter for the calculation of efl, swath, fov, d1, R1 and R2. The limits for the Slider which varies α depends on the variable fov. Dont know why but Mathematica doesnt like the way I'm defining this variable and highlights in red the fov parameter inside the Dynamic@Slider of α. What I'm doing wrong?

Any help would be appreciated.

• Does it work as expected? Your code contains undefined symbols. If the only issue is the red coloring, than change Module to DynamicModule. The reason for the syntax highlighting is that one should not use Module variables inside Dynamic. Ref.: mathematica.stackexchange.com/a/29461/18476 Oct 3, 2016 at 12:13
• @Karsten7. Excellent!. That solved the problem. Thanks a lot for your help! Oct 3, 2016 at 12:18
• Variables inside Dynamic[] should not be Module variables, when the scope of the Module contains the Dynamic. Dynamic[Module[{x},f[x]]] is OK, but not Module[{x}, Dynamic[f[x]]]. Using DynamicModule[] instead as @Karsten suggests is OK, but it might not work the way you want. It will reset α every time a control is changed. Oct 3, 2016 at 12:19
• @MichaelE2 right know I need α to change when the variable fov changes because fov sets the limits of α. Oct 3, 2016 at 12:21
• OK. Do you mind if α is reset when λ, Δ, or e0 changes, even though fov is unaltered? Oct 3, 2016 at 12:28

A simpler solution may be to place the alpha slider up in the control area (along with all the other controls). Sliders and other controllers in a Manipulate can have dependent endpoints. Note how the range of the alpha slider changes dynamically with the variable fov. When a variable is reset that causes alpha to be outside the endpoints, alpha is reset by the If statement.

Manipulate[Module[{efl, swath, fov, d1, R1, R2},
efl = (1 10^-3) (h p)/gsd;
swath = (1 10^-3) np gsd;
fov = (1/Degree) 2 ArcTan[swath/(2 h)];
end = Range[-fov/2, fov/2, fov/10];
If[\[Alpha] < Min[end] || \[Alpha] > Max[end], alpha = fov/2];
d1 = FindRoot[1.0 - (1 10^-3) (\[Lambda] h)/(x grd) (1 + ((x p)/(\[Lambda] efl))^1.35)^(1/1.35), {x, 1 10^-3}, MaxIterations -> 100][[1, 2]];
R1 = -2 (\[CapitalDelta] efl)/((-\[CapitalDelta] + e) - efl);
R2 = 2.0 ((-\[CapitalDelta] + e) (R1/2.0 - \[CapitalDelta]))/(R1/
2.0 - 2.0 \[CapitalDelta] + e);
{\[Alpha], end}],
Grid[{{Row[{Control[{{\[Lambda], 0.1, "Wavelenght [\[Mu]m]"},
Range[0.1, 1.0, 0.1]}], Spacer,
Control[{{h, 400, "Altitude [Km]"}, Range[400, 700, 50]}],
Spacer,
Control[{{\[Alpha], 0.1, "alpha"}, Dynamic[end]}], Spacer,
Control[{{gsd, 0.3, "GSD [m]"}, Range[0.3, 5.0, 0.1]}], Spacer,
Control[{{p, 2, "Pixel Pitch [\[Mu]m]"},
Range[2.0, 15.0, 1.0]}], Spacer,
Control[{{np, 2000, "\!$$\*SubscriptBox[\(N$$, $$p$$]\) [U.A]"}, Range[2000, 12000, 1000]}]}], Spacer}, {},
{Grid[{{"GRD", Dynamic@Slider[
Dynamic[grd], {gsd (1 + (1 10^-3 (\[Lambda] h)/(12.0 gsd))^1.35)^(1/1.35), 10 gsd}, AutoAction -> False],
Dynamic[grd]}}]}, {}, {Row[{Control[{{\[CapitalDelta], -0.5,
"Dist. P-S [m]"}, Range[-0.5, -0.1, 0.02]}], Spacer,
Control[{{e0, 0.01, "Focus Distance [m]"},
Range[0.01, 0.1, 0.01]}]}]}}], SaveDefinitions -> False]

• Thanks @bills for your explanation. It works and avoids the reset of alpha every time a variable different from fov` is changed. Oct 3, 2016 at 13:45