3

It takes me a long time to construct the following picture:

σ = 2;
A = 1;
δ = 0.03;
pa = Plot[
      Max[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β) A - δ))], 
      {β, 0, 4}, Filling -> {1 -> Top}, 
      FillingStyle -> LightBlue, PlotStyle -> Opacity[0.5], 
      AxesLabel -> {"β", "ξ"}, PlotRange -> {{0, 4}, {0, 1.5}}
     ];

pb = Plot[
      Min[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β)A - δ))], {β, 0, 4}, 
      Filling -> {1 -> Axis, LightBlue}, PlotStyle -> Opacity[0.5], 
      AxesLabel -> {"β", "ξ"}, FillingStyle -> LightRed, 
      PlotRange -> {{0, 4}, {0, 1.5}}
     ];

pc = Plot[
      {(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β) A - δ))}, {β, 0, 4}, 
      Filling -> {2 -> {{1}, {LightOrange, LightGreen}}}, 
      PlotStyle -> Opacity[0.5], AxesLabel -> {"β", "ξ"}, 
      PlotRange -> {{0, 4}, {0, 1.5}}
     ];

Show[
  pa, pb, pc, 
  ImageSize -> Scaled[.5], AxesOrigin -> {0, 0},
  LabelStyle -> {FontFamily -> "Times New Roman"}
]

which gives exactly what I want. Now I would like to Manipulate this picture on $A$, $\delta$ and $\sigma$. But when I create the command

Manipulate[
  Show[{pa, pb, pc}], 
  {σ, 0.1, 2}, {A, 0.1, 5}, {δ, 0.001, 1}, 
  ContentSize -> Large
]

I have the graphic, the Manipulate container but nothing append when I move the cursor.

Incidentally I would like to label the curves: I know there are some exchanges on the subject, but it is not clear if the solutions will work with Manipulate.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
cyrille.piatecki
  • 4,582
  • 13
  • 26

3 Answers3

3

Move all of your plot definitions inside the Manipulate

Manipulate[
 pa = Plot[
   Max[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/\
σ (β/(1 + β) A - δ))], {β, 0, 4}, 
   Filling -> {1 -> Top}, FillingStyle -> LightBlue, 
   PlotStyle -> Opacity[0.5], AxesLabel -> {"β", "ξ"}, 
   PlotRange -> {{0, 4}, {0, 1.5}}];
 pb = Plot[
   Min[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/\
σ (β/(1 + β) A - δ))], {β, 0, 4}, 
   Filling -> {1 -> Axis, LightBlue}, PlotStyle -> Opacity[0.5], 
   AxesLabel -> {"β", "ξ"}, FillingStyle -> LightRed, 
   PlotRange -> {{0, 4}, {0, 1.5}}];
 pc = Plot[{(1 + β)/(2 β), (1 + β)^2/(2 β) \
(1/σ (β/(1 + β) A - δ))}, {β, 0, 4}, 
   Filling -> {2 -> {{1}, {LightOrange, LightGreen}}}, 
   PlotStyle -> Opacity[0.5], AxesLabel -> {"β", "ξ"}, 
   PlotRange -> {{0, 4}, {0, 1.5}}];

 Show[{pa, pb, pc}], {σ, 0.1, 2}, {A, 0.1, 5}, {δ, 
  0.001, 1}, ContentSize -> Large]

enter image description here

Jason B.
  • 68,381
  • 3
  • 139
  • 286
  • ... or alternatively define those plots as explicit functions of the parameters, so Manipulate is aware of the explicit dependence on those parameters. This may work to reduce clutter within the manipulate and retain the external definitions. Anyway, the issue has been discussed here as well, so I would think this is a possible duplicate. – MarcoB May 20 '16 at 15:46
  • @MarcoB You are totally right.... I get sloppy when questions come in right before I leave work, just post the first thing that works. :-) – Jason B. May 20 '16 at 18:14
3

I would use DyanmicModule to give better localization. I would also put as many plotting options as possible into Show to reduce code repetition.

DynamicModule[{pa, pb, pc, A, δ, σ},
  pa[A_, δ_, σ_] := 
    Plot[Max[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β) A - δ))], {β, 0, 4},
      Filling -> {1 -> Top},
      FillingStyle -> LightBlue];
  pb[A_, δ_, σ_] := 
    Plot[Min[(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β) A - δ))], {β, 0, 4},
      PlotRange -> {{0, 4}, {0, 1.5}},
      Filling -> {1 -> Bottom},
      FillingStyle -> LightRed];
  pc[A_, δ_, σ_] := 
    Plot[{(1 + β)/(2 β), (1 + β)^2/(2 β) (1/σ (β/(1 + β) A - δ))}, {β, 0, 4},
      Filling -> {2 -> {{1}, {LightOrange, LightGreen}}}];
  Manipulate[
    Show[pb[A, δ, σ], pa[A, δ, σ], pc[A, δ, σ],
      AxesLabel -> {"β", "ξ"},
      PlotStyle -> Opacity[0.5],
      AxesOrigin -> {0, 0}],
    {σ, 0.1, 2., 0.1, Appearance -> "Labeled"},
    {A, 0.1, 5., 0.1, Appearance -> "Labeled"},
    {δ, 0.01, 1.0, 0.01, Appearance -> "Labeled"},
    ContentSize -> Large]]

demo

Note that pb must be plotted first because it is controlling the plot range.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
1

Define plotting functions outside Manipulate:

g1[k_] := Plot[Sin[k x], {x, 0, 2 Pi}];
g2[k_] := Plot[Cos[k x], {x, 0, 2 Pi}];
g3[k_] := Show[g1[k], g2[k]];
Manipulate[g3[k], {k, 1, 10, 1}]
mitochondrial
  • 1,843
  • 10
  • 16
  • Although I think I can see where you are going with this, I am not sure that it is clearly spelled out. Your suggestion is to define the plots as functions explicitly dependent on the parameters to be manipulated to circumvent scoping issues, correct? Since the OP showed full code for her problem, perhaps you could show the pertinent solution here, rather than a generic one. – MarcoB May 20 '16 at 15:44
  • How can I copy, from Mathematica, and paste, here, code containing greek characters ? – mitochondrial May 20 '16 at 16:48
  • I copy straight from Mathematica, then use this character converter: http://steampiano.net/msc/ . mgoldberg also has some general pointers on copy / pasting formatted code for this site. – MarcoB May 20 '16 at 17:48