{ "cells": [ { "cell_type": "markdown", "id": "a69f4fe2", "metadata": {}, "source": [ "\n", "\n", "\n", "" ] }, { "cell_type": "markdown", "id": "f02afc82", "metadata": {}, "source": [ "# Complex Numbers and Trigonometry" ] }, { "cell_type": "markdown", "id": "b2437f8e", "metadata": {}, "source": [ "## Contents\n", "\n", "- [Complex Numbers and Trigonometry](#Complex-Numbers-and-Trigonometry) \n", " - [Overview](#Overview) \n", " - [De Moivre’s Theorem](#De-Moivre’s-Theorem) \n", " - [Applications of de Moivre’s Theorem](#Applications-of-de-Moivre’s-Theorem) " ] }, { "cell_type": "markdown", "id": "36ce0858", "metadata": {}, "source": [ "## Overview\n", "\n", "This lecture introduces some elementary mathematics and trigonometry.\n", "\n", "Useful and interesting in its own right, these concepts reap substantial rewards when studying dynamics generated\n", "by linear difference equations or linear differential equations.\n", "\n", "For example, these tools are keys to understanding outcomes attained by Paul\n", "Samuelson (1939) [[Sam39](https://python.quantecon.org/zreferences.html#id61)] in his classic paper on interactions\n", "between the investment accelerator and the Keynesian consumption function, our\n", "topic in the lecture [Samuelson Multiplier Accelerator](https://python.quantecon.org/samuelson.html).\n", "\n", "In addition to providing foundations for Samuelson’s work and extensions of\n", "it, this lecture can be read as a stand-alone quick reminder of key results\n", "from elementary high school trigonometry.\n", "\n", "So let’s dive in." ] }, { "cell_type": "markdown", "id": "b6a1c653", "metadata": {}, "source": [ "### Complex Numbers\n", "\n", "A complex number has a **real part** $x$ and a purely **imaginary part** $y$.\n", "\n", "The Euclidean, polar, and trigonometric forms of a complex number $z$ are:\n", "\n", "$$\n", "z = x + iy = re^{i\\theta} = r(\\cos{\\theta} + i \\sin{\\theta})\n", "$$\n", "\n", "The second equality above is known as **Euler’s formula**\n", "\n", "- [Euler](https://en.wikipedia.org/wiki/Leonhard_Euler) contributed many other formulas too! \n", "\n", "\n", "The complex conjugate $\\bar z$ of $z$ is defined as\n", "\n", "$$\n", "\\bar z = x - iy = r e^{-i \\theta} = r (\\cos{\\theta} - i \\sin{\\theta} )\n", "$$\n", "\n", "The value $x$ is the **real** part of $z$ and $y$ is the\n", "**imaginary** part of $z$.\n", "\n", "The symbol $| z |$ = $\\sqrt{\\bar{z}\\cdot z} = r$ represents the **modulus** of $z$.\n", "\n", "The value $r$ is the Euclidean distance of vector $(x,y)$ from the\n", "origin:\n", "\n", "$$\n", "r = |z| = \\sqrt{x^2 + y^2}\n", "$$\n", "\n", "The value $\\theta$ is the angle of $(x,y)$ with respect to the real axis.\n", "\n", "Evidently, the tangent of $\\theta$ is $\\left(\\frac{y}{x}\\right)$.\n", "\n", "Therefore,\n", "\n", "$$\n", "\\theta = \\tan^{-1} \\Big( \\frac{y}{x} \\Big)\n", "$$\n", "\n", "Three elementary trigonometric functions are\n", "\n", "$$\n", "\\cos{\\theta} = \\frac{x}{r} = \\frac{e^{i\\theta} + e^{-i\\theta}}{2} , \\quad\n", "\\sin{\\theta} = \\frac{y}{r} = \\frac{e^{i\\theta} - e^{-i\\theta}}{2i} , \\quad\n", "\\tan{\\theta} = \\frac{y}{x}\n", "$$\n", "\n", "We’ll need the following imports:" ] }, { "cell_type": "code", "execution_count": null, "id": "c4476212", "metadata": { "hide-output": false }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "plt.rcParams[\"figure.figsize\"] = (11, 5) #set default figure size\n", "import numpy as np\n", "from sympy import *" ] }, { "cell_type": "markdown", "id": "bc6d234b", "metadata": {}, "source": [ "### An Example\n", "\n", "Consider the complex number $z = 1 + \\sqrt{3} i$.\n", "\n", "For $z = 1 + \\sqrt{3} i$, $x = 1$, $y = \\sqrt{3}$.\n", "\n", "It follows that $r = 2$ and\n", "$\\theta = \\tan^{-1}(\\sqrt{3}) = \\frac{\\pi}{3} = 60^o$.\n", "\n", "Let’s use Python to plot the trigonometric form of the complex number\n", "$z = 1 + \\sqrt{3} i$." ] }, { "cell_type": "code", "execution_count": null, "id": "24581947", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Abbreviate useful values and functions\n", "π = np.pi\n", "\n", "\n", "# Set parameters\n", "r = 2\n", "θ = π/3\n", "x = r * np.cos(θ)\n", "x_range = np.linspace(0, x, 1000)\n", "θ_range = np.linspace(0, θ, 1000)\n", "\n", "# Plot\n", "fig = plt.figure(figsize=(8, 8))\n", "ax = plt.subplot(111, projection='polar')\n", "\n", "ax.plot((0, θ), (0, r), marker='o', color='b') # Plot r\n", "ax.plot(np.zeros(x_range.shape), x_range, color='b') # Plot x\n", "ax.plot(θ_range, x / np.cos(θ_range), color='b') # Plot y\n", "ax.plot(θ_range, np.full(θ_range.shape, 0.1), color='r') # Plot θ\n", "\n", "ax.margins(0) # Let the plot starts at origin\n", "\n", "ax.set_title(\"Trigonometry of complex numbers\", va='bottom',\n", " fontsize='x-large')\n", "\n", "ax.set_rmax(2)\n", "ax.set_rticks((0.5, 1, 1.5, 2)) # Less radial ticks\n", "ax.set_rlabel_position(-88.5) # Get radial labels away from plotted line\n", "\n", "ax.text(θ, r+0.01 , r'$z = x + iy = 1 + \\sqrt{3}\\, i$') # Label z\n", "ax.text(θ+0.2, 1 , '$r = 2$') # Label r\n", "ax.text(0-0.2, 0.5, '$x = 1$') # Label x\n", "ax.text(0.5, 1.2, r'$y = \\sqrt{3}$') # Label y\n", "ax.text(0.25, 0.15, r'$\\theta = 60^o$') # Label θ\n", "\n", "ax.grid(True)\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "6c5d63b5", "metadata": {}, "source": [ "## De Moivre’s Theorem\n", "\n", "de Moivre’s theorem states that:\n", "\n", "$$\n", "(r(\\cos{\\theta} + i \\sin{\\theta}))^n =\n", "r^n e^{in\\theta} =\n", "r^n(\\cos{n\\theta} + i \\sin{n\\theta})\n", "$$\n", "\n", "To prove de Moivre’s theorem, note that\n", "\n", "$$\n", "(r(\\cos{\\theta} + i \\sin{\\theta}))^n = \\big( re^{i\\theta} \\big)^n\n", "$$\n", "\n", "and compute." ] }, { "cell_type": "markdown", "id": "e47b7401", "metadata": {}, "source": [ "## Applications of de Moivre’s Theorem" ] }, { "cell_type": "markdown", "id": "0e168dc8", "metadata": {}, "source": [ "### Example 1\n", "\n", "We can use de Moivre’s theorem to show that\n", "$r = \\sqrt{x^2 + y^2}$.\n", "\n", "We have\n", "\n", "\n", "\\begin{aligned}\n", "1 &= e^{i\\theta} e^{-i\\theta} \\\\\n", "&= (\\cos{\\theta} + i \\sin{\\theta})(\\cos{(\\text{-}\\theta)} + i \\sin{(\\text{-}\\theta)}) \\\\\n", "&= (\\cos{\\theta} + i \\sin{\\theta})(\\cos{\\theta} - i \\sin{\\theta}) \\\\\n", "&= \\cos^2{\\theta} + \\sin^2{\\theta} \\\\\n", "&= \\frac{x^2}{r^2} + \\frac{y^2}{r^2}\n", "\\end{aligned}\n", "\n", "\n", "and thus\n", "\n", "$$\n", "x^2 + y^2 = r^2\n", "$$\n", "\n", "We recognize this as a theorem of **Pythagoras**." ] }, { "cell_type": "markdown", "id": "60664568", "metadata": {}, "source": [ "### Example 2\n", "\n", "Let $z = re^{i\\theta}$ and $\\bar{z} = re^{-i\\theta}$ so that $\\bar{z}$ is the **complex conjugate** of $z$.\n", "\n", "$(z, \\bar z)$ form a **complex conjugate pair** of complex numbers.\n", "\n", "Let $a = pe^{i\\omega}$ and $\\bar{a} = pe^{-i\\omega}$ be\n", "another complex conjugate pair.\n", "\n", "For each element of a sequence of integers $n = 0, 1, 2, \\ldots,$.\n", "\n", "To do so, we can apply de Moivre’s formula.\n", "\n", "Thus,\n", "\n", "\n", "\\begin{aligned}\n", "x_n &= az^n + \\bar{a}\\bar{z}^n \\\\\n", "&= p e^{i\\omega} (re^{i\\theta})^n + p e^{-i\\omega} (re^{-i\\theta})^n \\\\\n", "&= pr^n e^{i (\\omega + n\\theta)} + pr^n e^{-i (\\omega + n\\theta)} \\\\\n", "&= pr^n [\\cos{(\\omega + n\\theta)} + i \\sin{(\\omega + n\\theta)} +\n", " \\cos{(\\omega + n\\theta)} - i \\sin{(\\omega + n\\theta)}] \\\\\n", "&= 2 pr^n \\cos{(\\omega + n\\theta)}\n", "\\end{aligned}\n", "" ] }, { "cell_type": "markdown", "id": "35416d66", "metadata": {}, "source": [ "### Example 3\n", "\n", "This example provides machinery that is at the heard of Samuelson’s analysis of his multiplier-accelerator model [[Sam39](https://python.quantecon.org/zreferences.html#id61)].\n", "\n", "Thus, consider a **second-order linear difference equation**\n", "\n", "$$\n", "x_{n+2} = c_1 x_{n+1} + c_2 x_n\n", "$$\n", "\n", "whose **characteristic polynomial** is\n", "\n", "$$\n", "z^2 - c_1 z - c_2 = 0\n", "$$\n", "\n", "or\n", "\n", "$$\n", "(z^2 - c_1 z - c_2 ) = (z - z_1)(z- z_2) = 0\n", "$$\n", "\n", "has roots $z_1, z_1$.\n", "\n", "A **solution** is a sequence $\\{x_n\\}_{n=0}^\\infty$ that satisfies\n", "the difference equation.\n", "\n", "Under the following circumstances, we can apply our example 2 formula to\n", "solve the difference equation\n", "\n", "- the roots $z_1, z_2$ of the characteristic polynomial of the\n", " difference equation form a complex conjugate pair \n", "- the values $x_0, x_1$ are given initial conditions \n", "\n", "\n", "To solve the difference equation, recall from example 2 that\n", "\n", "$$\n", "x_n = 2 pr^n \\cos{(\\omega + n\\theta)}\n", "$$\n", "\n", "where $\\omega, p$ are coefficients to be determined from\n", "information encoded in the initial conditions $x_1, x_0$.\n", "\n", "Since\n", "$x_0 = 2 p \\cos{\\omega}$ and $x_1 = 2 pr \\cos{(\\omega + \\theta)}$\n", "the ratio of $x_1$ to $x_0$ is\n", "\n", "$$\n", "\\frac{x_1}{x_0} = \\frac{r \\cos{(\\omega + \\theta)}}{\\cos{\\omega}}\n", "$$\n", "\n", "We can solve this equation for $\\omega$ then solve for $p$ using $x_0 = 2 pr^0 \\cos{(\\omega + n\\theta)}$.\n", "\n", "With the sympy package in Python, we are able to solve and plot the\n", "dynamics of $x_n$ given different values of $n$.\n", "\n", "In this example, we set the initial values: - $r = 0.9$ -\n", "$\\theta = \\frac{1}{4}\\pi$ - $x_0 = 4$ -\n", "$x_1 = r \\cdot 2\\sqrt{2} = 1.8 \\sqrt{2}$.\n", "\n", "We first numerically solve for $\\omega$ and $p$ using\n", "nsolve in the sympy package based on the above initial\n", "condition:" ] }, { "cell_type": "code", "execution_count": null, "id": "2117e95e", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Set parameters\n", "r = 0.9\n", "θ = π/4\n", "x0 = 4\n", "x1 = 2 * r * sqrt(2)\n", "\n", "# Define symbols to be calculated\n", "ω, p = symbols('ω p', real=True)\n", "\n", "# Solve for ω\n", "## Note: we choose the solution near 0\n", "eq1 = Eq(x1/x0 - r * cos(ω+θ) / cos(ω), 0)\n", "ω = nsolve(eq1, ω, 0)\n", "ω = np.float(ω)\n", "print(f'ω = {ω:1.3f}')\n", "\n", "# Solve for p\n", "eq2 = Eq(x0 - 2 * p * cos(ω), 0)\n", "p = nsolve(eq2, p, 0)\n", "p = np.float(p)\n", "print(f'p = {p:1.3f}')" ] }, { "cell_type": "markdown", "id": "446ce26b", "metadata": {}, "source": [ "Using the code above, we compute that\n", "$\\omega = 0$ and $p = 2$.\n", "\n", "Then we plug in the values we solve for $\\omega$ and $p$\n", "and plot the dynamic." ] }, { "cell_type": "code", "execution_count": null, "id": "63fbba20", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Define range of n\n", "max_n = 30\n", "n = np.arange(0, max_n+1, 0.01)\n", "\n", "# Define x_n\n", "x = lambda n: 2 * p * r**n * np.cos(ω + n * θ)\n", "\n", "# Plot\n", "fig, ax = plt.subplots(figsize=(12, 8))\n", "\n", "ax.plot(n, x(n))\n", "ax.set(xlim=(0, max_n), ylim=(-5, 5), xlabel='$n$', ylabel='$x_n$')\n", "\n", "# Set x-axis in the middle of the plot\n", "ax.spines['bottom'].set_position('center')\n", "ax.spines['right'].set_color('none')\n", "ax.spines['top'].set_color('none')\n", "ax.xaxis.set_ticks_position('bottom')\n", "ax.yaxis.set_ticks_position('left')\n", "\n", "ticklab = ax.xaxis.get_ticklabels() # Set x-label position\n", "trans = ticklab.get_transform()\n", "ax.xaxis.set_label_coords(31, 0, transform=trans)\n", "\n", "ticklab = ax.yaxis.get_ticklabels() # Set y-label position\n", "trans = ticklab.get_transform()\n", "ax.yaxis.set_label_coords(0, 5, transform=trans)\n", "\n", "ax.grid()\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "0a8e48d9", "metadata": {}, "source": [ "### Trigonometric Identities\n", "\n", "We can obtain a complete suite of trigonometric identities by\n", "appropriately manipulating polar forms of complex numbers.\n", "\n", "We’ll get many of them by deducing implications of the equality\n", "\n", "$$\n", "e^{i(\\omega + \\theta)} = e^{i\\omega} e^{i\\theta}\n", "$$\n", "\n", "For example, we’ll calculate identities for\n", "\n", "$\\cos{(\\omega + \\theta)}$ and $\\sin{(\\omega + \\theta)}$.\n", "\n", "Using the sine and cosine formulas presented at the beginning of this\n", "lecture, we have:\n", "\n", "\n", "\\begin{aligned}\n", "\\cos{(\\omega + \\theta)} = \\frac{e^{i(\\omega + \\theta)} + e^{-i(\\omega + \\theta)}}{2} \\\\\n", "\\sin{(\\omega + \\theta)} = \\frac{e^{i(\\omega + \\theta)} - e^{-i(\\omega + \\theta)}}{2i}\n", "\\end{aligned}\n", "\n", "\n", "We can also obtain the trigonometric identities as follows:\n", "\n", "\n", "\\begin{aligned}\n", "\\cos{(\\omega + \\theta)} + i \\sin{(\\omega + \\theta)}\n", "&= e^{i(\\omega + \\theta)} \\\\\n", "&= e^{i\\omega} e^{i\\theta} \\\\\n", "&= (\\cos{\\omega} + i \\sin{\\omega})(\\cos{\\theta} + i \\sin{\\theta}) \\\\\n", "&= (\\cos{\\omega}\\cos{\\theta} - \\sin{\\omega}\\sin{\\theta}) +\n", "i (\\cos{\\omega}\\sin{\\theta} + \\sin{\\omega}\\cos{\\theta})\n", "\\end{aligned}\n", "\n", "\n", "Since both real and imaginary parts of the above formula should be\n", "equal, we get:\n", "\n", "\n", "\\begin{aligned}\n", "\\cos{(\\omega + \\theta)} = \\cos{\\omega}\\cos{\\theta} - \\sin{\\omega}\\sin{\\theta} \\\\\n", "\\sin{(\\omega + \\theta)} = \\cos{\\omega}\\sin{\\theta} + \\sin{\\omega}\\cos{\\theta}\n", "\\end{aligned}\n", "\n", "\n", "The equations above are also known as the **angle sum identities**. We\n", "can verify the equations using the simplify function in the\n", "sympy package:" ] }, { "cell_type": "code", "execution_count": null, "id": "ce6aa594", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Define symbols\n", "ω, θ = symbols('ω θ', real=True)\n", "\n", "# Verify\n", "print(\"cos(ω)cos(θ) - sin(ω)sin(θ) =\",\n", " simplify(cos(ω)*cos(θ) - sin(ω) * sin(θ)))\n", "print(\"cos(ω)sin(θ) + sin(ω)cos(θ) =\",\n", " simplify(cos(ω)*sin(θ) + sin(ω) * cos(θ)))" ] }, { "cell_type": "markdown", "id": "bf19f548", "metadata": {}, "source": [ "### Trigonometric Integrals\n", "\n", "We can also compute the trigonometric integrals using polar forms of\n", "complex numbers.\n", "\n", "For example, we want to solve the following integral:\n", "\n", "$$\n", "\\int_{-\\pi}^{\\pi} \\cos(\\omega) \\sin(\\omega) \\, d\\omega\n", "$$\n", "\n", "Using Euler’s formula, we have:\n", "\n", "\n", "\\begin{aligned}\n", "\\int \\cos(\\omega) \\sin(\\omega) \\, d\\omega\n", "&=\n", "\\int\n", "\\frac{(e^{i\\omega} + e^{-i\\omega})}{2}\n", "\\frac{(e^{i\\omega} - e^{-i\\omega})}{2i}\n", "\\, d\\omega \\\\\n", "&=\n", "\\frac{1}{4i}\n", "\\int\n", "e^{2i\\omega} - e^{-2i\\omega}\n", "\\, d\\omega \\\\\n", "&=\n", "\\frac{1}{4i}\n", "\\bigg( \\frac{-i}{2} e^{2i\\omega} - \\frac{i}{2} e^{-2i\\omega} + C_1 \\bigg) \\\\\n", "&=\n", "-\\frac{1}{8}\n", "\\bigg[ \\bigg(e^{i\\omega}\\bigg)^2 + \\bigg(e^{-i\\omega}\\bigg)^2 - 2 \\bigg] + C_2 \\\\\n", "&=\n", "-\\frac{1}{8} (e^{i\\omega} - e^{-i\\omega})^2 + C_2 \\\\\n", "&=\n", "\\frac{1}{2} \\bigg( \\frac{e^{i\\omega} - e^{-i\\omega}}{2i} \\bigg)^2 + C_2 \\\\\n", "&= \\frac{1}{2} \\sin^2(\\omega) + C_2\n", "\\end{aligned}\n", "\n", "\n", "and thus:\n", "\n", "$$\n", "\\int_{-\\pi}^{\\pi} \\cos(\\omega) \\sin(\\omega) \\, d\\omega =\n", "\\frac{1}{2}\\sin^2(\\pi) - \\frac{1}{2}\\sin^2(-\\pi) = 0\n", "$$\n", "\n", "We can verify the analytical as well as numerical results using\n", "integrate in the sympy package:" ] }, { "cell_type": "code", "execution_count": null, "id": "50a9d194", "metadata": { "hide-output": false }, "outputs": [], "source": [ "# Set initial printing\n", "init_printing()\n", "\n", "ω = Symbol('ω')\n", "print('The analytical solution for integral of cos(ω)sin(ω) is:')\n", "integrate(cos(ω) * sin(ω), ω)" ] }, { "cell_type": "code", "execution_count": null, "id": "a1207205", "metadata": { "hide-output": false }, "outputs": [], "source": [ "print('The numerical solution for the integral of cos(ω)sin(ω) \\\n", "from -π to π is:')\n", "integrate(cos(ω) * sin(ω), (ω, -π, π))" ] }, { "cell_type": "markdown", "id": "5cb8409e", "metadata": {}, "source": [ "### Exercises\n", "\n", "We invite the reader to verify analytically and with the sympy package the following two equalities:\n", "\n", "$$\n", "\\int_{-\\pi}^{\\pi} \\cos (\\omega)^2 \\, d\\omega = \\frac{\\pi}{2}\n", "$$\n", "\n", "$$\n", "\\int_{-\\pi}^{\\pi} \\sin (\\omega)^2 \\, d\\omega = \\frac{\\pi}{2}\n", "$$" ] } ], "metadata": { "date": 1643278480.9391353, "filename": "complex_and_trig.md", "kernelspec": { "display_name": "Python", "language": "python3", "name": "python3" }, "title": "Complex Numbers and Trigonometry" }, "nbformat": 4, "nbformat_minor": 5 }