Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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[{{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}]
Mathematica graphics

like image 538
terrace Avatar asked Mar 15 '11 03:03

terrace


2 Answers

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...

like image 97
Simon Avatar answered Oct 31 '22 08:10

Simon


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!

like image 33
Michael Pilat Avatar answered Oct 31 '22 09:10

Michael Pilat