The following code is a bit clumsy, but it constructs a variable number (n) of epicycles using the values from the radii and angularVel tables that follows the path of a parametric given as a sum of sinusoids. This code builds the epicycles which trace the parametric $\gamma(t)=e^{it}+\frac{1}{2}e^{7it}+\frac{i}{3}e^{-17it}$. So, given any function written as the sum of circles:

$z(t)=\sum_{i=1}^{n}R_1e^{ik_nt}$, where $R_n$ and $k_n$ are the radius and angular velocity of each circle respectively, my code will trace its path.

Now, as you can imagine, my goal is to visualize the Fourier Series as the sum of circles (=epicycles), however, for functions of real coefficients the Fourier Series is just the linear combination of sines and cosines alone, which in complex form are expressed like different variations of this: ${e^{ix}-e^{-ix}}$, and in that form I do not know how to feed it to my program.

QUESTION: How would you create a list of radii and angular velocities to create epicycles for something like this $f(x)=x^2$, whose 2nd order fourier series is $-2e^{-ix}-2e^{ix}+\frac{1}{2}e^{-2ix}+\frac{1}{2}e^{2ix}+\frac{\pi^2}{3}$?

This may not exactly be a programming question, I will also try the math forum but seeing the code is important for anyone to help me.

All help is appreciated.

n = 3;
radii = {I/3, 1/2, 1, 15, 18};
angularVel = {-17, 7, 1, -1, -6};

circles = Table[radii[[i]]*E^((angularVel[[i]])*I*t), {i, 1, n}];
circleCoords = 
Table[{N[Re[circles[[i]]]], N[Im[circles[[i]]]]}, {i, 1, n}];
harmonicCircles = 
Table[Sum[circleCoords[[j]], {j, i + 1, n}], {i, 1, n - 1}];
AppendTo[harmonicCircles, {5, 2}];
pt = Sum[circleCoords[[i]], {i, 1, n}];

circlesForGraphic = 
Table[Circle[harmonicCircles[[i]], Abs[radii[[i]]]], {i, 1, n - 1}];
PrependTo[circlesForGraphic, Circle[{0, 0}, radii[[n]]]];

ordering = 
Table[Text[i, harmonicCircles[[i]], Offset[{3, 3}]], {i, 1, n}];
PrependTo[ordering, Text[n, {0, 0}, Offset[{3, 3}]]];

epicyclesGraphic = 
Graphics[{PointSize[0.025], Purple, Thick, Point[{0, 0}], Point[pt],
 ordering, circlesForGraphic}, PlotRange -> Automatic];

var = 400;
data = {};
For[t = 1, t <= var, t = t + 0.1,
AppendTo[data, pt];

shape = ListCurvePathPlot[data, PlotTheme -> "Detailed"];
Dynamic[Show[epicyclesGraphic, shape]]
{Slider[Dynamic[t], {0, 10}], Dynamic[t]}

  • $\begingroup$ I don't understand why you can't plot the second series if you can plot the first. The radii and angular velocities are just {-2, -2, 1/2, 1/2, Pi^2/3} and {-1, 1, -2, 2, 0} respectively. $\endgroup$
    – user484
    Commented Nov 26, 2015 at 9:53
  • $\begingroup$ @Rahul Thank you for your comment. When I run my code for n=5 and the radii and angular velocities that you write, I get a flat line. $f(x)=x^2$ is not periodic, but at least for some part of the real like I suppose my epicycles should be able to follow its path. No? $\endgroup$
    – Mike
    Commented Nov 26, 2015 at 10:49
  • 2
    $\begingroup$ Ah, I see. The code is plotting the image of (an approximation to) $t^2$ in the complex plane, which is a subset of the real line. Try taking the Fourier series of $t + it^2$, whose image is the graph of $y=x^2$. $\endgroup$
    – user484
    Commented Nov 26, 2015 at 16:00
  • $\begingroup$ @Rahul Hi Rahul, I've found some more time to dedicate to to this project and I was wondering. I have written code that builds the epicycles that approximate a function f(x). However, I want to approximate parametrics now, for example the Lissajous Curves look fun: mathworld.wolfram.com/LissajousCurve.html Now, I try to draw this: {2 Sin[(2 t)/3], Sin[t]} curve. It is closed and very simple, however when I get the fourier series of $2sin(2t/3)+i*sin(t)}$, what my code draws does not resemble the specific parametric. Isn't this the way to do it? Thanks in advance for any comment! $\endgroup$
    – Mike
    Commented Dec 8, 2015 at 14:00
  • 1
    $\begingroup$ That's because its period is not $2\pi$. Try plotting $(2\sin 2t, \sin 3t)$. $\endgroup$
    – user484
    Commented Dec 8, 2015 at 14:31

1 Answer 1


Well, I got carried away.

epicycles[f_, t_, n_] := 
 SortBy[Table[{FourierCoefficient[f, t, i], i}, {i, -n, n}], 
        {Abs@Last@# &, -Abs@First@# &}]
es = epicycles[t + I t^2, t, 2]
(* {{(I π^2)/3, 0}, {-3 I, 1}, {-I, -1}, {I, 2}, {0, -2}} *)

epicycleCenters[es_, t_] := 
 Accumulate[ComplexExpand@Through@{Re, Im}@(First@# Exp[I Last@# t]) & /@ es]
epicycleCenters[es, t]
(* {{0, π^2/3}, 
    {3 Sin[t], π^2/3 - 3 Cos[t]}, 
    {2 Sin[t], π^2/3 - 4 Cos[t]}, 
    {2 Sin[t] - Sin[2 t], π^2/3 - 4 Cos[t] + Cos[2 t]}, 
    {2 Sin[t] - Sin[2 t], π^2/3 - 4 Cos[t] + Cos[2 t]}} *)

epicyclePlot[es_, t_] := 
 With[{centers = epicycleCenters[es, t], 
       radii = Append[Abs@*First /@ Rest@es, 0]}, 
  Show[ParametricPlot[Last@epicycleCenters[es, s], {s, 0, 2 π}, 
                      PlotStyle -> Orange], 
                 PointSize[Large], Point[centers], 
                 Thick, Circle @@@ Transpose@{centers, radii}}]]]
Show[epicyclePlot[es, 1.2], PlotRange -> All]

enter image description here

Use a Manipulate[epicyclePlot[es, t], {t, ...}] or Animate[...] to see it in motion.

Here it is with 10th-order Fourier series, so you can see it start to approach a parabola:

Show[epicyclePlot[epicycles[t + I t^2, t, 10], 1.2], PlotRange -> All]

enter image description here

  • $\begingroup$ I've been trying to understand why on earth this isn't printing what I want until your comment made me realize that expressing a function, say, $f(x)=x^2+x+1$ in the real plane looks like $z(t)=t+I*t^2+t+1$ I also like the SortBy function to put the epicycles in increasing order depending on their radius. I'll do that with my tables. I've been asking variations of this question for a few days now and you're the only person with a concrete answer. Thanks a bunch. $\endgroup$
    – Mike
    Commented Nov 27, 2015 at 1:27

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