{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mathematical models in Jupyter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction\n", "\n", "Jupyter is a fully-functional alternative to Mathematica or Maple notebooks for developing and analyzing mathematical models in biology (or any other discipine, for that matter). For this, you will need to use a [Computer Algebra System](https://en.wikipedia.org/wiki/Computer_algebra_system) (CAS). A CAS is software that emulates manual (pen-and-paper) manipulations of mathematical expressions. Yes, it can be done, and very effectively, for a vast array of mathematical problems! A CAS combined with a graphing/plotting package like matplotlib gives you a powerful tool for mathematical modelling using a Jupyter notebook.\n", "\n", "We will use Python's [SymPy](http://sympy.org/en/index.html) package, which provides powerful CAS features for most common mathematical modelling problems. \n", "\n", "There is also [Sage](http://www.sagemath.org/), a more capable CAS. We will not use it here because is not a regular Python package, but rather, uses Python as its programming language. So unlike SymPy it cannot just be loaded in a Jupyter nb with a Python kernel. Instead, you will need to [install its Jupyter kernel](http://doc.sagemath.org/html/en/reference/repl/sage/repl/ipython_kernel/install.html). You can [install and try sage](http://www.sagemath.org/download.html) outside of Jupyter if you want.\n", "\n", "So let's use SymPy in Jupyter." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you used [Anaconda](http://continuum.io/downloads) to install Jupyter, it should already include SymPy, Matplotlib, IPython, NumPy, and other useful packages for scientific computing. If you don't have SymPy for some other reason, install it in Linux/mac using: \n", "```bash\n", "$ sudo apt install python3-sympy\n", "```\n", "Otherwise, follow the instructions [here](http://docs.sympy.org/latest/install.html). \n", "\n", "We also need to rum some commands so that the plots appear correctly:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And import scipy and sympy:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from sympy import *\n", "import scipy as sc\n", "init_printing() # for pretty-printing equations etc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some preliminaries\n", "\n", "Before we get started with our mathematical modelling session in Jupyter, some SymPy preliminaries. \n", "\n", "### Symbolic variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In CAS' like SymPy, we need to create symbolic variables for the mathematical variables we want to work with. A new symbolic variable can be created using `var`. Try this:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "sympy.core.symbol.Symbol" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = var('x')\n", "type(x) # check it's class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also define multiple symbolic variables at one go:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "a, b, c = var(\"a, b, c\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more info on symbolic variables, [have a look at this](http://docs.sympy.org/latest/gotchas.html#variables)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is often important to add assumptions (constraints) to our symbolic vars:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "x = var('x', real=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now check:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.is_imaginary" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "x = Symbol('x', positive=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, check:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAACoAAAAPBAMAAABgjEDtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMA782r3SJ2ZjIQmUS7VIlAnjihAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAv0lEQVQYGWNg/GQs72z0hQEV8Acw5BcwNKIKMsxiAIkyo4mGg0XZJqAKR4BFOUCi0Q2c3QFwWaAJ3Iq5j0LXH+A9n8DAuvwxWAooysC4dn4B0wEG/gSGLRO4JUEaQKJMDgwMPGBROQYGMaAgRNQAKsrxq7zcHC66ACrK/hckBARgExbA1H4DiyFEmQ8wxCcwODEwTIOpZQGqZRdguHiSob+AYSUDA/caeZkV3Of/XGBgeJc2RWQCp1XeBKghaBQAM0c287zNvm0AAAAASUVORK5CYII=\n", "text/latex": [ "$$\\mathrm{True}$$" ], "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x > 0" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAC0AAAAPBAMAAACCUFuUAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMqvdzRC772ZUdiKJmUS6hfrNAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA90lEQVQYGWMQsv+kpPI/gAEIhHxAJAzIJzAwiAuAeKxFMDEQLa/AwMAxASySBCahBEicdQEDCKCLb2U4gFW8m4Gha80CkHrGlV0ODHt7tIHKgOZkM3BaM1wCiYcxMBjwmTI8B7pPvkj9NwNnIcP7AKA4UGAB+wEGdqBmsHoGxjX3NwDFmb+tZpC/0dHUABbvZmB12yAvABRnPfL/Qj/E0SB3MrAZMMiLBiQxbGXg+CcPNAQIwOL8BxjmywkkMcxiYNBgdmBgXQAVZ09gOC8RkMYwPYAhm/EHA28Aw177b7cZGLh110R5Cf3XftPV84Ah6NZRoDnYAQAwwD6NhCuIjAAAAABJRU5ErkJggg==\n", "text/latex": [ "$$\\mathrm{False}$$" ], "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x < 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Symbolic equations\n", "\n", "We can define the mathematical equations (functions) that we will be using/manipulating as follows: " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEgAAAAbBAMAAAAt2dQtAAAAMFBMVEX///8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIkQqzLsm4+cAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABYUlEQVQoFY2SMUjEUAyG/95JW2mtVcHBqYPgqoKb4AkdRFDkVpdu6lYHcRD1JnV01sXJRdAbvMmlwsFNDs46OLqIukqhJi+tpXi0lyEv+d9HXsILUGWdhagKgeWONCoh3XW+q6Er7acSAsx4AEgPB4A6xGgeuf5Wc0mvbZCb6A8o9Zn8DW6B5RKoCzgrZ4tSLedO85AiK8RwknxB53dzK0L2udxM5gBHRQircrtHR+9w6iCUNIWMhIzG4vHJ5gG7XdvSGyrLKj28786MkTIt6ja1F1mxmbYmlZyW5R7x/YlAF1QJQ7Mquff9Hd9f47iOVz4+2QEEAaNpR3njHwj4IoXoOYrf2LNl0zWh/v5aVGrcjI5hRJKmkB1DbVHaOA1511rCuDBZJSPAJSubIlPBp/1e05Msg+oeXlhZF7n0W7RAILUuErJ/zEOK6IPF5gpyMelmadnS/RXQvAz/d6r1JfUXHhFDT+L/2ZAAAAAASUVORK5CYII=\n", "text/latex": [ "$$\\left(x + \\pi\\right)^{2}$$" ], "text/plain": [ " 2\n", "(x + π) " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "MyFun = (pi + x)**2; MyFun" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "MyFun = N_0 + (N_max - N_0) * exp(-exp(r_max * exp(1) * (t_lag - t)/((N_max - N_0) * log(10)) + 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See the nice $\\LaTeX$ - formatted output: this is where `init_printing()` comes handy. \n", "\n", "