Ask Your Question

How to plot implicit3d plot with level sets in SageMath?

asked 2018-04-10 20:11:52 +0200

daviddglmath gravatar image

updated 2018-04-11 11:28:54 +0200

slelievre gravatar image

I want to plot a quadratic form with its level sets in SageMath. How to do that?

plot = Graphics()
for h in [0..5]:
    plot+=contour_plot(f,(x,-4,4),(y,-4,4), fill=false, labels=true, contours=10,colorbar=true,    

How to achieve this?

edit retag flag offensive close merge delete


I could not upload any image since i dont have enough karma for that. Sorry

daviddglmath gravatar imagedaviddglmath ( 2018-04-10 20:12:25 +0200 )edit

Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2018-04-11 11:29:11 +0200 )edit

Please provide your definition of f. Ideally, one should be able to copy and paste the code you provide in a fresh Sage session, to see what your problem is.

slelievre gravatar imageslelievre ( 2018-04-11 11:32:38 +0200 )edit

I agree that it is not clear what level sets you're seeking to plot. However, the answers to these questions could be helpful

j.c. gravatar imagej.c. ( 2018-04-11 11:55:10 +0200 )edit

Your question has several problems :

  • you do not define $f$
  • you do not use $h$
  • you try to "add" a set of 2D plots to a 3D plot, without any specification of how to do this "addition".

Could you refine (rethink ?) your question ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2018-04-12 21:19:01 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-15 23:46:15 +0200

slelievre gravatar image

Similar questions have been asked on Ask Sage before, and solutions or workarounds were given that work in a general enough case.

Here specifically, the function we study happens to be a quadratic form:

f = lambda x, y: 1/4*x^2 + 1/9*y^2

In this simple special case, the level set of level r^2, defined by

[z == 1/4 * x^2 + 1/9 * y^2, z == r^2]

are ellipses, and can be parametrized as

x = lambda r: lambda t: 2 * r * cos(t)
y = lambda r: lambda t: 3 * r * sin(t)
z = lambda r: lambda t: r^2

and then, defining the x-, y-, z- ranges, min and max as

xab = xa, xb = -9, 9
yab = ya, yb = -13, 13
zab = za, zb = 0, 18

we can plot the function (with some transparency):

p = plot3d(f, xab, yab, zmin=0, zmax=18, opacity=0.5)

and superimpose the desired level sets:

tau = 2*RDF.pi()
for r in range(5):
    p += parametric_plot3d((x(r), y(r), z(r)), (0, tau), color='red')

and finally visualize the combination, either with jmol or threejs:, zmax=zb, aspect_ratio=1, viewer='jmol'), zmax=zb, aspect_ratio=1, viewer='threejs')

For other choices of viewers, see the documentation, by doing one of the following:
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2018-04-10 20:11:52 +0200

Seen: 481 times

Last updated: Apr 15 '18