Ask Your Question

Plot the level sets of a function

asked 2011-02-04 06:20:26 +0200

updated 2015-01-14 12:01:57 +0200

FrédéricC gravatar image

I'm trying to draw level set of a function f:R^2->R, that is the set of solutions of f(x,y)=h for a given h.

For that purpose I wrote the following

#! /usr/bin/sage -python
# -*- coding: utf8 -*-

from sage.all import *

def level_curve(f,h):
    solutions_list = solve(f==h,y)
return [sol.rhs() for sol in solutions_list]

for g in level_curve(f,3):
print g

print "-----"

for g in level_curve(f,3):
    print g

This works, but I'm not satisfied principally because I got the level sets under the form of a list of functions. Moreover it will not work if the level set is vertical.

Thus I would prefer to get the solution under the form of a parametric curve.

Does Sage provides something for that ?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2011-02-04 07:14:11 +0200

DSM gravatar image

updated 2011-02-04 13:14:55 +0200

I'm not quite sure what you're looking for. If you want to plot the level sets, you have several options,which I've blatantly stolen from the level set examples worksheet by evanmb:

import matplotlib

var("x, y")
f = x+y+2

# manually using implicit plot for particular h values:
p = Graphics()
for h in [-5..5]:
    p += implicit_plot(f==h,(x,-4,4),(y,-4,4))

# using a full contour plot
contour_plot(f,(x,-4,4),(y,-4,4), fill=false, labels=true, contours=10, colorbar=true, 

# maybe filled
contour_plot(f,(x,-4,4),(y,-4,4), fill=True, labels=true, contours=10, label_colors='black',colorbar=true, 

# or 3d

Or is there something more analytic that you're trying to do first? Depending on how complicated your real functions will be, finding a parametric solution might be a challenge.

edit flag offensive delete link more

answered 2011-02-04 08:15:03 +0200

The aim being to show graphics to my students, a graphical solution is sufficient.

Obviously the advantage of an analytic solution is that it can be passed to pstricks and then be included in LaTeX figures in a smoother way than \includegraphics{blabla.png}.

BTW I found \psplotImp

edit flag offensive delete link more


You might look at SageTex for a very easy way to include plots and other Sage output in TeX. Basically, you can simply do something like \sageplot{contour_plot(f,(x,-4,4),(y,-4,4)} inside your tex document and the plot shows up in the pdf.

Jason Grout gravatar imageJason Grout ( 2011-02-09 01:20:18 +0200 )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


Asked: 2011-02-04 06:20:26 +0200

Seen: 2,258 times

Last updated: Feb 04 '11