1
$\begingroup$

This should have a trivial solution, but how do I plot a list of (complex) numbers in the complex plane? Or, put another way, why doesn't the code below work?

I get the error "... is not a list of numbers or pairs of numbers."

I'm pretty new to Mathematica, so I'm kinda fumbling in the dark. I've taken the last part of the code from here.

EDIT: Rahul found the bug, it's corrected in the code below. However, I only see two points in the resulting plot, instead of the expected four. Why?

k = 2;
S = Sum[Sign[i] x^(n + i), {i, -k, k}];

sol = N[ComplexExpand[FullSimplify[Solve[S == 0, x]]]]
(* {{x -> -1.}, {x -> 1.}, {x -> -0.5 - 0.866025 I}, {x -> -0.5 + 0.866025 I}} *)

p = ListPlot[{Re[#], Im[#]} & /@x /. sol,
   AxesOrigin -> {0, 0},
   PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}},
   ImagePadding -> 40,
   AspectRatio -> 1,
   Frame -> True,
   FrameLabel -> {{Im, None}, {Re, "complex plane"}}, 
   PlotStyle -> Directive[Red, PointSize[.02]]];

Show[p]
$\endgroup$
4
  • 1
    $\begingroup$ sol is not a list of complex numbers. sol is a list of replacement rules, each of which replaces x with a complex number. So you should plot (x /. sol) instead of sol directly. $\endgroup$ – user484 Feb 25 '16 at 20:08
  • $\begingroup$ That did the trick, thanks! $\endgroup$ – Bobson Dugnutt Feb 25 '16 at 20:09
  • $\begingroup$ @Rahul Hmm, that seemed to help, but I only get a plot of two numbers (instead of the four on the list of replacement rules) - do you have an idea what might be going on? $\endgroup$ – Bobson Dugnutt Feb 25 '16 at 20:13
  • $\begingroup$ You need to account for precedence: {Re[#], Im[#]} & /@ (x /. sol). ReplaceAll has lower precedence than Map, so you need some braces here. $\endgroup$ – Yves Klett Feb 26 '16 at 14:19
4
$\begingroup$

First, the expression

    {Re[#], Im[#]} & /@ x /. sol

does not produce the output you seem to think it does, but rather:

{-1., 1., -0.5 - 0.866025 I, -0.5 + 0.866025 I}

As the 3rd and 4th values are not real, they cannot be used as vertical coordinates at horizontal coordinates 3 and 4.

You could use instead:

    {Re[#], Im[#]} & /@ (x /. sol)
(* {{-1., 0}, {1., 0}, {-0.5, -0.866025}, {-0.5, 0.866025}} *)

Which would serve as the correct point data for ListPlot.

It's easier to do the whole thing in a more direct way, with Graphics along with Point.

Second, at least since Mathematica version 10.1, you have the built-in function ReIm to use in place of the {Re[#], Im[#]} & /@ expression.

Third, it's been a long time since one needed to use Show like that: omit the semicolon at the end of the graphics expression and get the graphics as direct output.

    Graphics[{Red, PointSize[0.02], Point@ReIm[x /. sol]}, 
        AxesOrigin -> {0, 0}, PlotRange -> {{-1.2, 1.2}, {-1.2, 1.2}}, 
        ImagePadding -> 40, AspectRatio -> 1, Frame -> True, 
        FrameLabel -> {{Im, None}, {Re, "complex plane"}}]

enter image description here

$\endgroup$
5
  • $\begingroup$ Thanks! However, this gives the following error: Coordinate $CellContextReIm[{-1, 1, -0.5 + Complex[0, -0.5] 1.7320508075688772, -0.5 + Complex[0, 0.5] 1.7320508075688772`}] should be a pair of numbers, or a Scaled or Offset form. What am I doing wrong? (I've got version 10.0) $\endgroup$ – Bobson Dugnutt Feb 25 '16 at 20:29
  • $\begingroup$ What version of Mathematica are you using? $\endgroup$ – murray Feb 25 '16 at 20:40
  • $\begingroup$ My version doesn't support ReIm. I don't seem to be able to just replace "Point@ReIm[x /. sol]" with "Point@{Re[#], Im[#]} & /@ x /. sol" - is this how I should do it? Again, sorry for these probably dumb questions, but I don't really understand the syntax yet. $\endgroup$ – Bobson Dugnutt Feb 25 '16 at 20:49
  • $\begingroup$ @Lovsovs: Note carefully the presence of parentheses around (x /. sol) in my comment and in the first half of murray's answer. $\endgroup$ – user484 Feb 25 '16 at 20:54
  • $\begingroup$ @Rahul Yup, that did it! $\endgroup$ – Bobson Dugnutt Feb 25 '16 at 21:00
3
$\begingroup$

V10.1 introduced ReIm, which make this kind of plot a little easier to create. Also, since FullSimplify, ComplexExpand and N aren't needed, the code can be reduced to:

Module[{k = 2, sum, pts},
  sum = Sum[Sign[i] x^(n + i), {i, -k, k}];
  pts = ReIm /@ Solve[sum == 0, x][[All, 1, 2]];
  Graphics[{Red, AbsolutePointSize[6], Point[pts]},
    PlotRangePadding -> .1,
    Frame -> True]]

plot

$\endgroup$

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