Pregunta

Say I have set up the following function f[a,b,c] which I wish to plot while varying a and b

f[a_,b_,c_]:=a b c Exp[a b]

Manipulate[
Plot
[
f[a,b,c],
{c,0,1},
PlotRange->{{0,0.05},Automatic}
],
{a,0,1},
{b,0,1}
]

Is it possible to have the ordinate scaled automatically when I fix the abscissa viewing range? You'll notice with the code above that when varying a and b the ordinate does scale automatically as if I were viewing the whole range of {c,0,1}. I would like it to still handle c from 0 to 1, but if I want to view a smaller section of this plot, say c from 0 to 0.05, still have the vertical axis scaled correctly. Thank you all for your help.

¿Fue útil?

Solución

A variant on Artes Docendo's suggestion:

Manipulate[
 Plot[f[a, b, c], {c, 0, Evaluate@d}, 
  PlotRange -> {{0, Evaluate@d}, Full}], {a, 0., 1.}, {b, 0., 1.}, {d, 
  0.05, 1.}]

Notice the Evaluate to force the machine-precision value to be fed to the Plot function before it actually tries to draw something.

I prefer Full instead of Automatic for the y-axis PlotRange in cases like this, because then you know it will never crop the plot in ways that hide parts of the curve.

Otros consejos

Here is one of many possible solutions :

f[a_, b_, c_] := a b c Exp[a b]
Manipulate[ Plot[f[a, b, c], {c, 0, d}, PlotRange -> Automatic], 
            {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, Initialization :> (d := 0.1)]

However your example is not very instructive, to see how it works better try something like this :

g[a_, b_, c_] := 3 (a - 0.5) Cos[4 Pi (a - c)] Sin[8 Pi (c - 0.5)] Cos[6 Pi (b - c)]

Manipulate[
           Plot[g[a, b, c], {c, 0, d}, PlotRange -> Automatic],
           {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, 
           Initialization :> (a := 0.4; b := 0.4; d := 0.5)]

See if this does what you want. I simply use ListPlot instead of plot.

But I am not sure what you are doing, as you are plotting f for c from 0 to 1, but then setting the x-range to only be from 0 to 0.05? Why not then just plot f using {c,0,0.05}? May be I am missing something.

Anyway, here is what I have

 Manipulate[

 xmax = 0.05;
 y = Table[f[a, b, c], {c, 0, xmax, 0.01}];
 max = Max[y];
 min = Min[y];

 Plot[f[a, b, c], {c, 0, 1},
  PlotRange -> {{0, xmax}, {min, max}}, ImagePadding -> 30],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )

 ]

edit(1)

it just occurred to me, to make the above more efficient, is to use the first Table command, to generate the data itself as well, and not just find the max/min of the plot range. And then use ListPlot instead of Plot. This should be faster, so that sampling of the function f only happens once instead of 2 times?

So here is second version

Manipulate[xmax = 0.05;

 data = Table[{c, f[a, b, c]}, {c, 0, xmax, 0.01}];
 max = Max[data[[All, 2]]];
 min = Min[data[[All, 2]]];

 ListPlot[
  data,
  PlotRange -> {Automatic, {min, max}},
  Joined -> True,
  ImagePadding -> 30
  ],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )
 ]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top