Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have some larger square, Hermitian matrices, M, (dimension 20, say, much more than 4) with 2 independent variables, call them x and y.

I can plot the eigenvalues as functions of x and y with:

Plot3D[Eigenvalues[M], {x,-1,1}, {y,-1,1}]

even though I obviously have no analytic expressions for the eigenvalues as functions of x and y.

I am interested in the nature of certain contours of these functions, let's say their intersections with zero.


ContourPlot[Eigenvalues[M], {x-1,1}, {y,-1,1}]

will give me plots (e.g. http://i.imgur.com/g9GgFSy.png?1)

and in fact, the zero shown there was from a mouse-over of the central contour (cursor not present in image).

So Mathematica is clearly able to plot the contours, even the analytic zero contour.

However, if I change to

ContourPlot[Eigenvalues[M] == 0, {x-1,1}, {y,-1,1}]

I will get errors about "Mathematica can't find all the roots of the characteristic polynomial". This simple change of commands has apparently really altered how Mathematica is going about evaluating it and now it is trying (and of course failing) to solve an order-20 polynomial for analytic roots.

But from the regular contour plot, I can see that it should be capable of giving me the numerical zero contour.

So how can I get Mathematica to weaken the equality condition and give me the numerical zero contour approximately?


Perhaps there is another way altogether to extract the single contour I need from the 3D plot or the full ContourPlot?

share|improve this question
See this mathematica.stackexchange.com/questions/20281/…. I have used it and works. –  caya Jan 6 at 22:02
Forgot to say that there is the caveat that the orientation wasn't uniform (some lines from left to right, others from right to left - kind of, you get the point) and I fixed that by hand based on my particular case. –  caya Jan 6 at 22:08
Related: (23363), (32681), (32734) –  Mr.Wizard Jan 6 at 23:18
Please provide a "representative" example matrix. –  Daniel Lichtblau Jan 7 at 3:36
add comment

2 Answers

In such cases, you can plot just the contour that you want (i.e. without == 0) and remove the colored polygons in a post-processing step:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, MaxRecursion -> 3,
    Contours -> {0}, ContourStyle -> Black, Mesh -> None] /. _Polygon -> Sequence[]

enter image description here

share|improve this answer
Based on the three questions I linked as related above, do you feel that this question is sufficiently different to justify not being closed as a duplicate? –  Mr.Wizard Jan 6 at 23:19
@Mr.Wizard Without OP's actual matrix it's hard to say, but perhaps a little different: In all the other three questions, the issue is some variant of "function only touches 0 but doesn't cross it". I don't think that's the problem here. According to OP, their function just won't evaluate (or has trouble evaluating) with an == 0 sign, but it's OK without it. Given this scenario, they want to be able to plot a single contour without using == 0. I'm not entirely sure if their problem is solved by the other answers, but I certainly won't complain if this is closed as a duplicate :) –  rm -rf Jan 7 at 0:00
Okay. +1 for a nice concise solution to an apparently different problem. :-) –  Mr.Wizard Jan 7 at 2:01
As an alternative to /. _Polygon -> Sequence[] you could use the Option ColorFunction -> (None &). –  Mr.Wizard Jan 7 at 2:03
I didn't post the actual matrix only because it's such a mess and I think the problem is more general. It seems that without the ==0 addition, mathematica uses specific values of the parameters x and y and then finds the eigenvalues (real numbers) and plots them. No problem. But with the addition of the ==0, it seems that it tries to evaluate the plot argument functions (Eigenvalues[M]) first, so of course we get 20 Root[] type answers that can't be found analytically, and it gives errors. –  Steve Jan 7 at 17:49
show 7 more comments

rm -rf provided a solution to the problem which works. Thanks very much to him.

Sort[Eigenvalues[M]][[s]], {x, -1,1}, {y, -1,1}, Contours -> {w},
PlotRange -> {{-1, 1}, {-1,1}}] /. _Polygon -> Sequence[]
, {s, 1, Dimensions[M][[1]]}

Will plot the contours of all the eiegenvalues of M with height w (with w=0 we get the special case I asked for here)

share|improve this answer
add comment

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.