42
$\begingroup$

Trying to plot with complex quantities seems not to work properly in what I want to accomplish. I would like to know if there is a general rule/way of plotting when you have complex counterparts in your function. I tried looking up ContourPlot and DensityPlot but I only have one single variable as ContourPlot asks for two variables in order to plot. The expression I am trying to plot is as so:

eqn := (25 Pi f I)/(1 + 10 Pi f I)
Plot[eqn,{f,-5,5}]

If there something else that is missing here?

$\endgroup$
6
  • $\begingroup$ Plot displays $\mathbb{R}\to\mathbb{R}$ functions. How is it supposed to interpret I? $\endgroup$
    – rcollyer
    Mar 25, 2012 at 1:25
  • $\begingroup$ Is your variable f supposed to be just real (as suggested by the domain in your Plot expression)? Or do you want it to take more general complex values, too? $\endgroup$
    – murray
    Mar 25, 2012 at 16:45
  • $\begingroup$ @murray: Well the function f is complex valued. It reads as G(f) = (25 Pi f I) / (1 + 10 Pi f I). So, what I was trying to accomplish is plot the spectrum or "Fourier Transform (frequency response)", of the function $g(t)$. Where $f$ just represent the frequency variable from the time-domain. I hope that makes sense to clear up your question. $\endgroup$
    – night owl
    Mar 26, 2012 at 0:07
  • $\begingroup$ @night owl: a typical communication between a mathematician and a non-matthematician? (I think of a complex-valued function of a real variable; you speak about respone of frequency.) But I understand! You do have a function from the real numbers to the complex numbers, so the way to represent it visually is unclear. For things like density plots and contour plots, one is dealing with a domain consisting of pairs of reals or, equivalently,complex numbers and range real numbers. Your situation is precisely the reverse. $\endgroup$
    – murray
    Mar 26, 2012 at 3:55
  • $\begingroup$ @murray: Edit: I meant to say the function G is complex, but can be seen from above. $\endgroup$
    – night owl
    Mar 26, 2012 at 4:45

5 Answers 5

29
$\begingroup$

The way you could use ContourPlot here, assuming your variable f is complex (f == x + I y) :

eqn[x_, y_] := (25 Pi ( x + I y) I)/(1 + 10 Pi ( x + I y) I)

{ContourPlot[Re@eqn[x, y], {x, -1, 1}, {y, -1, 1}, PlotPoints -> 50], 
 ContourPlot[Im@eqn[x, y], {x, -1, 1}, {y, -1, 1}, 
 PlotRange -> {-0.5, 0.5}, PlotPoints -> 50]}

enter image description here

These are respectively real and imaginary parts of the function eqn.

Let's plot the absolute value of eqn :

Plot3D[ Abs[ eqn[x, y]], {x, -1, 1}, {y, -1, 1}, PlotPoints -> 40]

enter image description here

And we complement with the plot of real and imaginary parts of eqn in the real domain :

eqnR[x_] := (25 Pi x I)/(1 + 10 Pi x I)
Plot[{ Tooltip@Re@eqnR[x], Tooltip@Im@eqnR[x]}, {x, -0.25, 0.25}, 
      PlotStyle -> Thick, PlotRange -> All]

enter image description here

$\endgroup$
10
  • $\begingroup$ Nice To know how it would work to do it as a contour and how it looks. But I really just needed it to output a regular curve. I mentioned contour because I had looked up how to plot complex variables. I might have to accept the other answer as to that it what I was more so looking to get. It is to see what the frequency spectrum is of a function. $\endgroup$
    – night owl
    Mar 25, 2012 at 1:55
  • $\begingroup$ I added the real and imaginary parts of eqn in one plot. Tooltips help to distinguish parts of eqn while the mouse pointer is on a given curve. $\endgroup$
    – Artes
    Mar 25, 2012 at 2:42
  • $\begingroup$ You may have won back the title! This is what I got doing it in matlab, and gnu plot, but could not figure it out here. This is what the frequency response should resemble, from my results in the other programs. :). Did you make this possible still using Plot by using the Re@eqnR[x] and Im@eqnR[x].? $\endgroup$
    – night owl
    Mar 25, 2012 at 4:02
  • $\begingroup$ @nightowl I'am not sure what you are asking for. You can remove Tooltip as well if you want, here it is superfluous. $\endgroup$
    – Artes
    Mar 25, 2012 at 14:31
  • 1
    $\begingroup$ I would advise only to study the issue on a case by case basis, otherwise there is no resonable answer. $\endgroup$
    – Artes
    Mar 26, 2012 at 0:32
35
$\begingroup$

The following function gives the complete information for a function $f:\mathbb{C}\mapsto\mathbb{C}$, by giving the absolute value as $z$-coordinate, and the argument as colour:

ComplexFnPlot[f_, range_, options___] := 
  Block[{rangerealvar, rangeimagvar, g}, 
    g[r_, i_] := (f /. range[[1]] :> r + I i);
    Plot3D[Abs@g[rangerealvar, rangeimagvar],
      {rangerealvar, Re@range[[2]], Re@range[[3]]},
      {rangeimagvar, Im@range[[2]], Im@range[[3]]}, options, 
      ColorFunction -> (Hue@Mod[Arg@g[#1, #2]/(2*Pi) + 1, 1] &), 
      ColorFunctionScaling -> False]]

For example, the call

ComplexFnPlot[Gamma[z],{z,-3.5-3.5I,3.5+5.5I},PlotRange->{0,4}]

gives

resulting graphics from the command above

Positive real numbers are red, negative real numbers are cyan. One can e.g. see that the poles of the Gamma function are of order one because going round them you go through the colour cycle just once.

$\endgroup$
2
  • $\begingroup$ Yes, such a plot does encode "complete information". But discerning from such a plot just how the function behaves is problematic. $\endgroup$
    – murray
    Apr 15, 2012 at 14:57
  • 1
    $\begingroup$ @murray: No more problematic than any other information gathered from graphs. Of course, reading from a graph can never replace a proper analysis. However it can give strong hints about what one is likely to find in that analysis. $\endgroup$
    – celtschk
    Apr 15, 2012 at 15:02
14
$\begingroup$

Here are two common ways to visualize complex functions. The first plots the image of a rectangle in the complex plane. The second plots real and imaginary contours on top of one another, illustrating the fact that they meet at right angles.

f[z_] := E^z;
pic1=ParametricPlot[{Re[f[x+I*y]],Im[f[x+I*y]]},
  {x,0,1},{y,0,Pi/2}, ImageSize -> 300];
pic2 = Show[
  ContourPlot[Re[f[x+I*y]],{x,0,3},{y,0,Pi/2},
    ContourShading -> False],
  ContourPlot[Im[f[x+I*y]],{x,0,3},{y,0,Pi/2},
    ContourShading -> False],
    AspectRatio -> Automatic, ImageSize -> 400
];
Row[{pic1,pic2}]

There should be many more examples at the Wolfram Demonstrations site.

enter image description here

$\endgroup$
3
  • $\begingroup$ Such a domain-codomain representation can be made more meaningful by using various dynamic treatments, e.g., allowing the user to move a Locator in the domain and seeing its image in the codomain; or moving (for such a polar plot) the radial segment or the circular arc and seeing the corresponding image in the codomain. And David Park's Presentations application makes doing this easier, since among other things it can directly process geometric objects described in terms of complex numbers without having to separate things into real and complex parts. $\endgroup$
    – murray
    Apr 15, 2012 at 15:03
  • $\begingroup$ @murray Like this one: demonstrations.wolfram.com/…? $\endgroup$ Apr 15, 2012 at 19:15
  • $\begingroup$ no, not quite like your demonstration (except that, like all such, it makes certain curves to curves). The ones by David Park and I (1) use polar as well as rectangular coordinates; either map a Locator point to its image or else vary a parameter to move a curve in domain and its image in codomain; use Presentations for simplicity in programming and true-to-the-spirit direct use of complex numbers and complex-valued curves. $\endgroup$
    – murray
    Apr 16, 2012 at 0:17
13
$\begingroup$

Just use ParametricPlot and split up the real and imaginary parts as shown below:

eqn = (25 Pi f I)/(1 + 10 Pi f I)
ParametricPlot[{Re[eqn], Im[eqn]}, {f, -5, 5}, AspectRatio -> 1]

enter image description here

Note that you can use Set (=) rather than SetDelayed (:=) here.

$\endgroup$
3
  • $\begingroup$ Thanks. When should one use the SetDelayed versus Set preferably. Because I know that I usually use and see others use it when defining independent variables to your function such as Artes has done. This looks a bit different from what I got using a different program. I wonder whats that about? $\endgroup$
    – night owl
    Mar 25, 2012 at 1:57
  • 1
    $\begingroup$ Try this: reference.wolfram.com/mathematica/tutorial/… $\endgroup$
    – Verbeia
    Mar 25, 2012 at 2:19
  • $\begingroup$ Thanks, for that link. That was helpful to distinguish the two. :). $\endgroup$
    – night owl
    Mar 25, 2012 at 3:23
10
$\begingroup$

There are several new functions in M12 that can be used to plot complex values. For functions of a real variable there are ReImPlot and AbsArgPlot. For your function they give:

ReImPlot[eqn, {f, -.25, .25}]

enter image description here

AbsArgPlot[eqn, {f, -.25, .25}, PlotRange->{{-.25,.25},{0, 2.5}}]

enter image description here

For visualizations over a complex domain, you can use ComplexPlot or ComplexPlot3D. Using celtschk's example of Gamma[z]:

ComplexPlot3D[Gamma[z], {z, -3.5 - 3.5 I, 3.5 + 5.5 I}, PlotRange->{0,4}]

enter image description here

$\endgroup$

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