- \n",
"
- Overview and Learning Objectives \n", "
- Basic Definitions \n", "
- Polar Representation \n", "
- Complex Operations \n", "
- Polar Coordinate Plot \n", "
- Exercise 1: Rotate Complex Number \n", "
- Exercise 2: Roots of Polynomial \n", "
- Exercise 3: Mandelbrot Set \n", "

\n",
"## Overview and Learning Objectives

\n",
" \n",
"As often in mathematics, transferring a problem from the real into the complex world can lead to significant simplifications. At first sight, this may seem a bit surprising since complex numbers are more difficult to understand than real numbers. As an application of complex numbers, let us consider the problem of finding solutions to polynomial equations. The equation $z^2-1=0$ has the two solutions $z=+1$ and $z=-1$ while the equation $z^2+1=0$ does not have any solution when only considering real numbers. Extending $\\mathbb{R}$ (the space of real numbers) to $\\mathbb{C}$ (the space of complex numbers), however, one also finds for the second equation two solutions given by $z=+i$ and $z=-i$, where $i$ denotes the complex unit. In other words, considering polynomial equations over $\\mathbb{C}$ (rather than $\\mathbb{R}$) makes the problem much easier to understand. In general, an extension of the real numbers to the complex numbers not only gives a broader view but also provides additional tools and structures. We will encounter another application for complex numbers in Unit 7, where we study a complex extension of the exponential function and its relation to trigonometric identities. \n",
" \n",
"In this unit, we review the basic properties of complex numbers. In particular, we provide Python code examples for visualizing complex numbers using either Cartesian coordinates or polar coordinates. Such visualizations, while being a nice application of the library `matplotlib` introduced in Unit 5, should help you gain a geometric understanding of complex numbers and the effect of their algebraic operations. In Exercise 1, you will apply previously introduced Python code to rotate complex numbers and visualize the effect. Then, in Exercise 2, we address the problem of finding the roots of a given polynomial using the NumPy function

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## Basic Definitions\n",
"\n",
"We can write a complex number $c = a + ib$ with real part $\\mathrm{Re}(c) = a$, imaginary part $\\mathrm{Im}(c) = b$, and imaginary unit $i = \\sqrt{-1}$. In Python, the symbol `j` is used to denote the imaginary unit. Furthermore, a coefficient before `j` is needed. To specify a complex number, one can also use the constructor `complex`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = 1.5\n",
"b = 0.8\n",
"c = a + b*1j\n",
"print('c = ', c, ', type(c) = ', type(c))\n",
"c2 = complex(a,b)\n",
"print('c2 = ', c2, ', type(c2) = ', type(c2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python offers the built-in `math` package for basic processing of complex numbers. As an alternative, we use here the external package `numpy`, which was introduced in the [PCP notebook on NumPy Basics](PCP_03_numpy.html)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"print(np.real(c))\n",
"print(np.imag(c))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A complex number $c = a+ib$ can be plotted as a point $(a,b)$ in the Cartesian coordinate system. This point is often visualized by an arrow starting at $(0,0)$ and ending at $(a,b)$. The next code cell serves the following purposes: \n",
"\n",
"* We provide a function `plot_vector` for plotting such an arrow for a given complex number $c$.\n",
"* We provide a function `generate_figure` used to open a figure with adjusted x- and y-axes. \n",
"* We show how to apply the functions and how to place text elements in the figure."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"def generate_figure(figsize=(2, 2), xlim=[0, 1], ylim=[0, 1]):\n",
" \"\"\"Generate figure for plotting complex numbers\n",
"\n",
" Notebook: PCP_06_complex.ipynb\n",
"\n",
" Args:\n",
" figsize: Width, height in inches (Default value = (2, 2))\n",
" xlim: Limits for x-axis (Default value = [0, 1])\n",
" ylim: Limits for y-axis (Default value = [0, 1])\n",
" \"\"\"\n",
" plt.figure(figsize=figsize)\n",
" plt.grid()\n",
" plt.xlim(xlim)\n",
" plt.ylim(ylim)\n",
" plt.xlabel('$\\mathrm{Re}$')\n",
" plt.ylabel('$\\mathrm{Im}$')\n",
"\n",
"def plot_vector(c, color='k', start=0, linestyle='-'):\n",
" \"\"\"Plot arrow corresponding to difference of two complex numbers\n",
"\n",
" Notebook: PCP_06_complex.ipynb\n",
"\n",
" Args:\n",
" c: Complex number\n",
" color: Color of arrow (Default value = 'k')\n",
" start: Complex number encoding the start position (Default value = 0)\n",
" linestyle: Linestyle of arrow (Default value = '-')\n",
"\n",
" Returns:\n",
" plt.arrow: matplotlib.patches.FancyArrow\n",
" \"\"\"\n",
" return plt.arrow(np.real(start), np.imag(start), np.real(c), np.imag(c),\n",
" linestyle=linestyle, head_width=0.05,\n",
" fc=color, ec=color, overhang=0.3, length_includes_head=True)\n",
" \n",
"\n",
"c = 1.5 + 0.8j\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[0, 2.5], ylim=[0, 1])\n",
"v = plot_vector(c, color='k')\n",
"plt.text(1.5, 0.8, '$c$', size='16')\n",
"plt.text(0.8, 0.55, '$|c|$', size='16')\n",
"plt.text(0.25, 0.05, '$\\gamma$', size='16');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## Polar Representation\n",
"\n",
"The **absolute value** (or **modulus**) of a complex number $a+ib$ is defined by\n",
"\n",
"$$|c| := \\sqrt{a^2 + b^2}.$$\n",
"\n",
"The **angle** (given in radians) is given by \n",
"\n",
"$$\\gamma := \\mathrm{atan2}(b, a).$$\n",
"\n",
"This yields a number in the interval $(-\\pi,\\pi]$, which can be mapped to $[0,2\\pi)$ by adding $2\\pi$ to negative values. The **angle** (given in degrees) is obtained by\n",
"\n",
"$$360 \\cdot \\frac{\\gamma}{2\\pi}.$$\n",
"\n",
"The complex number $c=a+ib$ is uniquely defined by the pair $(|c|, \\gamma)$, which is also called the **polar representation** of $c$. One obtains the Cartesian representation $(a,b)$ from the polar representation $(|c|,\\gamma)$ as follows:\n",
"\n",
"\\begin{eqnarray}\n",
"a &=& |c| \\cdot \\cos(\\gamma) \\\\\n",
"b &=& |c| \\cdot \\sin(\\gamma)\n",
"\\end{eqnarray}\n",
"\n",
"In the following code cell, we introduce some NumPy-functions for computing the absolute values and angle of a complex number."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = 1.5 + 0.8j\n",
"print('c = :', c)\n",
"print('Absolute value:', np.abs(c))\n",
"print('Angle (in radians):', np.angle(c))\n",
"print('Angle (in degree):', np.rad2deg(np.angle(c)))\n",
"print('Angle (in degree):', 180 * np.angle(c) / np.pi )\n",
"print(f'Cartesian representation: ({np.real(c)}, {np.imag(c)})') \n",
"print(f'Polar representation: ({np.abs(c)}, {np.angle(c)})') "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## Complex Operations\n",
"\n",
"For two complex numbers $c_1=a_1+ib_1$ and $c_2=a_2+ib_2$, the sum \n",
"\n",
"$$\n",
"c_1 + c_2 = (a_1 + ib_1) + (a_2 + ib_2) := (a_1 + a_2) + i(b_1 + b_2) \n",
"$$\n",
"\n",
"is defined by summing their real and imaginary parts individually. The geometric intuition of addition can be visualized by a parallelogram:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c1 = 1.3 - 0.3j\n",
"c2 = 0.3 + 0.5j\n",
"c = c1 + c2\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[-0.3, 2.2], ylim=[-0.4, 0.6])\n",
"v1 = plot_vector(c1, color='k')\n",
"v2 = plot_vector(c2, color='b')\n",
"plot_vector(c1, start=c2, linestyle=':', color='lightgray')\n",
"plot_vector(c2, start=c1, linestyle=':', color='lightgray')\n",
"v3 = plot_vector(c, color='r')\n",
"\n",
"plt.legend([v1, v2, v3], ['$c_1$', '$c_2$', '$c_1+c_2$']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Complex multiplication of two numbers $c_1=a_1+ib_1$ and $c_2=a_2+ib_2$ is defined by:\n",
"\n",
"$$c = c_1 \\cdot c_2 = (a_1 + ib_1) \\cdot (a_2 + ib_2) := (a_1a_2 - b_1b_2) + i(a_1b_2 + b_1a_2).$$\n",
"\n",
"Geometrically, the product is obtained by adding angles and by multiplying the absolute values. In other words, if $(|c_1|, \\gamma_1)$ and $(|c_2|, \\gamma_2)$ are the polar representations of $c_1$ and $c_1$, respectively, then the polar representation $(|c|, \\gamma)$ of $c$ is given by:\n",
"\n",
"\\begin{eqnarray}\n",
"\\gamma &=& \\gamma_1 + \\gamma_2 \\\\\n",
"|c| &=& |c_1| \\cdot |c_2|\n",
"\\end{eqnarray}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c1 = 1.0 - 0.5j\n",
"c2 = 2.3 + 0.7j\n",
"c = c1 * c2\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[-0.5, 4.0], ylim=[-0.75, 0.75])\n",
"v1 = plot_vector(c1, color='k')\n",
"v2 = plot_vector(c2, color='b')\n",
"v3 = plot_vector(c, color='r')\n",
"plt.legend([v1, v2, v3], ['$c_1$', '$c_2$', '$c_1 \\cdot c_2$']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given a complex number $c = a + bi$, the **complex conjugation** is defined by $\\overline{c} := a - bi$. Many computations can be expressed in a more compact form using the complex conjugate. The following identities hold:\n",
"As for the real and imaginary part as well as the absolute value, one has:\n",
"\n",
"\\begin{eqnarray}\n",
"a &=& \\frac{1}{2} (c+\\overline{c}) \\\\\n",
"b &=& \\frac{1}{2i} (c-\\overline{c}) \\\\\n",
"|c|^2 &=& c\\cdot \\overline{c}\\\\\n",
"\\overline{c_1+c_2} &=& \\overline{c_1} + \\overline{c_2}\\\\\n",
"\\overline{c_1\\cdot c_2} &=& \\overline{c_1} \\cdot \\overline{c_2}\n",
"\\end{eqnarray}\n",
"\n",
"Geometrically, conjugation is reflection on the real axis."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = 1.5 + 0.4j\n",
"c_conj = np.conj(c)\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[0, 2.5], ylim=[-0.5, 0.5])\n",
"v1 = plot_vector(c, color='k')\n",
"v2 = plot_vector(c_conj, color='r')\n",
"\n",
"plt.legend([v1, v2], ['$c$', r'$\\overline{c}$']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`np.roots`

