3
$\begingroup$

I am not a mathematician, and I barely know how to use Mathematica. But, over the course of many months and with help from this site awhile back, I managed to generate dynamic output that does exactly what I want it to do and looks exactly like I want it to look. However, the dynamics of the output are not smooth. When I slide the interactive sliders to change parameter values, the performance is very poor. I do not get a nice, smooth, and rapid change in the output. Then, I made matters worse when I upgraded my operating system and also upgraded to the latest version of Mathematica. My output is barely usable now.

I am hoping that somebody can re-write some of my code to fix this problem but without changing the fundamental output and appearance of the output (i.e., I want the same user interactivity, the same functions to be plotted, for those functions to change dynamically with the same user input, to calculate the values below the graphs, and for all the output's formatting to stay the same). Perhaps I am trying to do too much, but I am hoping there is more efficient coding or some other solution.

I use a MacBook Pro (Retina, 13-inch, Early 2015) with a 3.1 GHz Intel Core i7 processor, 16 GB 1867 MHz DDR3 memory, an Intel Iris Graphics 6100 1536 MB graphics card, Mac OS High Sierra version 10.13.3 system, and Mathematica 11.3.0.0.

Here is the code:

Manipulate[
 P = ArcSin[
   0.39795*Cos[0.2163108 + 2*ArcTan[0.9671396*Tan[0.0086*(x - 186)]]]];
 AEL = (Sin[DL*Pi/180] + Sin[EL*Pi/180]*Sin[P])/(Cos[EL*Pi/180]*
     Cos[P]);
 CAEL = Clip[AEL];
 AML = (Sin[DL*Pi/180] + Sin[ML*Pi/180]*Sin[P])/(Cos[ML*Pi/180]*
     Cos[P]);
 CAML = Clip[AML];
 AMML = (Sin[DL*Pi/180] + 
     Sin[((((ML*x) - (ML*EMB) - (EL*x) + (EL*EMB))/(EME - EMB)) + EL)*
        Pi/180]*Sin[(ArcSin[.39795*
          Cos[.2163108 + 
            2*ArcTan[.9671396*Tan[.00860 (x - 186)]]]])])/(Cos[((((ML*
               x) - (ML*EMB) - (EL*x) + (EL*EMB))/(EME - EMB)) + EL)*
       Pi/180]*Cos[(ArcSin[.39795*
         Cos[.2163108 + 
           2*ArcTan[.9671396*Tan[.00860 (x - 186)]]]])]);
 CAMML = Clip[AMML];
 AMEL = (Sin[DL*Pi/180] + 
     Sin[((((EL*x) - (EL*LMB) - (ML*x) + (ML*LMB))/(LME - LMB)) + ML)*
        Pi/180]*Sin[(ArcSin[.39795*
          Cos[.2163108 + 
            2*ArcTan[.9671396*Tan[.00860 (x - 186)]]]])])/(Cos[((((EL*
               x) - (EL*LMB) - (ML*x) + (ML*LMB))/(LME - LMB)) + ML)*
       Pi/180]*Cos[(ArcSin[.39795*
         Cos[.2163108 + 
           2*ArcTan[.9671396*Tan[.00860 (x - 186)]]]])]);
 CAMEL = Clip[AMEL];
 padding = {{20, 10}, {10, 10}};
 photoplotrange = {{0, 365}, {0, 24}};
 latplotrange = {{0, 365}, {-90, 90}};
 rr = Reap[Show[
    Plot[24 - (24/Pi)*ArcCos[CAEL], {x, 0, 365}, 
     PlotRange -> photoplotrange, 
     Ticks -> {{{79, "Mar 20"}, {172, "Jun 21"}, {265, 
         "Sep 22"}, {355, "Dec 21"}}, {0, 2, 4, 6, 8, 10, 12, 14, 16, 
        18, 20, 22, 24}}, AxesOrigin -> {0, 12}, 
     PlotStyle -> {Black, Thin}, PlotLabel -> "Hours of Daylight", 
     ImageSize -> Medium, ImagePadding -> padding],
    Plot[24 - (24/Pi)*ArcCos[CAML], {x, 0, 365}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black, Thin}],
    Plot[y = 24 - (24/Pi)*ArcCos[CAEL], {x, 0, EMB}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black}, 
     EvaluationMonitor :> Sow[{x, y}]],
    Plot[y = 24 - (24/Pi)*ArcCos[CAMML], {x, EMB, EME}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black, Dotted}, 
     EvaluationMonitor :> Sow[{x, y}]], 
    Plot[y = 24 - (24/Pi)*ArcCos[CAML], {x, EME, LMB}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black}, 
     EvaluationMonitor :> Sow[{x, y}]],
    Plot[y = 24 - (24/Pi)*ArcCos[CAMEL], {x, LMB, LME}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black, Dotted}, 
     EvaluationMonitor :> Sow[{x, y}]], 
    Plot[y = 24 - (24/Pi)*ArcCos[CAEL], {x, LME, 365}, 
     PlotRange -> photoplotrange, PlotStyle -> {Black}, 
     EvaluationMonitor :> Sow[{x, y}]]]]; rr1 = Reap[
   Plot[y = 24 - (24/Pi)*ArcCos[CAEL], {x, 0, EMB}, 
    PlotRange -> photoplotrange, PlotStyle -> {Black}, 
    EvaluationMonitor :> Sow[{x, y}]]];
 rr2 = Reap[
   Plot[y = 24 - (24/Pi)*ArcCos[CAEL], {x, LME, 365}, 
    PlotRange -> photoplotrange, PlotStyle -> {Black}, 
    EvaluationMonitor :> Sow[{x, y}]]];
 rr3 = Reap[
   Plot[y = 24 - (24/Pi)*ArcCos[CAML], {x, EME, LMB}, 
    PlotRange -> photoplotrange, PlotStyle -> {Black}, 
    EvaluationMonitor :> Sow[{x, y}]]]; Column[{
   Show[
    Plot[EL, {x, 0, EMB}, PlotRange -> latplotrange, 
     Ticks -> {{{79, "Mar 20"}, {172, "Jun 21"}, {265, 
         "Sep 22"}, {355, "Dec 21"}}, {-90, -60, -30, 0, 30, 60, 90}},
      AxesOrigin -> {0, 0}, PlotStyle -> {Black}, 
     PlotLabel -> "Degrees Latitude", ImageSize -> Medium, 
     AspectRatio -> 0.2, ImagePadding -> padding],
    Plot[(((ML - EL)/(EME - EMB))*
        x) + ((((EL*EME) - (ML*EME))/(EME - EMB)) + ML), {x, EMB, 
      EME}, PlotRange -> latplotrange, AxesOrigin -> {0, 0}, 
     PlotStyle -> {Black, Dotted}],
    Plot[ML, {x, EME, LMB}, PlotRange -> latplotrange, 
     AxesOrigin -> {0, 0}, PlotStyle -> {Black}], 
    Plot[(((EL - ML)/(LME - LMB))*
        x) + ((((ML*LME) - (EL*LME))/(LME - LMB)) + EL), {x, LMB, 
      LME}, PlotRange -> latplotrange, AxesOrigin -> {0, 0}, 
     PlotStyle -> {Black, Dotted}],
    Plot[EL, {x, LME, 365}, PlotRange -> latplotrange, 
     AxesOrigin -> {0, 0}, PlotStyle -> {Black}]],
   rr[[1]],
   StringTemplate["Proportion of Year in Daylight: `1`"]@
    Round[(Total[-Mean@#[[All, 2]] Subtract @@ #[[All, 1]] & /@ 
         Partition[Sort[rr[[2, 1]]], 2, 1]])/8760, 0.001],
   StringTemplate["Total Early/Late-Year Hours Daylight: `1`"]@
    Round[Total[-Mean@#[[All, 2]] Subtract @@ #[[All, 1]] & /@ 
        Partition[Sort[rr1[[2, 1]]], 2, 1]] + 
      Total[-Mean@#[[All, 2]] Subtract @@ #[[All, 1]] & /@ 
        Partition[Sort[rr2[[2, 1]]], 2, 1]], 1],
   StringTemplate["Total Mid-Year Hours Daylight: `1`"]@
    Round[Total[-Mean@#[[All, 2]] Subtract @@ #[[All, 1]] & /@ 
       Partition[Sort[rr3[[2, 1]]], 2, 1]], 1]}],
 {{DL, 0.8333, "Daylight Definition:"}, {0 -> "Sun Center at Horizon",
    0.26667 -> "Sun Top at Horizon", 
   0.8333 -> "Sun Top Apparent at Horizon", 
   6 -> "Civil Twilight Included", 12 -> "Nautical Twilight Included",
    18 -> "Astronomical Twilight Included"}, Appearance -> "Open"},
 {{EL, -70.0, "Early/Late-Year Latitude"}, -90, 90, 
  Appearance -> "Open"},
 {{ML, 70.0, "Mid-Year Latitude"}, -90, 90, Appearance -> "Open"},
 {{EMB, 93, "Early-Year Migration Begins"}, 1, EME, 
  Appearance -> "Open"},
 {{EME, 136, "Early-Year Migration Ends"}, (Abs[ML - EL]/21) + EMB, 
  365, Appearance -> "Open"},
 {{LMB, 229, "Late-Year Migration Begins"}, EME + 1, 355, 
  Appearance -> "Open"},
 {{LME, 309, "Late-Year Migration Ends"}, (Abs[EL - ML]/21) + LMB, 
  364, Appearance -> "Open"},
  ControlPlacement -> Left]
$\endgroup$
4
$\begingroup$

The problem is that it's constantly updating. An update is triggered whenever a variable is changed. You have many global variables (P, AEL, etc.) that are updated whenever the body is executed. And each time the body is executed, it triggers another update, ad infinitum. There is a simple fix, though. Add TrackedSymbols :> True to the end of you Manipulate:

Manipulate[
 <...your code here...>,
 TrackedSymbols :> True
]
$\endgroup$
  • $\begingroup$ BTW, a better approach would be to localize your variables, which would also eliminate the dependencies on P, AEL, etc. $\endgroup$ – Michael E2 Mar 15 '18 at 20:22
  • $\begingroup$ Michael E2, thank you so much. Your simple fix greatly improved how this works. At some point, I will work on figuring out how to localize my variables, as you suggest. But for now, this is 100% improved. Thanks again. --Keith $\endgroup$ – kwsockman Mar 15 '18 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.