开发者

Logarithmic Slider Control in Mathematica

I'm making a small interface for calculating voltage dividers in Mathematica. I have two sliders (z1 & z2) that represent the resistor values and a couple of sliders to represent Vin as a sinusoid.

The issue is that the range of available resistor values (in the real world) is roughly logarithmic on {r, 100, 1,000,000}. If I set my slider range to r, however, it's impractical to select common low resistor values in approx. {100, 10,000}.

Is it possible to create a slider that sweeps through a logarithmic range?

Manipulate[
 Grid开发者_JAVA百科[{{Plot[(VinRCos[t] + VinC), {t, -3, 9}, 
     PlotRange -> {-1, VMax}, AxesLabel -> {t, Vin}]}, {Plot[
     z2/(z1 + z2)(VinR*Cos[t] + VinC), {t, -3, 9}, 
     PlotRange -> {-1, VMax}, AxesLabel -> {t, Vout}]}}, 
  ItemSize -> 20],
 {{z1, 10000}, 10, 1000000, 10}, {z1}, {{z2, 10000}, 10, 
  1000000}, {z2}, Delimiter, {{VinR, 2.5}, 0, 
  5}, {VinR}, {{VinC, 2}, -VMax, VMax}, {VinC}]

Logarithmic Slider Control in Mathematica


Michael's answer is probably the best, i.e. just get the user to specify the exponent. An alternate solution is to make a LogSlider type command. Here's a simple example:

LogSlider[{v:Dynamic[var_], v0_?Positive}, {min_?Positive, max_?Positive}, 
   base_:10, options___] := DynamicModule[{ev}, Dynamic[
                              var = base^ev; 
                              Slider[Dynamic[ev], Log[base, {min, max}]]]]
LogSlider[v:Dynamic[var_], {min_?Positive, max_?Positive}, 
   base_:10, options___] :=  LogSlider[{v, min}, {min, max}]

The function only has a subset of the flexibility of Slider and will have to be extended if you want custom step sizes etc...

You then modify your Manipulate by specifying the variables using {{z1, 10000}, 10, 1000000, LogSlider[##]&} etc...


A simple fix is to just make the slider manipulate the exponent, and plug in e.g. 10^z1 where you need the actual value:

Manipulate[10^z1, {{z1, 5}, 2, 6}] (* 100 to 1M *)

In your particular case, you could of course also input a list of standard resistor values to pick from:

Manipulate[z1, {z1, {100, 110, 120, 130, 150, 160, 180, 200, 220, 240, 270}}]

HTH!


Here is my final result:

Manipulate[
 Evaluate[Round[10^Z2]/(Round[10^Z1] + Round[10^Z2])*Vin] "V",
 {{Z1, 5}, 2, 6},
 Pane["Z1  = " Dynamic[Round[10^Z1] "[CapitalOmega]"], 
  ImageMargins -> {{2.5, 0}, {3, 0}}],
 {{Z2, 5}, 2, 6},
 Pane["Z2  = " Dynamic[Round[10^Z2] "[CapitalOmega]"], 
  ImageMargins -> {{2.5, 0}, {0, -5}}], {{Vin, 2.5}, 0, VMax}, 
 Pane["Vin  = " Dynamic[Vin "V"], ImageMargins -> {{0, 0}, {0, -5}}]]

Logarithmic Slider Control in Mathematica


Here is a start to LogSlider that produces the standard two-way behavior the other controls have.

LogSlider[Dynamic[x_], max_] := 
 Module[{exp}, 
  Dynamic[exp = Log[max, x]; 
   Slider[Dynamic[exp, (exp = #; x = max^exp) &]]]]

{LogSlider[Dynamic@x, 10^6], Dynamic@x}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