Mathematica 8 中函数声明的问题

发布于 2024-11-08 05:50:05 字数 618 浏览 3 评论 0原文

这是一个奇怪的结果,在此示例中函数定义为“functionB”。有人可以解释一下吗?我想绘制 functionB[x]functionB[Sqrt[x]],它们必须不同,但此代码显示 functionB[x] = functionB [Sqrt[x]],这是不可能的。

model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435,
    b3 -> 0.712};
functionB[x_] := model /. fit

Show[
 ParametricPlot[{x, functionB[x]}, {x, 0, 1}],
 ParametricPlot[{x, functionB[Sqrt[x]]}, {x, 0, 1}]
 ]

functionB[x] 必须与 functionB[Sqrt[x]] 不同,但在这种情况下,两行是相同的(这是不正确的)。

This is a strange result with a function defined as "functionB" in this example. Can someone explain this? I want to plot functionB[x] and functionB[Sqrt[x]], they must be different, but this code shows that functionB[x] = functionB[Sqrt[x]], which is impossible.

model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435,
    b3 -> 0.712};
functionB[x_] := model /. fit

Show[
 ParametricPlot[{x, functionB[x]}, {x, 0, 1}],
 ParametricPlot[{x, functionB[Sqrt[x]]}, {x, 0, 1}]
 ]

functionB[x] must different from functionB[Sqrt[x]], but in this case, the 2 lines are the same (which is incorrect).

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

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

发布评论

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

评论(2

浪漫人生路 2024-11-15 05:50:05

如果您尝试 ?functionB,您会看到它存储为 functionB[x_]:=model/.fit。因此,只要您现在有了 functionB[y],对于任何 y,Mathematica 都会计算 model/.fit,获得 4/Sqrt [3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(4.29 + x)

这与使用 SetDelayed (即 :=)有关。每次 Mathematica 看到模式 f[_] 时,functionB[x_]:=model/.fit 的 rhs 都会重新计算。您将模式命名为 x 是无关紧要的。

您想要的可以通过例如 functionC[x_] = model / 来实现。适合。即通过使用 Set (=) 而不是 SetDelayed (:=) 来评估右旋。

希望这足够清楚(可能不是)......

If you try ?functionB, you'll see that it is stored as functionB[x_]:=model/.fit. Thus, whenever you now have functionB[y], for any y, Mathematica evaluates model/.fit, obtaining 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(4.29 + x).

This has to do with using SetDelayed (i.e., :=). The rhs of functionB[x_]:=model/.fit is evaluated anew each time Mathematica sees the pattern f[_]. That you have named the pattern x is irrelevant.

What you want could be achieved by e.g. functionC[x_] = model /. fit. That is, by using Set (=) rather than SetDelayed (:=), so as to evaluate the rhs.

Hope this is clear enough (it probably isn't)...

一城柳絮吹成雪 2024-11-15 05:50:05

您可能想尝试在 functionB 中定义模型,因此两个地方的 x 都是相关的:

fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435, b3 -> 0.712};
functionB[x_] := Module[
  {model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4},
  model /. fit
]

You might want to try defining the model inside functionB so x in both places are related:

fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435, b3 -> 0.712};
functionB[x_] := Module[
  {model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4},
  model /. fit
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文