Revision history [back]

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)