. The roots' visualizations will give you a feeling of how the roots distribute in the complex plane depending on the polynomials' coefficients. As another application of complex numbers, we discuss in Exercise 3 how to generate the Mandelbrot set, which is a famous and one of the most beautiful examples for a fractal set. When going through this unit, we recommend that you do the first two exercises while the third exercise is left as a playground for exploring the beauty of fractals and the power of visualizations (e.g., tweaking around with color maps).\n",
" \n",
"\n",
"**Note:** As the previous examples showed, the library

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For a non-zero complex number $c = a + bi$, there is an **inverse** complex number $c^{-1}$ with the property that $c\\cdot c^{-1} = 1$. The inverse is given by:\n",
"\n",
"$$c^{-1} := \\frac{a}{a^2 + b^2} + i \\frac{-b}{a^2 + b^2} = \\frac{a}{|c|^2} + i \\frac{-b}{|c|^2} = \\frac{\\overline{c}}{|c|^2}.$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = 1.5 + 0.4j\n",
"c_inv = 1 / c\n",
"c_prod = c * c_inv\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[-0.3, 2.2], ylim=[-0.5, 0.5])\n",
"v1 = plot_vector(c, color='k')\n",
"v2 = plot_vector(c_inv, color='r')\n",
"v3 = plot_vector(c_prod, color='gray')\n",
"\n",
"plt.legend([v1, v2, v3], ['$c$', '$c^{-1}$', '$c*c^{-1}$']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the inverse, division can be defined:\n",
" \n",
"$$\\frac{c_1}{c_2} = c_1 c_2^{-1} = \\frac{a_1 + ib_1}{a_2 + ib_2} := \\frac{a_1a_2 + b_1b_2}{a_2^2 + b_2^2} + i\\frac{b_1a_2 - a_1b_2}{a_2^2 + b_2^2} = \\frac{c_1\\cdot \\overline{c_2}}{|c_2|^2}.$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c1 = 1.3 + 0.3j\n",
"c2 = 0.8 + 0.4j\n",
"c = c1 / c2\n",
"\n",
"generate_figure(figsize=(7.5, 3), xlim=[-0.25, 2.25], ylim=[-0.5, 0.5])\n",
"v1 = plot_vector(c1, color='k')\n",
"v2 = plot_vector(c2, color='b')\n",
"v3 = plot_vector(c, color='r')\n",
"\n",
"plt.legend([v1, v2, v3], ['$c_1$', '$c_2$', '$c_1/c_2$']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## Polar Coordinate Plot\n",
"\n",
"Finally, we show how complex vectors can be visualized in a polar coordinate plot. Also, the following code cell illustrates some functionalities of the Python libraries `numpy` and `matplotlib`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def plot_polar_vector(c, label=None, color=None, start=0, linestyle='-'):\n",
" \"\"\"Plot arrow in polar plot\n",
"\n",
" Notebook: PCP_06_complex.ipynb\n",
"\n",
" Args:\n",
" c: Complex number\n",
" label: Label of arrow (Default value = None)\n",
" color: Color of arrow (Default value = None)\n",
" start: Complex number encoding the start position (Default value = 0)\n",
" linestyle: Linestyle of arrow (Default value = '-')\n",
" \"\"\"\n",
" # plot line in polar plane\n",
" line = plt.polar([np.angle(start), np.angle(c)], [np.abs(start), np.abs(c)], label=label,\n",
" color=color, linestyle=linestyle)\n",
" # plot arrow in same color\n",
" this_color = line[0].get_color() if color is None else color\n",
" plt.annotate('', xytext=(np.angle(start), np.abs(start)), xy=(np.angle(c), np.abs(c)),\n",
" arrowprops=dict(facecolor=this_color, edgecolor='none',\n",
" headlength=12, headwidth=10, shrink=1, width=0))\n",
" \n",
"c_abs = 1.5\n",
"c_angle = 45 # in degree\n",
"c_angle_rad = np.deg2rad(c_angle) \n",
"a = c_abs * np.cos(c_angle_rad)\n",
"b = c_abs * np.sin(c_angle_rad)\n",
"c1 = a + b*1j \n",
"c2 = -0.5 + 0.75*1j\n",
"\n",
"plt.figure(figsize=(6, 6))\n",
"plot_polar_vector(c1, label='$c_1$', color='k')\n",
"plot_polar_vector(np.conj(c1), label='$\\overline{c}_1$', color='gray')\n",
"plot_polar_vector(c2, label='$c_2$', color='b')\n",
"plot_polar_vector(c1*c2, label='$c_1\\cdot c_2$', color='r')\n",
"plot_polar_vector(c1/c2, label='$c_1/c_2$', color='g')\n",
"\n",
"plt.ylim([0, 1.8]);\n",
"plt.legend(framealpha=1);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises and Results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import libpcp.complex\n",
"show_result = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"`matplotlib`

