{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Complex Numbers\n",
"An imaginary number, $j$ is defined by the property $j^2=-1$. A complex number has a real part, $a$ and an imaginary part, $jb$ where $a$ and $b$ are real numbers. The complex number, $a+jb$, may be viewed as a two dimensional vector of sorts, where the real part, $a$, is one component, and the imaginary part, $jb$ is viewed as the other component. There are two ways of representing complex numbers, polar and rectangular. This is based on a famous formula credited to Leonard Euler, $e^{j\\theta} = cos\\theta + jsin\\theta$. Richard Feynman called this formula \"our jewel\" and \"the most remarkable formula in mathematics.\" See the figure below.![figure](Polar-Rectangular-Complex.png)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiplication by $j$ is rotation counterclockwise by $90^\\circ$ or $\\pi/2$. For example, $j = e^{j\\pi/2}$ and $e^{j \\pi} = -1$. The reciprocal of the imaginary number ($1/j$) is $-j$. \n",
"\n",
"The complex conjugate of a complex number is written with a superscript $*$ so the complex conjugate of $a+jb$ is written $(a+jb)^*$ and is equal to $a-jb$. You can take the complex conjugate of any function by replacing every $j$ with a $-j$. The real part of a complex number is $ \\mathscr {Re}(a+jb) = 1/2[(a+jb) + (a+jb)^*] = a$, or half the sum of the complex number and its complex conjugate. The imaginary part of a number is half the difference between the number and its complex conjugate. That is $\\mathscr{Im}[u] = 1/2(u-u^*)$. When you wish to multiply, divide, or exponentiate a complex numbers, the polar form is easiest to use. When you wish to add or subtract complex numbers,the rectangular form is most convenient.\n",
"\n",
"The magnitude of a complex number, $a+jb$ is the sum of the squares of its real and imaginary parts and is written with using magnitude bars, as with vectors. For example, the magnitude of a complex number $a+jb$ is written as $|a+jb| = \\sqrt{a^2+b^2}$. You can write $|a+jb|^2 = (a+jb)(a-jb) = (a+jb)(a+jb)^*$. Note that the magnitude of any complex number is a positive real number and is the distance from the origin to that number. See the figure above.\n",
"\n",
"The angle of a complex number, $a+jb$ is $tan^{-1}b/a$. You have to be very careful when using your calculator, because $tan^{-1}(\\frac{-1}{1}) \\ne tan^{-1}(\\frac{1}{-1})$ and $tan^{-1} (\\frac{-1}{-1}) \\ne tan^{-1}(\\frac{1}{1})$. They are $180^\\circ$ apart. Some calculators have a rectangular to polar conversion function, and it requires you enter both $a$ and $b$. The atan2(y, x) can also be used in some programming languages and it takes the abmiguity from the arctangent function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Phasors Application\n",
"In July 1893 Charles Steinmetz published the method of phasors which simplified solving sinusoidal steady state circuits problems. The key to this method was noticing that $e^{j\\omega t}$ is the eigenfunction of differentiation in time, so that circuits driven by $e^{j\\omega t}$ produce differential equations that turn into algebraic equations, and that solutions for circuits driven by sources of the form $V_m cos(\\omega t + \\phi)$ could be solved by applying $V_m e^{j(\\omega t + \\phi)}$ and then the real part of that solution would be the desired response for the original circuit.\n",
"\n",
"A linear timeinvariant system is linear. Being linear means that a linear combination of inputs has the same linear combination of the outputs. Linearity can be broken down into two properties, proportionality (if you double the input, you double the output), and superposition (if input one produces output one, and input two produces output two, then if the input is the sum of inputs one and two, the output will be the sum of outputs one and two). See the table below.\n",
"\n",
"| Input Signal into the System | Output Signal from the System | ****Reason for this Output**** |\n",
"|-------------------------------------------------|--------------------------------------------------------|------------------------------------|\n",
"| $x_1(t)$ | $y_1(t)$ | Given |\n",
"| $x_2(t)$ | $y_2(t)$ | Given |\n",
"| $\\alpha x_1(t)$ | $\\alpha y_1(t)$ | Proportionality |\n",
"| $x_1(t) + x_2(t)$ | $y_1(t) + y_2(t)$ | Superposition |\n",
"| $\\alpha x_1(t)+\\beta x_2(t)$ | $\\alpha y_1(t)+\\beta y_2(t)$ | Linearity |\n",
"\n",
"\n",
"Suppose we have a linear time invariant system, and the the impulse response is given as shown below.\n",
"\n",
"| Input Signal into the System | Output Signal from the System | ****Reason for this Output**** |\n",
"|----------------------------------------|-------------------------------------|------------------------------|\n",
"| $\\delta (t)$ | $h(t)$ | Given |\n",
"| $\\delta (t-t_0)$ | $h(t-t_0)$ | Time invariance |\n",
"| $x(t_0) \\delta (t-t_0)$ | $x(t_0)h(t-t_0)$ | Linearity |\n",
"| $x(t) =\\int_{-\\infty}^{\\infty}x(t_0) \\delta (t-t_0)dt_0$ | $\\int_{-\\infty}^{\\infty}x(t_0)h(t-t_0) dt_0$ | Linearity |\n",
"| $e^{j2\\pi f t}$ | $\\int_{-\\infty}^{\\infty}e^{j2\\pi ft_0}h(t-t_0) dt_0$ | The above line |\n",
"\n",
"The last output integral can be simplified with a substitution of variables to:\n",
"$$\\int_{-\\infty}^{\\infty}e^{j2\\pi ft_0}h(t-t_0) dt_0 = \\int_{-\\infty}^{\\infty}e^{j2\\pi f(t-t_0)}h(t_0) dt_0 =e^{j2\\pi ft} \\int_{-\\infty}^{\\infty}e^{-j2\\pi fu}h(u) du = e^{j2\\pi ft} H(f)$$ where $H(f)$ is the Fourier Transform of $h(t)$ and the phasor transfer function of the system. From this we see that $e^{j2\\pi ft}$ is the eigenfunction of the system, and that the eigen value for that particular $f$ is $H(f)$.\n",
"\n",
"## Application to a Circuit\n",
"Suppose we wish to understand how the following circuit ![Driven RC Circuit](RC.png) treats a sinusoidal source as the frequency is varied in steady state. Steady state means after transients have died down, so in the language of mathematics, we want the particular solution to the governing differential equation. In engineering we often call this the forced response. The radian frequency $\\omega = 2\\pi f$.\n",
"\n",
"Using Kirchoff's current law, we can write the differential equation $$C \\frac {dv_o}{dt} + \\frac{v_o(t) - v_i(t)}{R} = 0$$ Suppose we used $v_i(t) = V_m e^{j\\omega t} = V_m cos(\\omega t) + jV_m sin(\\omega t)$ as the excitation instead of $v_i(t) = V_m cos(\\omega t)$. Using the ideas of linearity, every part of the solution due to $jsin(\\omega t)$ will be imaginary. The solution we want is the part that is real, because it is due to the excitation, $V_m cos(\\omega t)$. If you stare at the differential equation above with this $v_i(t)$, you will eventually decide a good guess for the solution would be something like $v_o(t) = \\bar V e^{j\\omega t}$ where $\\bar V$ is a complex constant. Substituting this guess into the differential equation, $$ j\\omega C \\bar V e^{j\\omega t}+ \\frac {\\bar Ve^{j\\omega t} -V_me^{j\\omega t}}{R} = 0$$ $$ j\\omega C \\bar V + \\frac {\\bar V -V_m}{R} = 0$$ $$ \\bar V = \\frac{V_m/(j\\omega C)}{R+1/(j\\omega C)} = \\frac{V_m}{1+j\\omega RC} = \\frac{V_m}{\\sqrt{1+\\omega^2R^2C^2}}e^{-jtan^{-1}(\\omega RC)}$$\n",
"\n",
"Note that $H(f)$ from the table above is $H(f) = H(\\omega/(2\\pi)) = \\bar V / V_m =\\frac{1}{\\sqrt{1+\\omega^2R^2C^2}}e^{-jtan^{-1}(\\omega RC)}$. This is known as the transfer function. The ouput is $H(\\omega/(2\\pi)) V_me^{j\\omega t}$ as shown in the table above.\n",
"\n",
"The output due to $v_i(t) = V_m cos(\\omega t)$ is $$ v_o(t) = \\mathscr {Re}[\\bar V e^{j\\omega t}] = \\mathscr {Re}\\left[\\frac{1}{\\sqrt{1+\\omega^2R^2C^2}}e^{j(\\omega t - tan^{-1}(\\omega RC)}\\right]= \\frac{1}{\\sqrt{1+\\omega^2R^2C^2}}cos({\\omega t-tan^{-1}(\\omega RC)})$$ using Euler's formula.\n",
"\n",
"Euler's formula was intimately involved in this example. Visualize the phasor rotating with radian frequency around the complex plane as seen in the demo below. Note that $cos(\\omega t)$ is the projection on the real axis, and $sin(\\omega t)$ is the projection on the imaginary axis."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation\n",
"import matplotlib.ticker as plticker\n",
"import numpy as np\n",
"from IPython.display import HTML\n",
"\n",
"t = np.linspace(0, 2)\n",
"x = np.cos(np.pi*t)\n",
"y = np.sin(np.pi*t)\n",
"\n",
"fig, ax = plt.subplots(nrows=2, ncols=2,\n",
" gridspec_kw={'hspace': 0.3, 'wspace': 0.2}, figsize=(8, 8));\n",
"plt.close();\n",
"ax[0][0].set_aspect(1)\n",
"ax[0][0].axis([-1, 1, -1, 1])\n",
"ax[0][1].set_aspect(1)\n",
"ax[1][0].set_aspect(1)\n",
"ax[0][1].axis([0, 2, -1, 1])\n",
"ax[1][0].axis([-1, 1, 0, 2])\n",
"ax[1][1].axis('off')\n",
"x_title = '$\\mathscr {Re}[e^{j\\omega t}]$'\n",
"y_title = '$\\mathscr {Im}[e^{j\\omega t}]$'\n",
"x_label = '$\\mathscr {Im}$'\n",
"y_label = '$\\mathscr {Re}$'\n",
"t_label = 't (s)'\n",
"ax[0][0].set_xlabel(x_label)\n",
"ax[0][1].set_xlabel(t_label)\n",
"ax[1][0].set_xlabel(x_label)\n",
"ax[0][0].set_ylabel(y_label)\n",
"ax[0][0].xaxis.set_label_position(\"top\")\n",
"ax[0][0].xaxis.tick_top()\n",
"ax[0][1].yaxis.set_label_position(\"right\")\n",
"ax[0][1].yaxis.tick_right()\n",
"ax[1][0].yaxis.set_label_position(\"right\")\n",
"ax[1][0].yaxis.tick_right()\n",
"axis_linewidth = 0.75\n",
"real_color = 'cyan'\n",
"im_color = 'magenta'\n",
"complex_color = 'green'\n",
"ax[0][0].axhline(0, color=real_color, linewidth=axis_linewidth)\n",
"ax[0][0].axvline(0, color=im_color, linewidth=axis_linewidth)\n",
"ax[0][1].axhline(0, color='black', linewidth=axis_linewidth)\n",
"ax[1][0].axvline(0, color='black', linewidth=axis_linewidth)\n",
"ax[0][0].set_title('$e^{j\\omega t}$')\n",
"ax[0][1].set_title(y_title)\n",
"ax[1][0].set_title(x_title)\n",
"loc = plticker.MultipleLocator(base=1.0)\n",
"for i in range(len(ax)):\n",
" for j in range(len(ax[0])):\n",
" ax[i, j].xaxis.set_major_locator(loc)\n",
" ax[j, i].yaxis.set_major_locator(loc)\n",
"l1, = ax[0][0].plot([], [], '.', color=complex_color)\n",
"l2, = ax[0][1].plot([], [], '.', color=im_color)\n",
"l3, = ax[1][0].plot([], [], '.', color=real_color)\n",
"ax[0][1].legend([l2],[y_title])\n",
"ax[1][0].legend([l3],[x_title])\n",
"\n",
"def animate(i):\n",
" l1.set_data(x[:i], y[:i])\n",
" l2.set_data(t[:i], y[:i])\n",
" l3.set_data(x[:i], t[:i])\n",
"\n",
"ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t), interval=200);\n",
"\n",
"HTML(ani.to_html5_video()) # For just the animation, no controls.\n",
"#HTML(ani.to_jshtml()) # Animation with controls."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Euler's indentity is so important, because $e^{j2\\pi ft}$ is the eigen function of every linear time invariant system, which means solving for outputs of this very important class of systems is most easily accomplished if excitations can be made with linear combinations of $e^{j2\\pi ft}$. Essentially what was done in the example above is to write $$cos(\\omega t) = \\mathscr{Re}[e^{j\\omega t}] = 1/2[e^{j\\omega t} + (e^{j\\omega t})^*] = 1/2[e^{j\\omega t} + e^{-j\\omega t}] $$\n",
"\n",
"Fortunately Joseph Fourier discovered how to write most any function of $t$ as a linear combination of $e^{j2\\pi ft}$ for different frequencies, $f$. He started with the periodic functions as described in the notebook, [Complex Fourier Series](Complex%20Fourier%20Series.ipynb). He then took the limit as the period gets very large, and obtained the Fourier Transform which works for non-periodic functions. This made Euler's formula and complex numbers even more important in science and engineering. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}