Home

Taming complex numbers in Grapher.app

Sep 24, 2008

Of all the free extras that Mac OS X has, Grapher has to be one of the coolest. This little app, hidden away in the Applications/Utilities folder, is a powerful graphing tool for mathematical equations and data sets.

As you might expect from Apple, it typesets symbolic math beautifully and produces smooth, anti-aliased graphs. But this isn't just a little tech demo to showcase some of OS X's technologies: Grapher's features blow away your crusty old TI-83, and it comes with its own set of surprises. For example, not only can you save graphs as PDF or EPS, but it can export animations and even doubles as a LaTeX formula editor.

In fact, it does so much that its main weakness is the documentation, which only covers the very basics. The best way to learn Grapher is to look at the handful of included examples, although it might take you a while to find out how to replicate them from scratch.

The other day I needed to quickly graph a couple of things involving complex numbers, and it seemed that Grapher was doing some very freaky shit. Either that, or my math was really rusty. It turned out I'm not as stupid as I thought, and there are some weird caveats with using complex numbers in Grapher. Oddly, there is very little information online about it, so I figured for future reference, I should document the workarounds I discovered.

Let's dive in. Fuck MS Paint, I've got math to do.

(Note: this post assumes you know and like math.)

Refresher

To type formulas into Grapher, you can use the symbol palette, available in the Window menu, or type away using various keyboard shortcuts:

  • Type ^ for exponents, _ for indices, / for fractions. Grapher understands exponents and other notations, for example the Bessel functions Jn(x).
  • Use the arrow keys to move around the equation: in and out of parentheses, exponents, fractions, etc. Pay attention to the cursor to see where you're typing.
  • Type out greek letter names for the symbols: alpha, omega, pi.
  • Common mathematical constants work: e, π, i.
  • The very useful 'Copy LaTeX expression' command is hidden away in the editor's right-click menu.

Using complex numbers

At first sight, complex numbers 'just work'. Using i as the imaginary unit, you can use numbers like 1 + 2i or plot graphs like y=eix. You can use the Re() and Im() operators to explicitly extract the real or imaginary part of a complex number and use abs() and arg() to extract the modulus and argument. If an expression's result is complex, Grapher will only plot the real part.

This last bit is where things get tricky, because this silent casting of complex numbers to reals also sometimes happens in intermediate values.

Silent truncation

Let's plot a complex parametric curve directly using formulas of the form x + iy=.... As an example, let's look at this:

These equations are using Euler's formula ei·x = cos x + i·sin x to plot a half circle each. The only difference between the two formulas is that the second one is passing its value through the (useless) function f(t).

Now if we replace ei·x with 1/ei·x = e–i·x = cos x – i·sin x and change f(t) to 1/t, all that should happen is that the graph is mirrored vertically. Instead, this happens:

The blue circle segment is drawn as a broken horizontal line. What's happening is that Grapher is treating the definition f(t) = 1/t as if it said f(t) = 1/Re(t). In other words, it is truncating the complex input of f(t) to a real number.

To fix this, you need to replace the variable t with complex(t). This complex() function is listed in the built-in definitions list in the Help menu, but lacks any documentation. With this fix applied, the graph will plot as expected:

Further tests reveal that complex(t) is in fact equivalent to writing out Re(t) + i·Im(t), thus manually recomposing the complex number from its own real and imaginary parts. If it weren't for the existence of the complex() helper, one might consider this issue a bug. The way it is now, it seems this behaviour is somewhat intentional.

Moral of the story: wrap all your function inputs in complex() to avoid nasty surprises.

Broken built-ins

Another annoying issue is that certain built-in functions don't handle complex inputs. To show this, you can try plotting y=sinh(–i2·x). Mathematically, this is equivalent to plotting y=sinh(x) directly. However the presence of the imaginary unit causes the plot to fail.

As a workaround, you need to define your own functions using known formulas and incorporating the complex() fix.

For example, you might define:
fixsinh(x) = (ecomplex(x) – e-complex(x))/ 2
fixcosh(x) = (ecomplex(x) + e-complex(x))/ 2

Other built-ins are trickier. For example, Γ(z) needs replacing, but mathematically it is defined as an improper integral. Unfortunately, Grapher's integrator doesn't seem to handle the definition for Γ(z) at all — though it's supposed to do improper integrals.

When using built-in definitions, always verify that you're getting the results you need with a simple example.

Math porn

To round this off, here's an example where I use these tricks to plot a Kaiser sampling window and its frequency response:

Happy graphing!

Maths

Oct 01, 2008 Alexa Booth

Math > MSpaint anyday :)
Thanks for the in depth, and geeky read ;)

Graphing is fun again!

Oct 12, 2008 Darren VanBuren

