4

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[40], 
  Control[{{h, 400, "Altitude [Km]"}, Range[400, 700, 50] }], 
  Spacer[40], 
  Control[{{gsd, 0.3, "GSD [m]"}, Range[0.3, 5.0, 0.1] }], 
  Spacer[40], 
  Control[{{p, 2, "Pixel Pitch [μm]"}, 
    Range[2.0, 15.0, 1.0]}], Spacer[40], 
  Control[{{np, 2000, "\!\(\*SubscriptBox[\(N\), \(p\)]\) [U.A]"},
     Range[2000, 12000, 1000] }]}], Spacer[40]},

  {},

  {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[40], 
  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.

Tomas Libutti
  • 51
  • 1
  • 7
  • 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 ``Modulevariables insideDynamic`. Ref.: http://mathematica.stackexchange.com/a/29461/18476 – Karsten7 Oct 03 '16 at 12:13
  • @Karsten7. Excellent!. That solved the problem. Thanks a lot for your help! – Tomas Libutti Oct 03 '16 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. – Michael E2 Oct 03 '16 at 12:19
  • @MichaelE2 right know I need α to change when the variable fov changes because fov sets the limits of α. – Tomas Libutti Oct 03 '16 at 12:21
  • OK. Do you mind if α is reset when λ, Δ, or e0 changes, even though fov is unaltered? – Michael E2 Oct 03 '16 at 12:28
  • Oh didnt see that. How can I prevent this to happen? @MichaelE2 – Tomas Libutti Oct 03 '16 at 12:39
  • Another thing is that the variables that are defined inside the Grid are used in the subsequent part of the code but I cant end this command with ; because I need the bars to be printed when the program is executed. This is a problem because if I write another equation below Grid without the semicolon at the end the programs gives me Null at the output. How can I solve this? Thanks – Tomas Libutti Oct 03 '16 at 12:45
  • Wouldn't it be simpler to put the alpha variable up in the control section of the Manipulate? – bill s Oct 03 '16 at 12:55
  • Thanks for the responde @bills. There is a little problem with alpha being defined in the control section because there is a problem with the limits depending on the variable fov which is defined inside the code. It gives any value to alpha not related to te actual value of fov – Tomas Libutti Oct 03 '16 at 13:04
  • Hey guys why when I put the Dynamic control of a variable (for example α in the previous code) whenever I try to operate with this variable I get an error and if I want to print α as output Mathematica return this symbol: α$$. Any clue? – Tomas Libutti Oct 03 '16 at 16:35

1 Answers1

3

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[40], 
      Control[{{h, 400, "Altitude [Km]"}, Range[400, 700, 50]}], 
      Spacer[40],
      Control[{{\[Alpha], 0.1, "alpha"}, Dynamic[end]}], Spacer[20], 
      Control[{{gsd, 0.3, "GSD [m]"}, Range[0.3, 5.0, 0.1]}], Spacer[40], 
      Control[{{p, 2, "Pixel Pitch [\[Mu]m]"}, 
        Range[2.0, 15.0, 1.0]}], Spacer[40], 
      Control[{{np, 2000, "\!\(\*SubscriptBox[\(N\), \(p\)]\) [U.A]"}, Range[2000, 12000, 1000]}]}], Spacer[40]}, {}, 
     {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[40], 
      Control[{{e0, 0.01, "Focus Distance [m]"}, 
        Range[0.01, 0.1, 0.01]}]}]}}], SaveDefinitions -> False]
bill s
  • 68,936
  • 4
  • 101
  • 191
  • Thanks @bills for your explanation. It works and avoids the reset of alpha every time a variable different from fov is changed. – Tomas Libutti Oct 03 '16 at 13:45