关于绘图过程 - 关于“Mathematica 8 中函数声明的问题”的进一步问题

发布于 2024-11-08 07:07:15 字数 1241 浏览 0 评论 0原文

相关 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/(
4.29 + x)
, and then how its plot 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?

enter image description here

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 技术交流群。

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

发布评论

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

评论(3

将军与妓 2024-11-15 07:07:30

定义:

functionB2[x_] := model /. fit

是一条指示 Mathematica 的指令,将所有未来出现的类似 functionB2[x_] 的表达式替换为用参数值替换每次出现的 x 的结果。表达式 model / 中的 code>。适合。但是在model / 中没有出现x。 fit:该表达式中唯一的符号是 modelfit(从技术上讲,是 ReplaceAll)。因此,定义返回一个固定结果,model /。适合,无论参数如何。事实上,定义可以简单地是:

functionB2a[] := model /. fit

如果绘制 functionB2a[],您将得到与绘制 functionB2[anything] 相同的结果。为什么?因为 Plot 将计算该表达式,同时在绘图范围内改变符号 x。碰巧 model /. fit 计算结果为涉及该符号的表达式,因此您可以得到所展示的图。

现在考虑functionB1

functionB1[x_] = model /. fit

它也表示要替换右侧所有出现的x——但这次右侧是在之前< /em> 定义成立。评估模型/的结果。 fit 是一个确实包含符号x的表达式,因此现在定义对传递的参数值敏感。最终结果就像函数是这样定义的:

functionB1a[x_] := 4/Sqrt[3]-0.335/(0.435+x)^2+0.347/(0.712+x)^4-0.27/(4.29+x)

因此,如果您绘制 functionB1[Sqrt[x]]Plot 命令将看到表达式:

4/Sqrt[3]-0.335/(0.435 +Sqrt[x])^2+0.347/(0.712 +Sqrt[x])^4-0.27/(4.29 +Sqrt[x])

形式符号

使用SetDelayed建立定义时,形式参数的名称(本例中为x)独立于同一符号在定义之外的任何出现。定义。这样的定义可以使用任何其他符号,并且仍然生成相同的结果。另一方面,使用 Set 建立的定义(例如 functionB1依赖包含相同符号的右侧的求值结果作为正式论证。这可能是微妙错误的根源,因为必须小心不要使用意外具有预先存在的下值的符号。使用形式符号(在字母和类似字母的形式中描述)进行论证名称可以帮助解决这个问题。

The definition:

functionB2[x_] := model /. fit

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 of x in the expression model /. fit. But there are no occurrences of x in model /. fit: the only symbols in that expression are model and fit (and, technically, ReplaceAll). Therefore, the definition returns a fixed result, model /. fit, irrespective of the argument. Indeed, the definition could just simply be:

functionB2a[] := model /. fit

If you plot functionB2a[], you will get the same result as if you plotted functionB2[anything]. Why? Because Plot will evaluate that expression while varying the symbol x over the plot range. It so happens that model /. fit evaluates to an expression involving that symbol, so you get the exhibited plot.

Now consider functionB1:

functionB1[x_] = model /. fit

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 evaluating model /. fit is an expression that does contain the symbol x, so now the definition is sensitive to the passed argument value. The net result is as if the function were defined thus:

functionB1a[x_] := 4/Sqrt[3]-0.335/(0.435+x)^2+0.347/(0.712+x)^4-0.27/(4.29+x)

So, if you plot functionB1[Sqrt[x]], the Plot command will see the expression:

4/Sqrt[3]-0.335/(0.435 +Sqrt[x])^2+0.347/(0.712 +Sqrt[x])^4-0.27/(4.29 +Sqrt[x])

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 using Set (such as functionB1) 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.

胡渣熟男 2024-11-15 07:07:30

你可以通过尝试来理解发生了什么:

Table[functionB2[Sqrt[y]],{y,0.5,.5,.5}]
Table[functionB2[Sqrt[x]],{x,0.5,.5,.5}]
(*
{4/Sqrt[3] - 0.335/(0.435+ x)^2 + 0.347/(0.712+ x)^4 - 0.27/(4.29+ x)}
{2.03065}
*)

被替换的是 functionB2 定义中的 x,而不是形式参数。

编辑

您得到的情节不是您想要的。 Sqrt[x]functionB2[...] 中被忽略,隐式 x 被替换,如下所示:

在此处输入图像描述

You can understand what is going on by trying:

Table[functionB2[Sqrt[y]],{y,0.5,.5,.5}]
Table[functionB2[Sqrt[x]],{x,0.5,.5,.5}]
(*
{4/Sqrt[3] - 0.335/(0.435+ x)^2 + 0.347/(0.712+ x)^4 - 0.27/(4.29+ x)}
{2.03065}
*)

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 in functionB2[...] and the implicit x is replaced, as you can see here:

enter image description here

初见终念 2024-11-15 07:07:29

SetDelayed 充当作用域构造。如有必要,参数会被本地化。任何显式匹配参数的变量都绑定在此范围内,其他变量则不然。

In[78]:= a[x_] := x^2 + b
         b = x^4;

(* the first x^2 is explicitly present and bound to the argument. 
   The x in x^4 present via b is not bound *)

In[80]:= a[x]

Out[80]= x^2 + x^4 (* this is what you would expect *)

In[81]:= a[y]

Out[81]= x^4 + y^2 (* surprise *)

In[82]:= a[1]

Out[82]= 1 + x^4 (* surprise *)

因此,您可以做的是以下两件事之一:

  • 使用 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 的定义,任何参数值都会产生相同的结果,如上所述:

In[93]:= functionB2[1]

Out[93]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + 
   x)^4 - 0.27/(4.29 + x)

In[94]:= functionB2["Even a string yields the same ouput"]

Out[94]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + 
   x)^4 - 0.27/(4.29 + x)

但是,此表达式包含 x,因此如果我们为 x 提供数值,它可以获得一个数值:

In[95]:= functionB2["Even a string yields the same ouput"] /. x -> 1

Out[95]= 2.13607

嗯,这基本上是 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.

In[78]:= a[x_] := x^2 + b
         b = x^4;

(* the first x^2 is explicitly present and bound to the argument. 
   The x in x^4 present via b is not bound *)

In[80]:= a[x]

Out[80]= x^2 + x^4 (* this is what you would expect *)

In[81]:= a[y]

Out[81]= x^4 + y^2 (* surprise *)

In[82]:= a[1]

Out[82]= 1 + x^4 (* surprise *)

So, what you could do is one of two things:

  • Use 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:

In[93]:= functionB2[1]

Out[93]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + 
   x)^4 - 0.27/(4.29 + x)

In[94]:= functionB2["Even a string yields the same ouput"]

Out[94]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + 
   x)^4 - 0.27/(4.29 + x)

However, this expression contains x's and therefore it can get a numerical value if we provide a numerical value for x:

In[95]:= functionB2["Even a string yields the same ouput"] /. x -> 1

Out[95]= 2.13607

Well, this basically what Plot does too. This is why you still get a plot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文