3
$\begingroup$

Consider this example:

f[x_] := -I*2*x + 3*x^2

which is a complex function defined on a complex plane $x=x_r+ix_i$

It is convenient to plot the contour of its real part Re[f] with

ListContourPlot[Re[Table[ComplexExpand[f[xr + I*xi]], {xr, 0, 0.2, 0.01}, {xi, -0.05,
 0.05, 0.001}]]]

But sometimes we want to only show the contour for a certain value with a legend for the specific value. For example, how can I only plot the contour for Re[f]=0 with a legend for it?

I naively tried as follows, but it didn't work.

ListContourPlot[Re[Table[ComplexExpand[f[xr + I*xi]], {xr, 0, 0.2, 0.01}, {xi, -0.05,
  0.05, 0.001}]] == 0]

Thank you for any help.

$\endgroup$
  • $\begingroup$ How about ContourPlot[ Re[ComplexExpand[f[xr + I*xi]]] == 0, {xr, 0, 0.2}, {xi, -0.05, 0.05}] $\endgroup$ – chris Jul 5 at 11:42
  • $\begingroup$ @Chris, it can do this, but for some cases when we do not have an analytical function $f$, it is expected that we can plot with ListContourPlot. $\endgroup$ – user55777 Jul 5 at 11:57
2
$\begingroup$

This should work

  ff = Re[Table[{xr, xi, ComplexExpand[f[xr + I*xi]]}, {xr, 0, 0.2, 
  0.01}, {xi, -0.05, 0.05, 0.001}]] // Flatten[#, 1] & //Interpolation

enter image description here

Then

  ContourPlot[ff[xr, xi] == 0, {xr, 0, 0.2}, {xi, -0.05, 0.05}]

enter image description here

As requested by OP, 'ff' could be written as

   ff = Interpolation [Flatten[Re[Table[{xr, xi, ComplexExpand[f[xr +I*xi]]}, 
  {xr, 0, 0.2,  0.01}, 
 {xi, -0.05, 0.05, 0.001}]], 1]]
| improve this answer | |
$\endgroup$
  • $\begingroup$ thanks! It works. You created a pure function. Well, could you make it without the postfix form using //Interpolation if it is possible? $\endgroup$ – user55777 Jul 5 at 13:09
4
$\begingroup$
f[x_] := -I*2*x + 3*x^2

Since you are taking the real part, ComplexExpand is not necessary. To get a specific contour, just specify the contour.

ListContourPlot[
 Table[{xr, xi, Re@f[xr + I*xi]},
   {xr, 0, 0.2, 0.01}, {xi, -0.05, 0.05, 0.001}] //
  Flatten[#, 1] &,
 Contours -> {{0}},
 ContourShading -> None]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ your comment and method are useful as well. But I cannot accept both. I accepted chris' answer because that solved my problem earlier. Sorry about that. $\endgroup$ – user55777 Jul 5 at 14:32
  • $\begingroup$ @user55777 you could still upvote this answer? his answer is better since it doesn't rely on unnecessary interpolation. $\endgroup$ – chris Jul 5 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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