Ask Your Question
2

contour_plot/plot and function arguments

asked 2017-02-09 16:06:46 -0500

anonymous user

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-02-10 22:23:48 -0500

updated 2017-02-10 22:44:48 -0500

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.

edit flag offensive delete link more

Comments

Thank you very much. I greatly appreciate your answer.

curios_mind gravatar imagecurios_mind ( 2017-02-12 04:22:28 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-02-09 16:06:46 -0500

Seen: 41 times

Last updated: Feb 10