Mathematica 中的对数滑块控制

发布于 2024-10-22 13:29:03 字数 812 浏览 2 评论 0原文

我正在制作一个小接口,用于在 Mathematica 中计算分压器。我有两个滑块(z1 和 z2)表示电阻值,还有几个滑块将 Vin 表示为正弦曲线。

问题是可用电阻值的范围(在现实世界中)大致是 {r, 100, 1,000,000} 上的对数。但是,如果我将滑块范围设置为 r,则选择大约 100% 左右的常见低电阻值是不切实际的。 {100, 10,000}

是否可以创建一个扫过对数范围的滑块? <代码>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桃气十足 2024-10-29 13:29:03

迈克尔的答案可能是最好的,即让用户指定指数。另一种解决方案是创建一个 LogSlider 类型命令。这是一个简单的示例:

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}]

该函数仅具有 Slider 灵活性的子集,如果您想要自定义步长等,则必须对其进行扩展...

然后修改您的 Manipulate > 通过使用指定变量
{{z1, 10000}, 10, 1000000, LogSlider[##]&} 等...

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

傲娇萝莉攻 2024-10-29 13:29:03

一个简单的解决方法是让滑块操纵指数,然后在需要实际值的地方插入例如 10^z1

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

在您的特定情况下,您当然也可以输入标准电阻器列表可供选择的值:

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

HTH!

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!

扮仙女 2024-10-29 13:29:03

这是我的最终结果:
<代码>

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}}]]

enter image description here

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}}]]


enter image description here

罪歌 2024-10-29 13:29:03

这是 LogSlider 的开始,它产生其他控件所具有的标准双向行为。

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}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文