关于绘图过程 - 关于“Mathematica 8 中函数声明的问题”的进一步问题
Clear["Global`*"]
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};
functionB1[x_] = model /. fit;
functionB2[x_] := model /. fit;
评估差异functionB1 和 functionB2 之间的关系可以通过 mma 中的 Trace
命令显示,如下所示:
functionB1[Sqrt[0.2]] // Trace
functionB2[Sqrt[0.2]] // Trace
我对 functionB1 没有疑问。 令我困惑的是,因为 functionB2[Sqrt[0.2]]
甚至没有给出数字结果,而是给出 x 4/Sqrt[3] 的函数- 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/( 4.29 + x)
,那么它的绘图 Plot[functionB2[Sqrt[x]], {x, 0, 1}]
是如何可能的呢?
我的意思是,当你运行 Plot[functionB2[Sqrt[x]], {x, 0, 1}]
时,mma 内部发生的事情是:
x 接受一个数字,比如说 0.2,然后 0.2 最终是传递给 functionB2,但 functionB2 给出的是一个函数,而不是一个数字。那么下图是如何生成的呢?
及其跟踪结果 ( Plot[functionB2[Sqrt[x]], {x, 0 , 1}] // Trace
) 看起来非常不可读。我想知道functionB2的清晰绘图过程。有人可以展示一下吗?
谢谢~:)
Related A problem in Mathematica 8 with function declaration
Clear["Global`*"]
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};
functionB1[x_] = model /. fit;
functionB2[x_] := model /. fit;
The evaluation difference between functionB1 and functionB2 can be revealed by Trace
command in mma, as below:
functionB1[Sqrt[0.2]] // Trace
functionB2[Sqrt[0.2]] // Trace
I have no question about functionB1. what puzzles me is that because functionB2[Sqrt[0.2]]
doesn't even gives a numeric result but gives a function of x 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(
, and then how its plot
4.29 + x)Plot[functionB2[Sqrt[x]], {x, 0, 1}]
is possible?
I mean when you run Plot[functionB2[Sqrt[x]], {x, 0, 1}]
, what happens inside mma is:
x takes a number, say, 0.2, then 0.2 is finally passed to functionB2, but functionB2 gives a function, not a number. Then how is the following figure generated?
And its trace result ( Plot[functionB2[Sqrt[x]], {x, 0, 1}] // Trace
) seems very unreadable. I wonder the clear plotting process of functionB2. Can anybody show it?
thanks~ :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
定义:
是一条指示 Mathematica 的指令,将所有未来出现的类似
functionB2[x_]
的表达式替换为用参数值替换每次出现的x
的结果。表达式model / 中的 code>。适合。但是在
model / 中没有出现
:该表达式中唯一的符号是x
。 fitmodel
和fit
(从技术上讲,是ReplaceAll
)。因此,定义返回一个固定结果,model /。适合
,无论参数如何。事实上,定义可以简单地是:如果绘制
functionB2a[]
,您将得到与绘制functionB2[anything]
相同的结果。为什么?因为Plot
将计算该表达式,同时在绘图范围内改变符号x
。碰巧model /. fit
计算结果为涉及该符号的表达式,因此您可以得到所展示的图。现在考虑
functionB1
:它也表示要替换右侧所有出现的
x
——但这次右侧是在之前< /em> 定义成立。评估模型/的结果。 fit
是一个确实包含符号x
的表达式,因此现在定义对传递的参数值敏感。最终结果就像函数是这样定义的:因此,如果您绘制
functionB1[Sqrt[x]]
,Plot
命令将看到表达式:形式符号
使用
SetDelayed
建立定义时,形式参数的名称(本例中为x
)独立于同一符号在定义之外的任何出现。定义。这样的定义可以使用任何其他符号,并且仍然生成相同的结果。另一方面,使用Set
建立的定义(例如functionB1
)依赖包含相同符号的右侧的求值结果作为正式论证。这可能是微妙错误的根源,因为必须小心不要使用意外具有预先存在的下值的符号。使用形式符号(在字母和类似字母的形式中描述)进行论证名称可以帮助解决这个问题。The definition:
is an instruction to Mathematica to replace all future occurrences of an expression that looks like
functionB2[x_]
with the result of substituting the value of the argument for every occurrence ofx
in the expressionmodel /. fit
. But there are no occurrences ofx
inmodel /. fit
: the only symbols in that expression aremodel
andfit
(and, technically,ReplaceAll
). Therefore, the definition returns a fixed result,model /. fit
, irrespective of the argument. Indeed, the definition could just simply be:If you plot
functionB2a[]
, you will get the same result as if you plottedfunctionB2[anything]
. Why? BecausePlot
will evaluate that expression while varying the symbolx
over the plot range. It so happens thatmodel /. fit
evaluates to an expression involving that symbol, so you get the exhibited plot.Now consider
functionB1
:It too says to replace all occurrences of
x
on the right-hand side -- but this time the right-hand side is evaluated before the definition is established. The result of evaluatingmodel /. fit
is an expression that does contain the symbolx
, so now the definition is sensitive to the passed argument value. The net result is as if the function were defined thus:So, if you plot
functionB1[Sqrt[x]]
, thePlot
command will see the expression:Formal Symbols
When establishing definitions using
SetDelayed
, the name of the formal argument (x
in this case) is independent of any occurrences of the same symbol outside of the definition. Such definitions can use any other symbol, and still generate the same result. On the other hand, definitions established usingSet
(such asfunctionB1
) rely on the result of evaluating the right-hand side containing the same symbol as the formal argument. This can be a source of subtle bugs as one must take care not use symbols that accidentally have pre-existing down-values. The use of formal symbols (described in Letters and Letter-like Forms) for argument names can help manage this problem.你可以通过尝试来理解发生了什么:
被替换的是 functionB2 定义中的 x,而不是形式参数。
编辑
您得到的情节不是您想要的。
Sqrt[x]
在functionB2[...]
中被忽略,隐式 x 被替换,如下所示:You can understand what is going on by trying:
What is getting replaced is the
x
inside the definition of functionB2, not a formal argument.Edit
The plot you are getting is not what you want. The
Sqrt[x]
is disregarded infunctionB2[...]
and the implicit x is replaced, as you can see here:SetDelayed
充当作用域构造。如有必要,参数会被本地化。任何显式匹配参数的变量都绑定在此范围内,其他变量则不然。因此,您可以做的是以下两件事之一:
Evaluate
:functionB2[x_] := Evaluate[model /. fit];
使
model
对 x 的依赖显式:In[68]:= model2[x_] =
4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
In[69]:= functionB3[x_] := model2[x] /。适合;
In[85]:= functionB3[Sqrt[0.2]]
Out[85]= 2.01415
编辑因为问题更新
由于您对 functionB2 的定义,任何参数值都会产生相同的结果,如上所述:
但是,此表达式包含 x,因此如果我们为 x 提供数值,它可以获得一个数值:
嗯,这基本上是
Plot
也是如此。这就是为什么你仍然会得到一个情节。SetDelayed
acts as a scoping construction. Arguments are localized if necessary. Any variables that explicitly match the arguments are bound within this scope, others are not.So, what you could do is one of two things:
Evaluate
:functionB2[x_] := Evaluate[model /. fit];
Make dependence of
model
on x explicit:In[68]:= model2[x_] =
4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
In[69]:= functionB3[x_] := model2[x] /. fit;
In[85]:= functionB3[Sqrt[0.2]]
Out[85]= 2.01415
Edit because of question update
Because of your definition of functionB2 any argument value yields the same result, as explained above:
However, this expression contains x's and therefore it can get a numerical value if we provide a numerical value for x:
Well, this basically what
Plot
does too. This is why you still get a plot.