How to plot a complex trigonometric function with a computer?

I want to plot, on the complex plane, $\cos(x+yi)$, where $-\pi\le y\le\pi$. Which software can accomplish this? It is best to use a free software. Please include your script to do this.

More concretely, I want the image of $\cos(x+yi)$ on the complex plane. A point $a+bi$ is placed on the graph if there exist some $x$ and $y$ such that $\cos(x+yi)=a+bi$ and $-\pi\le y\le\pi$. A point $a+bi$ has distance $a$ on the real axis and distance $b$ on the imaginary axis. The set of all such points is the graph I want.

• Can give WolframAlpha or Mathematica a try. – Simply Beautiful Art Oct 11 '16 at 15:11
• @SimpleArt Wolfram Alpha gives the real part and the imaginary part separately. I want the combined graph on the complex plane. And Mathematica is not free. – Zirui Wang Oct 11 '16 at 15:20
• @ZiruiWang I would point out that mathematica online is, to some degree. Lot less power (especially with the "free" version) but should still plot something like this. – Brevan Ellefsen Oct 11 '16 at 15:32
• It's a bit unclear what kind of plot you want, but here's something for Matlab: se.mathworks.com/company/newsletters/articles/…. If that's the kind of thing you're after, see section "Internet Sources" here for more suggestions: users.mai.liu.se/hanlu09/complex. – Hans Lundmark Oct 11 '16 at 16:38
• An option is to switch to a four dimensional universe and plot the quadruples $(x,y,a,b)$, giving a bidimensional variety. You will need a tool like Hyper$_4$Matlab and a 4D printer. :) – Yves Daoust Oct 12 '16 at 12:43

Let's describe the set of values of $\cos(x+iy)$ for $x\in\Bbb R$ and $y\in[-\pi,\pi]$.

We have $\cos(t+is)=\cos t\cosh s-i\sin t\sinh s$, so the images of $t+is$ for a fixed $s$ and a varying $t$, are described by the parametric equation

$$\left\{\begin{matrix}x=\alpha \cos t\\y=\beta\sin t\end{matrix}\right.$$

With $\alpha=\cosh s$ and $\beta=-\sinh s$

This is the equation of an ellipse, with semi-major axis $\alpha$ and semi-minor axis $|\beta|$. The values of $\alpha$ and $\beta$ are restricted: the point with coordinates $(\alpha,\beta)$ lies on a rectangular hyperbola (equation $x^2-y^2=1$, since $\cosh^2 s-\sinh^2 s=1$ for all $s$), and you want also that $s\in[-\pi,\pi]$.

We can further restrict the values of $s$ to $[0,\pi]$, as negative values won't yield more points since $\cos(t-is)=\cos(-t+is)$ and $t$ spans $\Bbb R$.

Lastly, for positive $s$, $\cosh s$ and $\sinh s$ are increasing with $s$. Therefore, the ellipse for $s_1$ lies inside the ellipse for $s_2>s_1$. The ellipses are thus "continuously growing" from the degenerate case $s=0$ (for which the points $\cos(t+0i)=\cos t$ simply describe the segment between points $-1$ and $+1$ in the complex plane), to the larger one for $s=\pi$.

All in all, the values of $\cos(t+is)$ for $t\in\Bbb R$ and $s\in[-\pi,\pi]$ describe exactly the ellipse $x=\cosh\pi\cos t,y=\sinh \pi\sin t$, and along with all points in its interior.

Now that we know what we are after, it's easy to plot with WolframAlpha: it's simply the set described by the inequation

$$\left(\frac{x}{\cosh\pi}\right)^2+\left(\frac{y}{\sinh\pi}\right)^2\leq1$$

See this on WA. It looks like a circle, because $\cosh \pi$ and $\sinh \pi$ are very close (less than 0.4% difference).

Here is an R program to plot the ellipse together with points computed directly as $\cos(x+iy)$.

png("ellipse.png", 500, 500)
frame()
plot.window(xlim=c(-12, 12), ylim=c(-12, 12), asp=1)
s <- seq(-pi, pi, length.out=100)
x <- cosh(pi) * cos(s)
y <- sinh(pi) * sin(s)
polygon(x, y, col="gray")
x <- rep(s, times=100)
y <- rep(s, each=100)
z <- cos(complex(real=x, imaginary=y))
points(z, pch=16, cex=0.2)
axis(1)
axis(2)
box()
dev.off()


Plotting in four dimensions isn't so easy. As a workaround, you can use time for the fourth dimension.

This works fairly well for the exponential function,

$$e^z=e^{t+iu}=e^t(\cos u+i\sin u).$$

Taking $t$ for time, and $u$ for abscissa, at a given instant you get a straight helix of parametric equation

$$\begin{cases}x=u,\\y=R\cos u,\\z=R\sin u\end{cases}$$ where $R=e^t$. The helix has an horizontal axis and as time goes, its radius grows exponentially.

If you keep a trace of the curve at all instants (this is a projection form 4D to 3D), you will get an helicoidal surface. Anyway, the true $(t,x,y,z)$ representation is the helix embedded in this surface, that grows over time.

In the case of the cosine, you can use the representation $z=u+it$ and

$$\begin{cases}x=u,\\y=A\cos u,\\z=B\sin u\end{cases}$$ where $A=\cosh t,B=-\sinh t$.

This time we have an elliptic helix that degenerates to a sinusoid at $t=0$ and asymptotically tends to a circular helix of radius $e^{|t|}/2$ at infinity on both sides. ($A,B$ describe an equilateral hyperbola.)

Due to the function parities, we in fact have two intersecting helixes.

Using different 4D points of view, other very different representations can be obtained.

I tried the following script in Octave:

hold on;
for i = 1:100,
for j = 1:100,
c = cos(complex(-10 + 20 * i / 100.0, -pi + 2 * pi * j / 100.0));
x(j) = real(c);
y(j) = imag(c);
end;
plot(x, y);
end;
hold off;


And the result looks like this:

Octave colored the region blue instead of black.

• Why is the surface cut on the left? Is there an additional constraint on $x$ not in your question? You have plotted for $x\in[-2,2]$. If you plot for $x\in[-\pi,\pi]$, you will get the full ellipse. – Jean-Claude Arbaut Oct 12 '16 at 11:07
• @Jean-ClaudeArbaut Corrected. Thanks. – Zirui Wang Oct 12 '16 at 11:30
• Better. Minor detail: since $\cos$ is $2\pi$-pieriodic, no need to plot for $x$ from $-10$ to $10$, the interval $[-\pi,\pi]$ (or $[0,2\pi]$) is enough. – Jean-Claude Arbaut Oct 12 '16 at 11:42
• Also, a matter of style and efficiency in Octave and other array languages: avoid loops whenever it's possible. Use vector instead. For instance, you may replace the inner loop with something like v=linspace(-pi,pi,100) to build the vector. There is probably a function to make a grid array, so you could remove the two loops altogether (then reshape to get only one vector instead of a matrix, with the same values). I can't write the code since I haven't used Octave nor Matlab for more than 10 years, but it's probably as easy as it would be with R - and with R I can show you if you need :) – Jean-Claude Arbaut Oct 12 '16 at 11:47