1

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.

Furthermore:

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

will give me plots (e.g. https://i.stack.imgur.com/Q1pew.png)

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?

or

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Steve
  • 1,153
  • 7
  • 17

2 Answers2

3

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

rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • 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 06 '14 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 07 '14 at 00:00
  • Okay. +1 for a nice concise solution to an apparently different problem. :-) – Mr.Wizard Jan 07 '14 at 02:01
  • As an alternative to /. _Polygon -> Sequence[] you could use the Option ColorFunction -> (None &). – Mr.Wizard Jan 07 '14 at 02: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 07 '14 at 17:49
  • Is there a way to use Evaluate, Hold, etc. type commands to force it to function as it does without the ==0 addition (properly) but with the ==0? – Steve Jan 07 '14 at 17:49
  • rm -rf, could you explain the last part of your solution, /. _Polygon -> Sequence[] – Steve Jan 07 '14 at 17:56
  • @Steve That simply removes the colored polygons, so that you're only left with the contour line. Try removing that part to see what you get without it. As for the remaining comments, I'm afraid I can't say much without an example. Try to see if you can post the equation for a 3x3 matrix instead of a 20x20. – rm -rf Jan 07 '14 at 18:20
  • Thank you for responding, and for your original help. I've posted my own answer now to the original problem, which uses your solution. Thanks! – Steve Jan 07 '14 at 20:50
  • @Steve It's not necessary to copy my answer and repost it... What we really need is an example of your M matrix (or the actual matrix itself), so that people can try it out and reproduce the problem and the solution. Without it, your answer is just a copy and an unhelpful one at that as well. Please try to either come up with a minimal problem or link to your data. – rm -rf Jan 07 '14 at 20:56
  • Is there a reason not to use ContourShading -> None to remove the polygons? – Simon Woods Jan 07 '14 at 21:15
  • @SimonWoods Does memory loss count as a valid reason? ;) – rm -rf Jan 07 '14 at 21:21
0

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

Show[Table[
ContourPlot[
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)

Steve
  • 1,153
  • 7
  • 17