contour_plot/plot and function arguments Anonymous

Hello,

I am considering switching from Mathematica to Sage but I am confused on some fundamental matters pertaining Sage. I really appreciate if you could shed some light on the problem .

Let's look at the code in Sage:

reset()
f(k1,k2) = k1^2 + k2
contour_plot(f,(0,2),(3,5))


this code beautifully draws the contour. Now, what I tried was switching the order of the arguments of f from f(k1,k2) to f(k2,k1):

f(k2,k1)=k1^2+k2


What I intuitively expected was the switch of the graph. I expected (0,2) to match k2 instead of k1 since it is the first argument of the new function f and (3,5) to k1 instead of k2 (for the same reasoning). However, the graph was identical to that of f(k1,k2). How does Sage choose range arguments to match with f arguments?

I have a similar problem with plot as well:

t=var('t')
f(z)=z^2
plot(f(z),(t,0,5))


This code draws the graph, but the function is in z and the range variable is in t. Shouldn't it give an error?! This code is wrong in Mathematica

f[z_]=z^2
Plot[f[z],{t,0,5}]


it should be Plot[f[t],{t,0,5}]. Why doesn't Sage give an error?

edit retag close merge delete

Sort by » oldest newest most voted

When ranges are given without explicit variables, the plotting routine creates a list of function arguments in alphabetical order of variable names, and the ranges are then assigned in the same order. You can (and probably should) override this automatic behavior by always specifying names: contour_plot(f,(k1,0,2),(k2,3,5)) will then differ from contour_plot(f,(k2,0,2),(k1,3,5)) as expected.

The missing error with plot occurs because the routine sets a flag expect_one_var=True. This is meant to be a convenience that makes plotting a bit easier by suppressing a different error message. Unfortunately it creates the confusion you've noticed.

more