allows for using certain LaTeX code to render mathematical text in the figures. To this end, one needs to activate certain settings and uses specific encodings in order to avoid conflicts between special characters used both in Python and LateX for different purposes. In particular, the backslash `\\`

needs to be handled with care, which can be done by using so-called raw strings marked by `r'...'`

. For further details, we refer to the Python documentation and other tutorials available on the web.\n",
"\n",
"**Exercise 1: Rotate Complex Number**

\n", "Create and plot the following complex numbers using the functions described above.\n", "

"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n", "Create and plot the following complex numbers using the functions described above.\n", "

- \n",
"
- Create a complex number $c$ with an angle of $20$ degrees and an absolute value of $1.2$. Also plot its conjugate and inverse. \n", "
- Write a function
`rotate_complex`

that rotates a complex number $c$ by $r$ degrees in clockwise direction. Apply this function for $c= 1 + 0.5i$ and $r\\in\\{10,20, 30\\}$. Plot all resulting complex numbers. \n",
"

\n",
"**Exercise 2: Roots of Polynomial**

\n", "Let $p(z)= p_0 z^N + p_1 z^{N-1} + \\ldots + p_{N-1}z + p_N$ be a complex-valued polynomial of degree $N\\in\\mathbb{N}$ with coefficients $p_n\\in\\mathbb{C}$ for $n\\in[0:N]$. Define a function

