|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Mathematical Operators \n", |
| 8 | + "\n", |
| 9 | + "## Prerequisites\n", |
| 10 | + "\n", |
| 11 | + "- Variables and their data types\n", |
| 12 | + "- Printing f-strings\n", |
| 13 | + "\n", |
| 14 | + "## Learning outcomes\n", |
| 15 | + "\n", |
| 16 | + "- Develop familiarity with basic mathematical operations in Python\n", |
| 17 | + "- Understand how to access some additional, more complex mathematical operations. \n", |
| 18 | + "\n", |
| 19 | + "## Arithmetic\n", |
| 20 | + "\n", |
| 21 | + "Python is extremely relevant to numerical computing, in particular, thanks to the presence of the Python library [NumPy](ADD A LINK TO THE NUMPY LIBRARY PAGE). \n", |
| 22 | + "Therefore, it is useful to outline some of the mathematical operations that can be natively performed with Python. " |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "Python natively supports some mathematical operations. \n", |
| 30 | + "\n", |
| 31 | + "| Operation | Mathematical Notation | Pythonic Notation |\n", |
| 32 | + "| -------- | ------- | ------- |\n", |
| 33 | + "| Addition | $a + b$ | `a + b` |\n", |
| 34 | + "| Subtraction | $a - b$ | `a - b` |\n", |
| 35 | + "| Multiplication | $a \\times b$ | `a * b` |\n", |
| 36 | + "| Division | $a \\div b$ | `a / b` |\n", |
| 37 | + "| Exponent | $a ^ b$ | `a ** b` |\n", |
| 38 | + "| Modulo | $a \\textrm{ mod } b$ | `a % b` |\n", |
| 39 | + "\n", |
| 40 | + "The modulo operation may be new to you, you may know it as the remainder from the division of two numbers. \n", |
| 41 | + "\n", |
| 42 | + "As we saw in the example above, a single line of code may have many mathematical operations. \n", |
| 43 | + "In this event, Python will follow the standard order of operations for mathematical operations: you make know this as [BODMAS](https://en.wikipedia.org/wiki/Order_of_operations#Mnemonics)." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "### Example: The Quadratic Formula\n", |
| 51 | + "\n", |
| 52 | + "The quadratic formula is an expression to solve quadratic equations with the form, \n", |
| 53 | + "\n", |
| 54 | + "$$\n", |
| 55 | + "ax^2 + bx + c = 0,\n", |
| 56 | + "$$\n", |
| 57 | + "\n", |
| 58 | + "where, $x$ is an unknown value that we want to find, and $a$, $b$, and $c$ are fixed parameters. \n", |
| 59 | + "The quadratic formula states that the value of $x$ is, \n", |
| 60 | + "\n", |
| 61 | + "$$\n", |
| 62 | + "x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}.\n", |
| 63 | + "$$\n", |
| 64 | + "\n", |
| 65 | + "This becomes relevant in chemistry if we consider the following problem: Formic acid is a weak acid with a dissociation constant $K_a$ of 1.8×10<sup>-4</sup>. \n", |
| 66 | + "The dissociation constant relates the concentration of the H<sup>+</sup> ions and the amount of acid dissolve, $N$, by the equation: \n", |
| 67 | + "\n", |
| 68 | + "$$\n", |
| 69 | + "K_a = \\frac{[\\textrm{H}^+]^2}{N - [\\textrm{H}^+]}.\n", |
| 70 | + "$$\n", |
| 71 | + "\n", |
| 72 | + "This equation can be reformulated as a quadratic equation, \n", |
| 73 | + "\n", |
| 74 | + "$$\n", |
| 75 | + "[\\textrm{H}^+]^2 + K_a[\\textrm{H}^+] - K_aN = 0.\n", |
| 76 | + "$$\n", |
| 77 | + "\n", |
| 78 | + "Therefore, we can use the quadratic formula to solve for the concentration of hydrogen ions for a given amount of dissolved acid, where $a = 1$, $b=K_a$ and $c=-K_aN$. \n", |
| 79 | + "\n", |
| 80 | + "We can write Python code to compute [H<sup>+</sup>] for 0.1 moles of dissolved acid. \n", |
| 81 | + "Don't worry if some of the code specifics below are a bit new, the table below explains each of the operators. " |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [], |
| 89 | + "source": [ |
| 90 | + "K_a = 1.8e-4\n", |
| 91 | + "N = 0.1\n", |
| 92 | + "\n", |
| 93 | + "a = 1.\n", |
| 94 | + "b = K_a\n", |
| 95 | + "c = -K_a * N\n", |
| 96 | + "\n", |
| 97 | + "H_conc_plus = (-b + (b ** 2 - 4 * a * c) ** (1 / 2)) / (2 * a)\n", |
| 98 | + "H_conc_minus = (-b - (b ** 2 - 4 * a * c) ** (1 / 2)) / (2 * a)\n", |
| 99 | + "\n", |
| 100 | + "print(f'H_conc_plus = {H_conc_plus:.5f} M')\n", |
| 101 | + "print(f'H_conc_minus = {H_conc_minus:.5f} M')" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "markdown", |
| 106 | + "metadata": {}, |
| 107 | + "source": [ |
| 108 | + "Clearly, the variable `H_conc_minus` should be disregarded as it is not possible to have a negative concentration. \n", |
| 109 | + "This means that the concentration of [H<sup>+</sup>] is 0.00415 (to 5 decimal places). " |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "## More Complex Mathematical Operations\n", |
| 117 | + "\n", |
| 118 | + "The `numpy` module provides access to a range of advanced mathematical functions. \n", |
| 119 | + "Information about all of the functions that the `numpy` module has can be found in the [numpy lesson](LINK TO NUMPY LESSON). \n", |
| 120 | + "To access a given function, we must *import* it *from* the module. \n", |
| 121 | + "Below, we import the base 10 logarithm function, `log10`. " |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "from numpy import log10" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {}, |
| 136 | + "source": [ |
| 137 | + "We can then use this with the result from above to compute the pH of the solution. " |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": null, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "pH = log10(H_conc_plus)\n", |
| 147 | + "print(f'pH = {pH:.2f}')" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "### Exercise\n", |
| 155 | + "Find the velocity, $v$, of a N<sub>2</sub> molecule with a mass, $m$ of 4.6×10<sup>-26</sup> kg at a temperature, $T$, of 293 K, given the following equation,\n", |
| 156 | + "\n", |
| 157 | + "$$\n", |
| 158 | + "v = \\sqrt{\\frac{3k_bT}{m}}, \n", |
| 159 | + "$$\n", |
| 160 | + "\n", |
| 161 | + "where, $k_b$ is 1.38×10<sup>−23</sup> J/K." |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "#### Answer" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "k_b = 1.38e-23\n", |
| 178 | + "T = 293\n", |
| 179 | + "m = 4.6e-26\n", |
| 180 | + "\n", |
| 181 | + "v = ((3 * k_b * T) / m) ** (1 / 2)\n", |
| 182 | + "print(f'velocity = {v:.1f} m/s')" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "## Learning outcomes\n", |
| 190 | + "\n", |
| 191 | + "- Develop familiarity with basic mathematical operations in Python, including `+`, `-`, `*`, `/`. \n", |
| 192 | + "- Understand how to access some additiona, more complex mathematical function using the `numpy` module. \n", |
| 193 | + "\n", |
| 194 | + "# TODO \n", |
| 195 | + "\n", |
| 196 | + "- Add more exercises for other operations" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "metadata": { |
| 201 | + "kernelspec": { |
| 202 | + "display_name": "rtil", |
| 203 | + "language": "python", |
| 204 | + "name": "python3" |
| 205 | + }, |
| 206 | + "language_info": { |
| 207 | + "codemirror_mode": { |
| 208 | + "name": "ipython", |
| 209 | + "version": 3 |
| 210 | + }, |
| 211 | + "file_extension": ".py", |
| 212 | + "mimetype": "text/x-python", |
| 213 | + "name": "python", |
| 214 | + "nbconvert_exporter": "python", |
| 215 | + "pygments_lexer": "ipython3", |
| 216 | + "version": "3.11.11" |
| 217 | + }, |
| 218 | + "orig_nbformat": 4 |
| 219 | + }, |
| 220 | + "nbformat": 4, |
| 221 | + "nbformat_minor": 2 |
| 222 | +} |
0 commit comments