# How to plot implicit3d plot with level sets in SageMath?

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

plot = Graphics()
plot+=plot3d(1/4*x^2+1/9*y^2,(x,-2,2),(y,-2,2))
for h in [0..5]:
plot+=contour_plot(f,(x,-4,4),(y,-4,4), fill=false, labels=true, contours=10,colorbar=true,cmap=matplotlib.cm.gist_rainbow).show(aspect_ratio=1)
show(plot)


How to achieve this?

edit retag close merge delete

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

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

( 2018-04-11 11:29:11 +0200 )edit
2

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.

( 2018-04-11 11:32:38 +0200 )edit
1

I agree that it is not clear what level sets you're seeking to plot. However, the answers to these questions could be helpful https://ask.sagemath.org/question/919...https://ask.sagemath.org/question/896...

( 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 ?

( 2018-04-12 21:19:01 +0200 )edit

Sort by » oldest newest most voted

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:

p.show(zmin=za, zmax=zb, aspect_ratio=1, viewer='jmol')
p.show(zmin=za, zmax=zb, aspect_ratio=1, viewer='threejs')


For other choices of viewers, see the documentation, by doing one of the following:

p.show?
help(p.show)
browse_sage_doc(p.show)

more

## Stats

Seen: 266 times

Last updated: Apr 15 '18