3
$\begingroup$

Suppose I have this equation:

$$ z^2 + 3z + (x^2 + y^2) = 0 $$

I want the real and complex contour plot of $z(x,y)$. Analytically, the real/imaginary boundary is separated by condition $x^2 + y^2 \leq \frac{9}{4}$

The regular contour plot only plots the real parts when I use findRoot:

eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0
ContourPlot[ 
 z /. FindRoot[eqn[x, y, z], {z, 1}], {x, 0, 2}, {y, 0, 2}, 
 PlotLegends -> Automatic]

enter image description here

The darkest purple represents the complex area.

How do I obtain contour plots of real roots of z, imaginary roots of z? Also, if possible combine them into a single graph.

Tried this different approach of letting $z = a + bi$, but doesn't work.

 z = a + b I;
 eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0
    ContourPlot[ 
     z /. FindRoot[Im[eqn[x, y, z]], {b, 1}], {x, 0, 2}, {y, 0, 2}, 
     PlotLegends -> Automatic]

Plots from difficult problem: First one is real, second one is imaginary root contour plot.

enter image description here enter image description here

$\endgroup$
1
  • $\begingroup$ ListContourPlot? $\endgroup$
    – Apple
    Jul 24, 2014 at 12:43

2 Answers 2

2
$\begingroup$

How about using NSolve like this. As you know z roots are 2-sets. but I have tried the one of things.

eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0

opts := Sequence[
   PlotLegends -> Automatic,
   Exclusions -> {x^2 + y^2 == 9/4},
   ExclusionsStyle -> Red,
   ColorFunction -> "SunsetColors"];

rec = ContourPlot[
  Re[z] /. NSolve[eqn[x, y, z], z][[1]], {x, 0, 2}, {y, 0, 2}, 
  Evaluate@opts, Contours -> 10]

Blockquote

imc = ContourPlot[
  Im[z] /. NSolve[eqn[x, y, z], z][[1]], {x, 0, 2}, {y, 0, 2}, 
  Evaluate@opts, Contours -> 10]

Blockquote

ContourPlot[
 If[x^2 + y^2 < 9/4,
  Re[z] /. NSolve[eqn[x, y, z], z][[1]],
  Im[z] /. NSolve[eqn[x, y, z], z][[1]]], {x, 0, 2}, {y, 0, 2}, 
 Evaluate@opts, Contours -> 20
 ]

enter image description here

Plot3D[
 If[x^2 + y^2 < 9/4,
  Re[z] /. NSolve[eqn[x, y, z], z][[1]],
  Im[z] /. NSolve[eqn[x, y, z], z][[1]]], {x, 0, 2}, {y, 0, 2}, 
 Evaluate@opts, Mesh -> 20, MeshFunctions -> {#3 &}
 ]

Blockquote

$\endgroup$
0
$\begingroup$

You can't Plot a complex function. But you can always plot the Re and Im part of the solution.

eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0
sol[x_,y_]=z/.Solve[eqn[x,y,z],z]
ContourPlot[Re[sol[x,y]][[2]],{x,0,2},{y,0,2},MaxRecursion->3,PlotLabel->"Real"]
ContourPlot[Im[sol[x,y]][[2]],{x,0,2},{y,0,2},MaxRecursion->3,PlotLabel->"Imaginary"]

enter image description here

similarly you can also plot the other solution.

EDIT : With FindRoot

eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0
ContourPlot[z /. FindRoot[eqn[x, y, z], {z, 1. I}, MaxIterations -> 100] //Re, {x, 0, 2}, {y, 0, 2},PlotLabel -> "Real"]
ContourPlot[z /. FindRoot[eqn[x, y, z], {z, 1. I}, MaxIterations -> 100] //Im, {x, 0, 2}, {y, 0, 2},PlotLabel -> "Imaginary"]

enter image description here

$\endgroup$
6
  • $\begingroup$ Does this work with FindRoot as well? Because I'm applying it to a more difficult problem that uses FindRoot $\endgroup$
    – user44840
    Jul 24, 2014 at 12:40
  • $\begingroup$ try a complex initial value like 0.+ I in FindRoot. $\endgroup$
    – Sumit
    Jul 24, 2014 at 12:43
  • $\begingroup$ Let's say that I can only use FindRoot for my difficult problem. I applied it to this (simpler)problem, and it doesn't seem to work. eqn[x_, y_, z_] := z^2 + 3 z + x^2 + y^2 == 0 ContourPlot[ z /. FindRoot[eqn[x, y, z], {z, 1.4 I}], {x, 0, 2}, {y, 0, 2}, PlotLegends -> Automatic] $\endgroup$
    – user44840
    Jul 24, 2014 at 12:52
  • $\begingroup$ @user44840 : you didn't mention Re or Im, that's you get the plot for only real region and white space for imaginary portion. Have a look at my modified answer. $\endgroup$
    – Sumit
    Jul 24, 2014 at 13:04
  • $\begingroup$ Hmm that works! But in the imaginary plot, why are there such rough and random contours? How do you separate which is real and imaginary then? (i.e. identify the boundary). I've uploaded pictures of the real and imaginary contour plots. From the real plot, it seems like there's an imaginary root in some regions, while the imaginary contour plot is a mess. $\endgroup$
    – user44840
    Jul 24, 2014 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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