Mathematica 8中带有函数声明的问题 [英] A problem in Mathematica 8 with function declaration

查看:132
本文介绍了Mathematica 8中带有函数声明的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中,将函数定义为"functionB"的结果很奇怪.有人可以解释吗?我想绘制functionB[x]functionB[Sqrt[x]],它们必须不同,但是此代码显示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]必须与functionB[Sqrt[x]]不同,但是在这种情况下,两行相同(这是不正确的).

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

推荐答案

如果尝试使用?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).

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

这与使用SetDelayed(即:=)有关.每当Mathematica看到f[_]模式时,都会重新评估functionB[x_]:=model/.fit的rhs.您已将模式命名为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.

您想要的可以通过例如functionC[x_] = model /. fit.也就是说,通过使用Set(=)而不是SetDelayed(:=)来评估rhs.

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

这篇关于Mathematica 8中带有函数声明的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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