当参数改变时更新 Manipulate[]'d 图 [英] Update Manipulate[]'d plots when parameters change

查看:19
本文介绍了当参数改变时更新 Manipulate[]'d 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我一直在为一个项目使用 Mathematica 的 Manipulate 函数.

I've been fighting with Mathematica's Manipulate function for the last few days for a project.

我正在调整进入物理模型的假设和边界条件.为此,我希望能够绘制不同的方程并调整参数并动态更新图表.Manipulate 似乎是完成这项工作的完美工具——除了我无法让它工作.当参数改变时,绘图不会更新.

I'm working on tweaking assumptions and boundary conditions that go into a physical model. For this, I want to be able to plot different equations and adjust the parameters and have the graphs update on the fly. Manipulate seems to be the perfect tool for the job -- except that I can't get it to work. The plots won't update when the parameters are changed.

基本示例:

a =.;
b =.;
c =.;
func1[x_] := a*x;
func2[x_] := a*x^2 + b*x + c;
funcNamesList := {"Linear", "Quadratic"};
funcList := {func1[x], func2[x]}
Manipulate[
   Plot[function, {x, -5, 5}], {function,MapThread[Function[#1 -> #2],
    {funcList, funcNamesList}]}, {a, -5, 5}, {b, -5, 5}, {c, -5, 5},
    LocalizeVariables -> False
]

例如我可以通过点击func1,调整a,然后点击func1来刷新func1code> 再次,但我希望在我调整 a 时更新它,因为我使用的实际函数在参数方面相当不稳定.

I can get, for example, func1 to refresh by clicking func1, adjusting a, and then clickingfunc1 again, but I'm hoping to have it update when I adjust a because the real functions I'm using are rather temperamental with respect to their parameters.

-因为我将处理具有不同参数的长函数,所以使用函数列表很有用.

-Because I'll be dealing with long functions that have different parameters, using a list of functions is useful.

如果它为任何人产生任何想法,以下是我想要做的各个组件的一些工作示例(来自 Wolfram 文档):

In case it produces any ideas for anyone, here are some working examples of the individual components of what I want to do (from the Wolfram documentation):

绘制图形并在更改参数时更新它们:

Plot graphs and have them update when parameters are changed:

Manipulate[
    Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
    {{b, 0, "Phase Parameter"}, 0, 10}
]

注意:当函数被带到外面时,这会中断:

Note: This breaks when the function is taken outside:

func[x] := Sin[a x + b];
Manipulate[
  Plot[func[x], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
  {{b, 0, "Phase Parameter"}, 0, 10}, LocalizeVariables -> False
]

更改正在绘制的函数的示例:

Example of changing the function being plotted:

Manipulate[
   Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> "sine", Cos -> "cosine", Tan -> "tangent"}}
]

编辑 2将 func2 从 a*x^2 更改为 a*x^2 + b*x + c 以反映函数可能具有不同参数的事实.

Edit 2 Changed func2 from a*x^2 to a*x^2 + b*x + c to reflect the fact that the functions may have different parameters.

编辑 3 添加了我用来在功能按钮上取好名字的花絮.

Edit 3 Added the tidbit I use to get nice names on the function buttons.

推荐答案

有两个问题会阻止您的 Manipulate 语句工作.

There are two problems that prevent your Manipulate statement from working.

首先,虽然 Manipulate 变量 a 是全局的,因为 LocalizeVariables ->False 设置,Plot 变量 x 不是.xPlot 表达式的局部变量.

First, while the Manipulate variable a is global due to the LocalizeVariables -> False setting, the Plot variable x is not. x is local to the Plot expression.

第二个问题是Manipulate,默认情况下,假定TrackedSymbols ->完整.这意味着仅跟踪明确出现在操纵表达式中的符号.请注意,a 不会出现在表达式中,因此不会被跟踪.

The second problem is that Manipulate, by default, assumes TrackedSymbols -> Full. This means that only symbols that explicitly appear in the manipulated expression are tracked. Note that a does not appear in the expression, so it is not tracked.

我们可以这样纠正这两个问题:

We can correct both problems thus:

a =.;
function =.;
func1[x_] := a*x;
func2[x_] := a*x^2;
funcList := {func1, func2}
Manipulate[
 Plot[function[x], {x, -5, 5}], {function, funcList}, {a, -5, 5}, 
 LocalizeVariables -> False, TrackedSymbols :> {a, function}
 ]

变化是:

  1. funcList 改为 {func1, func2}
  2. Plot 表达式已更改为 function[x],从而引用本地 x 变量.
  3. Manipulate 选项 TrackedSymbols :>{a, function} 已添加.
  4. function 最初未设置.
  1. funcList was changed to {func1, func2}
  2. The Plot expression was changed to function[x], thereby referencing the local x variable.
  3. The Manipulate option TrackedSymbols :> {a, function} was added.
  4. function is initially unset.

这篇关于当参数改变时更新 Manipulate[]'d 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