"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n", "Let $p(z)= p_0 z^N + p_1 z^{N-1} + \\ldots + p_{N-1}z + p_N$ be a complex-valued polynomial of degree $N\\in\\mathbb{N}$ with coefficients $p_n\\in\\mathbb{C}$ for $n\\in[0:N]$. Define a function

`vis_root`

that inputs a polynomial and visualizes all roots of the polynomial (i.e., all zeros of the polynomial). To compute the roots, use the NumPy function `np.roots`

. To encode the polynomial follow the conventions as used for `np.roots`

, where the above polynomial is represented by the array `(p[0],p[1], ..., p[N])`

. For the visualization, use the function `plt.scatter`

for representing each root as a dot in the Cartesian plane. Apply the function for the following polynomials and discuss the results. \n",
"- \n",
"
- $p(z)=z^2-2$ (
`p = np.array([1, 0, -2])`

) \n",
" - $p(z)=z^2+2$ (
`p = np.array([1, 0, 2])`

) \n",
" - $p(z)=z^8-1$ (
`p = np.array([1, 0, 0, 0, 0, 0, 0, 0, -1])`

) \n",
" - $p(z)=z^8 + z^7 + z^6$ (
`p = np.array([1, 1, 1, 0, 0, 0, 0, 0, 0])`

) \n",
" - $p(z)=z^8 + z^7 + z^6 + 0.000001$ (
`p = np.array([1, 1, 1, 0, 0, 0, 0, 0, 0.000001])`

) \n",
" - $p(z)=z^3 -2iz^2 + (2+4i)z + 3 $ (
`p = np.array([1, -2j, 2 + 4j, 3])`

) \n",
"

