Mathematica 8 中函数声明的问题
这是一个奇怪的结果,在此示例中函数定义为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试
?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 asfunctionB[x_]:=model/.fit
. Thus, whenever you now havefunctionB[y]
, for anyy
, Mathematica evaluatesmodel/.fit
, obtaining4/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 offunctionB[x_]:=model/.fit
is evaluated anew each time Mathematica sees the patternf[_]
. That you have named the patternx
is irrelevant.What you want could be achieved by e.g.
functionC[x_] = model /. fit
. That is, by usingSet
(=
) rather thanSetDelayed
(:=
), so as to evaluate the rhs.Hope this is clear enough (it probably isn't)...
您可能想尝试在 functionB 中定义模型,因此两个地方的 x 都是相关的:
You might want to try defining the model inside functionB so x in both places are related: