Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create an image in matlab which contains specified level sets for a complex polynomial. Suppose, for example, I wanted to create an image containing the sets $\{z\in\mathbb{C}:|p(z)|=2\}$, $\{z\in\mathbb{C}:|p(z)|=5\}$, $\{z\in\mathbb{C}:|p(z)|=9.5\}$. Could someone help me out with the code for this? Thanks

share|cite|improve this question
Haha I saw this post and thought, "that's something Trevor would work on." I guess I was right! – Joel Jul 6 '13 at 17:15
Did you intend $\{z\in\mathbb{C}:|p(z)|=2\}$ etc? – Tpofofn Jul 6 '13 at 17:35
@Tpofofn Yes of course. Thanks, I made the edit. – Trevor Richards Jul 6 '13 at 19:13
@Joel You were right. Small world. – Trevor Richards Jul 6 '13 at 19:14
up vote 3 down vote accepted

Taking the Benice equation example from @Joel's answer, in Matlab this can be done simply via the builtin contour or the lower-level contourc functions:

f = @(x,y)(x.^2+y.^2-3).*sqrt(x.^2+y.^2)+0.75+ ...
    sin(8*sqrt(x.^2+y.^2)).*cos(6*atan(y./abs(x))) ...
x = -2:0.01:2;       % x- and y- range, here y = x
[X,Y] = meshgrid(x); % Create mesh, here Y = X.'
Z = f(X,Y);          % Value of Benice equation at mesh points
v = [0 2];           % Heights/values of level set(s) to plot
contour(x,x,Z,v);    % Plot level set(s)
axis equal

As the help/documentation points out, the key is the fourth argument to contour and contourc functions, v. If you want plot just one level set, w, then you must set v = [w w].

More on creating contour plots with Matlab.

share|cite|improve this answer

Not exactly Matlab, but gnuplot is a free professional graphing utility that can graph level curves, and produce figures appropriate for latex.

Here is a link with some code for a level curve:

share|cite|improve this answer
Thanks, I will give it a try. – Trevor Richards Jul 6 '13 at 19:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.