I am a fan of the Grapher app as well. I use it to make graphs for the "Graph of the Week" feature on my wiki. I have done 3D graphs only so far (they look the coolest) for the Graph of the Week.

More Praise for Grapher

Oct 24, 2008 Will Speak

I love Grapher too. I used it to plot the graphs that i used in my Core 3 Maths Coursework that I did for college. It's a great little app, I don't know why it's hidden in the utilities folder.

Oooh talk about rusty

Dec 08, 2008 OldFogey

I could have sworn I had a TI-55, but perhaps it was the TI-57 (LED version) http://en.wikipedia.org/wiki/TI-57

Here comes some more rust:
How do you translate the circle along the y axis?

I can translate along the x axis by adding Xshift
x+iy = e^(it) + Xshift
And adjust the Radius
x+iy = Radius e^(it)

But how do I translate along the y axis?

TIA

@OldFogey

Dec 08, 2008 Steven

To shift the graph, you add an arbitrary number to the equation... an arbitrary complex number.

x+iy = eit + Xshift + Yshift*i

complex numbers in leopard grapher

Mar 04, 2009 Anonymous

hi - i cannot figure out for the life of me how to graph these complex equations.

I would like to graph a complex number like 1+2i, then 2+3i, then the sum of the two of them... on a single graph, then export.

Is this even possible? I can't even figure out how to change the labels for the graph so that the y axis is the imaginary axis. The documentation for this little app from apple sucks big time.

I'd appreciate any help you can give me, I'm tired of trying to draw the graphs out by hand in LineForm, then export, etc.

Implied complex

Mar 04, 2009 Steven

You don't need to enable the complex plane: Grapher.app can do it transparently. Just type "x+iy = 2+i" as an equation, and it will plot that point.

The gotcha is that you have to type "iy" and not "yi" on the left hand side. Otherwise, Grapher will solve it as an implicit equation, but discard the imaginary parts.

When in doubt, look at the built-in equation templates.

Graphing Complex Formula

Oct 29, 2009 Bec

Hey, i have a Maths C assignment where we have to graph stuff on a complex plane, and i have to explore what happens when i change the z and c values of this equation...
z(n)= z(n-1)^3 + z(n-1)^2 + c

but i can't seem to find a way to get the equation to work.
i looked up the examples, but i have no idea if they are the same (i'm only in year 11 so don't know too much about it). any suggestions on how to get it to work?

Recurrence relationship

Oct 29, 2009 Steven

What you've pasted looks like an iteration/recurrence formula, not an equation in the classical sense. It describes how to calculate zn from zn-1 where zi is the i'th element in a series.

e.g.:
c = 0.5 + 0.5i
f(z) = z3 + z2 + c
z1 = 0.1 + 0.2i
z2 = f(z1) = f(0.1 + 0.2i)
z3 = f(z2) = f(f(0.1 + 0.2i))
z4 = f(z3) = f(f(f(0.1 + 0.2i)))
...

What you need to do is examine how the series of zi values changes when you pick a different starting point z1 and a different c.

For example, you could graph a single series of zi's on the complex plane for a given z1 and c. Or you could define a parametric curve of points (a line, a circle, a spiral, etc) in the complex plane, and run its points through the iteration formula one or more times. Or, you could draw a 2D picture by taking every pixel to be a starting point z1 in the complex plane, iterating z up to a certain 'n' (e.g. 10) and plotting a color/brightness based on some property of the resulting zn, like the complex magnitude or argument. As 'n' gets bigger, you can also start to think about what z = limn→∞ zn would be.

Madelbulb Formula

Dec 10, 2009 Paul

Hi - i recently saw this article on mapping a 3D version of a Madelbrot (see here: http://www.skytopia.com/project/fractal/2mandelbulb.html#formula ) and I was wondering if the formula could be mapped in Grapher. My attempts were not successful. Any interest in seeing if this is possible?
Regards-

Iterated functions

Dec 10, 2009 Steven

The Mandelbulb is an iterated escape time fractal, like the Mandelbrot, and hence requires a dedicated renderer. Grapher.app does not support this.

Complex Zeta Function

Feb 07, 2010 armos

I want to plot a 3D graph of the absolute value of the Zeta function.

http://de.wikipedia.org/wiki/Riemannsche_ζ-Funktion

Somehow I dont seem to find anything in the documentation that would be of a great help to do this.

Could you give me a hint?

Thanks,
armos

Post new comment

Note: all posts containing spam will be removed.
The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <b> <dd> <dl> <dt> <i> <li> <ol> <u> <ul> <img> <em> <p> <br> <span> <div> <h2> <h3> <abbr> <small> <table> <tr> <td> <strong> <acronym> <th> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

Recent comments

Images