tags:

views:

57

answers:

1

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.

+2  A: 

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.

rcollyer
Thanks, that worked!
npsken
Good answer, Evaluate is the way to fix this. One comment I would make is that, technically, it is not actually Replace that is effectively used to set values for x and y here. It is Block. That's why this works, despite p not being present in the literal first argument to Plot: e = p^2; Plot[e, {p, 0, 1}]
Andrew Moylan
@Andrew, well I wasn't sure exactly what causes this problem, and it seemed to behave a bit like Replace, but wrapping everything in a Block makes a lot sense both in regards to the error and just good practice.
rcollyer