\n",
"**Note:** As another application of complex numbers, we will consider in the next exercise a construction of a subset of complex numbers known as Mandelbrot set. As noted at Wikipedia, images of the Mandelbrot set exhibit an elaborate and infinitely complicated boundary that reveals progressively ever-finer recursive detail at increasing magnifications, making the boundary of the Mandelbrot set a fractal curve. The Mandelbrot set has become popular outside mathematics both for its aesthetic appeal and as an example of a complex structure arising from applying simple rules. It is one of the best-known examples of mathematical visualization, mathematical beauty, and motif. In practice, one cannot easily compute the Mandelbrot set. Instead, one uses iterative algorithms based on heuristics to find an approximation of the Mandelbrot set, which can then be visualized as a subset of the complex plane. Often the boundary of the Mandelbrot set (the fractal curve) and its outer neighborhood are visualized with a color-coding that expresses divergence properties. This leads to the fascinating images of the Mandelbrot set you may have encountered. On the web, you can find numerous examples of how to approximate the Mandelbrot set and visualize it (e.g., also in an interactive fashion that allows you to zoom into the Mandelbrot set). In the following exercise, we will dive into this topic of fractals and their visualization. **But be careful: You may become addicted and get lost in this topic. Don't forget to continue with the other units.** \n",
"

