General::ivar: ... 不是有效变量

发布于 2024-09-26 23:33:56 字数 151 浏览 2 评论 0原文

我一直试图破译这个输出的含义,但我似乎无法弄清楚。有人知道这是怎么回事吗?

alt text

我什至尝试过逐行运行这些行,只有当最后一行(显示)为被执行。

I have been trying to decipher what this output means, but I just can't seem to figure it out. Does anybody know what is going on here?

alt text

I have even tried running the lines one by one and the errors only show up when the final line (show) is executed.

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

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

发布评论

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

评论(1

鹊巢 2024-10-03 23:33:56

单独单步执行这些行不会告诉您发生了什么,您必须拆开给您带来麻烦的语句:在本例中,Show[p1, p2[1,1]。就其本身而言,p1Show 都不会给您带来麻烦,因此得出的结论是它一定是 p2[1,1]。这是通过单独运行它而产生的,它会产生相同的错误。

由于 PlotPlot3D 等如何评估函数参数,这会生成错误。一般来说,它们本质上是对函数文本进行替换,并且可能不会扩展函数调用。一个简单的修复方法是重写 p2

p2[x0_, y0_] := Plot3D[Evaluate[p[x, y, x0, y0]], {x, 0, 2}, {y, 0, 2}]

消除错误。 Evaluate 确保在 Plot3D 获取函数之前对函数进行符号求值,从而避免任何误处理。我希望我能更好地了解在这些情况下何时使用 Evaluate,但如果一般来说,如果您从此类绘图函数中收到错误,那么很可能是对该函数的处理不当。

Stepping through the lines individually by themselves would not show you what is going on, you have to take apart the statement that is giving you the trouble: in this case, Show[p1, p2[1,1]. By themselves, neither p1 nor Show should give you trouble, which leads to the conclusion that it must be p2[1,1]. This is born out by running that by itself, which generates the same error.

This generates an error because of how Plot, Plot3D, etc evaluate the function argument. In general, they essentially do a Replace on the text of the function and may not expand function calls. A simple fix is to rewrite p2 as

p2[x0_, y0_] := Plot3D[Evaluate[p[x, y, x0, y0]], {x, 0, 2}, {y, 0, 2}]

which gets rid of the errors. Evaluate ensures that function is evaluated symbolically before Plot3D gets a hold of it avoiding any mishandling. I wish I had a better idea of when to use Evaluate in these cases, but if in general if you are getting errors from a plotting function like these, then it is most likely mishandling the function.

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