General::ivar: ... is not a valid variable
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?
I have even tried running the lines one by one and the errors only show up when the final 开发者_Python百科line (show) is executed.
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.
精彩评论