"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import IPython.display as ipd\n",
"ipd.display(ipd.YouTubeVideo('b005iHf8Z3g', width=600, height=450))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"**Exercise 3: Mandelbrot Set**

\n", "Let $c\\in\\mathbb{C}$ be a complex number and $f_c:\\mathbb{C}\\to\\mathbb{C}$ the function defined by $f_c(z)=z^2+c$ for $z\\in\\mathbb{C}$. Starting with $z=0$, we consider the iteration $v_c(0):=f_c(0)$ and $v_c(k) := f_c(v_c(k-1))$ for $k\\in\\mathbb{N}$. The Mandelbrot set is the set of complex numbers $c$ for which the series $(v_c(k))_{k\\in\\mathbb{N}}$ stays bounded (i.e., if there is a constant $\\gamma_c$ such that $v_c(k)\\leq \\gamma_c$ for all $k\\in\\mathbb{N}$. Write a function that plots the Mandelbrot set in the Cartesian plane, where a number $c$ is colored black if it belongs to the Mandelbrot set and otherwise is colored white. \n", "

"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n", "Let $c\\in\\mathbb{C}$ be a complex number and $f_c:\\mathbb{C}\\to\\mathbb{C}$ the function defined by $f_c(z)=z^2+c$ for $z\\in\\mathbb{C}$. Starting with $z=0$, we consider the iteration $v_c(0):=f_c(0)$ and $v_c(k) := f_c(v_c(k-1))$ for $k\\in\\mathbb{N}$. The Mandelbrot set is the set of complex numbers $c$ for which the series $(v_c(k))_{k\\in\\mathbb{N}}$ stays bounded (i.e., if there is a constant $\\gamma_c$ such that $v_c(k)\\leq \\gamma_c$ for all $k\\in\\mathbb{N}$. Write a function that plots the Mandelbrot set in the Cartesian plane, where a number $c$ is colored black if it belongs to the Mandelbrot set and otherwise is colored white. \n", "

- \n",
"
- Model the Mandelbrot set as a binary indicator function $\\chi:\\mathbb{C}\\in\\{0,1\\}$, where $\\chi(c)=1$ if $c$ belongs to the Mandelbrot set and $\\chi(c)=0$ otherwise. \n", "
- Only consider complex numbers $c=a+ib$ on a discrete grid on a bounded range. It suffices to consider the range $a\\in[-2,1]$ and $b\\in[-1.2,1.2]$. Furthermore, for efficiency reasons, use a grid spacing that is not too fine. First, try out $\\Delta a = \\Delta b = 0.01$. To create the grid, you may use the function
`np.meshgrid`

. \n",
" - Test for each $c=a+ib$ on that grid, if $(v_c(k))_{k\\in\\mathbb{N}}$ remains bounded or not. Computationally, this cannot be tested easily. However, usually, the sequence $(v_c(k))$ increases in an exponential fashion in the case that it is not bounded. Therefore, a pragmatic (yet not always correct) test is to fix a maximum number of iterations (e.g., $K = 50$) and a threshold (e.g., $L = 100$). In case that $v_c(K)
\n", " - Plot $\\chi$ using the function
`np.imshow`

, use the colormap`'gray_r'`

. Furthermore, use the parameter`extent`

to adjust ranges of the horizontal axis $[-2,1]$ (real part) and vertical axis $[-1.2,1.2]$ (imaginary part). \n",
"