{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Computing with formulas\n", "*AKA I bought an expensive computer and I use it as if were a cheap calculator*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A formula\n", "\n", "Evaluate $y(t) = v_0t - \\frac{1}{2}gt^2$ for $v_0=0.5 [\\text{m} \\text{s}^{-1}]$, $g=9.81 [\\text{m} \\text{s}^{-1}]$ and $t=0.6 [\\text{s}]$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2342\n" ] } ], "source": [ "print 5*0.6 - 0.5*9.81*0.6**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Evaluate again $y(t) = v_0t - \\frac{1}{2}gt^2$, now for $v_0=1 [\\text{m} \\text{s}^{-1}]$ and $t=0.1 [\\text{s}]$, while keeping the same acceleration $g=9.81 [\\text{m} \\text{s}^{-1}]$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.05095\n" ] } ], "source": [ "print 1*0.1 - 0.5*9.81*0.1**2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables\n", "\n", "Other ways of evaluating $y(t) = v_0t - \\frac{1}{2}gt^2$ for $v_0=0.5 [\\text{m} \\text{s}^{-1}]$, $g=9.81 \\text{m} \\text{s}^{-1}$ and $t=0.6 \\text{s}$ \n", "\n", "We now define **variables** to denote relevant quantities ('initial velocity', the 'acceleraton of gravity' and 'time') assign them a numerical value (or an expression of already defined variables)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2342\n" ] } ], "source": [ "v0 = 5\n", "g = 9.81\n", "t = 0.6\n", "\n", "y = v0*t - 0.5*g*t**2 \n", "\n", "print y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Variable names** can contain any lower or upper case letter or numbers from 0 to 9 (the first character of the name cannot be a number though)\n", "\n", "* Long variables names are explicative but can make the code visually cluttered" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2342\n" ] } ], "source": [ "initial_velocity = 5\n", "acceleration_of_gravity = 9.81\n", "TIME = 0.6\n", "\n", "VerticalPositionOfBall = initial_velocity*TIME - 0.5*acceleration_of_gravity*TIME**2\n", "\n", "print VerticalPositionOfBall" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whenever a Python variable is used to denote a mathematical symbol (for instance, an actual mathematical variable in an actual mathematical formula), it is good practice to name in a way that it resembles the original symbol" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comments\n", "\n", "Python **comments** are important to maintain an intuitive connection between mathematical expressions and the code used to represent/evaluate them" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.2342\n" ] } ], "source": [ "# Compute the height of a ball in vertical motion\n", "\n", "v0 = 5 # initial velocity\n", "g = 9.81 # acceleration of gravity\n", "t = 0.6 # time\n", "\n", "y = v0*t - 0.5*g*t**2 # vertical position\n", "\n", "print y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Prettier printing\n", "\n", "Printing the results should be done in such a way that what gets outputted is informative\n", "\n", "#### The printf syntax for formatting" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At t=0.6 s, the height of the ball is 1.23 m.\n" ] } ], "source": [ "print 'At t=%g s, the height of the ball is %.2f m.' % (t, y)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The string has '*slots*' starting with a percentage sign (here, '**%g**' and '**%.2f**') in which Python variables can be inserted\n", "\n", "* **The percent sign marks the slot**\n", "* **The following character sets the format specification**\n", "\n", "The variables are listed, separated by commas, inside parenthesis after the string (here, '**% (t, y)**')\n", "\n", "* **The first variable goes into the first slot**\n", "* **The second variable goes into the second slot**\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### An example\n", "\n", "Various format specifications" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"62\"\n", "\" 62\"\n", "\"00062\"\n", "\"1.89877e+08\"\n", "\"1.89877E+08\"\n", "\"1.898765e+08\"\n", "\"1.898765E+08\"\n", "\" 1.90E+08\"\n", "\" 1.89877e+08\"\n", "\"1.89877e+08 \"\n", "\"1.899e+08 \"\n", "62\n", "189876545.765\n", "5.1 % of 346.00 Euro is 17.65 Euro\n" ] } ], "source": [ "i = 62\n", "r = 189876545.7654675432\n", "\n", "# Print out numbers with quotes \"\" to see width of field\n", "\n", "print '\"%d\"' % i # minimum field\n", "print '\"%5d\"' % i # field of width 5 characters\n", "print '\"%05d\"' % i # pad with zeros\n", "\n", "print '\"%g\"' % r # r is big number, scientific notation\n", "print '\"%G\"' % r # E in the exponent\n", "print '\"%e\"' % r # compact scientific notation\n", "print '\"%E\"' % r # compact scientific notation\n", "print '\"%20.2E\"' % r # 2 decimals, field of width 20\n", "print '\"%30g\"' % r # field of width 30 (right-adjusted)\n", "print '\"%-30g\"' % r # left-adjust number\n", "print '\"%-30.4g\"' % r # 3 decimals\n", "\n", "print '%s' % i # convert i to string automatically\n", "print '%s' % r\n", "\n", "# Use %% to print the percentage sign\n", "print '%g %% of %.2f Euro is %.2f Euro' % (5.1, 346, 5.1/100*346)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### An example\n", "\n", "Triple-quoted strings for multi-line printing" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "At t=0.600000 s, a ball with\n", "initial velocity v0=5.000E+00 m/s\n", "is located at the height 1.23 m.\n", "\n" ] } ], "source": [ "v0 = 5\n", "g = 9.81\n", "t = 0.6\n", "\n", "y = v0*t - 0.5*g*t**2\n", "\n", "print \"\"\"\n", "At t=%f s, a ball with\n", "initial velocity v0=%.3E m/s\n", "is located at the height %.2f m.\n", "\"\"\" % (t, v0, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The format syntax for formatting\n", "\n", "An alternative to **printf**, with more functionalities and a different syntax\n", "\n", "* Slots are denoted by curly braces (rather than a percentage sign)\n", "* Variable are listed with an optional colon and format specifier\n", "* Variables and their values are listed at the end of the statement\n", "* Slots have names (the sequence of variables is not important)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At t=0.6 s, the height of the ball is 1.23 m.\n", "At t=6.000000E-01 s, the height of the ball is 1.234 m.\n" ] } ], "source": [ "print 'At t={t:g} s, the height of the ball is {y:.2f} m.' .format(t=t, y=y)\n", "\n", "print 'At t={t:E} s, the height of the ball is {y:.4g} m.' .format(t=t*t/t, y=y+2*t-t*2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "At t=0.600000 s, a ball with\n", "initial velocity v0=5.000E+00 m/s\n", "is located at the height 1.23 m.\n", "\n" ] } ], "source": [ "print \"\"\"\n", "At t={t:f} s, a ball with\n", "initial velocity v0={v0:.3E} m/s\n", "is located at the height {y:.2f} m.\n", "\"\"\".format(t=t, v0=v0, y=y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Another formula\n", "\n", "Evaluate $F = \\cfrac{9}{5}C + 32$ for, say, $C=21C$" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "53\n" ] } ], "source": [ "C = 21\n", "F = (9/5)*C + 32\n", "print F" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$53$ is not the correct answer, as $\\cfrac{9}{5}21 + 32 = 1.8 \\cdot 21 + 32 = 37.8 + 32 = 69.8$\n", "\n", "The problem is in the evaluation of $\\cfrac{9}{5}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Integer division and float division\n", "\n", "In many computer languages there are two type of divisions\n", "\n", "* ** Float division**\n", "* ** Integer division**\n", "\n", "##### Float division\n", "\n", "$9/5 = 1.8$, in decimal notation\n", "\n", "##### Integer division\n", "\n", "$9/5 = 1$\n", "\n", "Let $a$ and $b$ be two integers, $a/b$ is also an integer\n", "* It is the largest integer $c$ such that $bc \\le a$\n", "\n", "To avoid accidental integer division:\n", "* Double forward-slash (**//**)\n", "* Argument to interpeter (**-Qnew** and/or **-Qwarnall**)\n", "* Divisions imported from future (**from \\_\\_future\\_\\_ import division**)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Objects\n", "\n", "* **Integer objects**\n", "* **Float objects**\n", "* **String objects**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mathematical functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An example\n", "\n", "Solve $\\cfrac{1}{2}gt^2 - v_0t + y_c = 0$ to get \n", "\\begin{cases} \n", "t_1 = \\cfrac{v_0 - \\sqrt{v_0^2 - 2gy_c}}{g} \\\\\n", "t_1 = \\cfrac{v_0 + \\sqrt{v_0^2 - 2gy_c}}{g}\n", "\\end{cases}\n", "\n", "We need the 'square root' function from the 'math module'\n", "* We must import the module to make its functions available\n", "* **import module\\_name** then **module\\_name.function\\_name** to use" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At t=0.0417064 s and 0.977662 s, the height is 0.2 m.\n" ] } ], "source": [ "v0 = 5\n", "g = 9.81\n", "yc = 0.2\n", "\n", "import math\n", "t1 = (v0 - math.sqrt(v0**2 - 2*g*yc))/g\n", "t2 = (v0 + math.sqrt(v0**2 - 2*g*yc))/g\n", "\n", "print 'At t=%g s and %g s, the height is %g m.' % (t1, t2, yc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, we can use an alternative import syntax\n", "* **from module\\_name import function\\_name** and then **funtion_name** to use" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import sqrt\n", "\n", "t1 = (v0 - sqrt(v0**2 - 2*g*yc))/g\n", "t2 = (v0 + sqrt(v0**2 - 2*g*yc))/g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All functions in a module can be imported at once\n", "* **from module\\_name import \\* **\n", "* **import module\\_name as short\\_module\\_name**" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.22464679915e-16\n", "1.60943791243\n", "1.33742735699\n" ] } ], "source": [ "import math as m # m is now the name of the math module\n", "\n", "v = m.sin(m.pi)\n", "print v\n", "\n", "from math import log as ln\n", "v = ln(5)\n", "print v\n", "\n", "from math import sin as s, cos as c, log as ln\n", "v = s(5)*c(5) + ln(5)\n", "print v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### An example\n", "\n", "Consider function $\\sinh{x} = \\cfrac{1}{2}\\Big(e^x - e^{-x}\\Big)$ and evaluate it for some value of $x$\n", "\n", "* Using **sinh.math** directly\n", "* Using **math.exp**\n", "* Using **math.e**" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "267.744894041\n", "267.744894041\n", "267.744894041\n" ] } ], "source": [ "\n", "from math import sinh, exp, e, pi\n", "\n", "x = 2*pi\n", "\n", "r1 = sinh(x)\n", "r2 = 0.5*(exp(x) - exp(-x))\n", "r3 = 0.5*(e**x - e**(-x))\n", "\n", "print r1\n", "print r2\n", "print r3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The valuations apparently led to identical results, but because of **rounding errors** they aren't \n", "* This is easily shown by using a more exhaustive format for displaying the results" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "267.7448940410164369\n", "267.7448940410164369\n", "267.7448940410163232\n" ] } ], "source": [ "print '%.16f' % (r1)\n", "print '%.16f' % (r2)\n", "print '%.16f' % (r3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Even more trivial calculations are prone to rounding errors\n", "* $\\cfrac{1}{49}49$ \n", "* $\\cfrac{1}{51}51$" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9999999999999999 1.0000000000000000\n" ] } ], "source": [ "print '%.16f %.16f' % (1/49.0*49, 1/51.0*51)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Type conversion" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- Case A ---\n", "21\n", "\n", "--- ------ --- \n", "\n", "--- Case B ---\n", "21.0\n", "\n", "--- ------ --- \n", "\n", "--- Case C ---\n", "20.9\n", "\n", "--- ------ --- \n", "\n", "--- Case D ---\n", "20\n", "\n", "--- ------ --- \n", "\n" ] } ], "source": [ "print '--- Case A ---'\n", "C=21; print C; print type(C)\n", "print '--- ------ --- \\n'\n", "\n", "print '--- Case B ---'\n", "C = float(C); print C; print type(C)\n", "print '--- ------ --- \\n'\n", "\n", "print '--- Case C ---'\n", "C = 20.9; print C; print type(C)\n", "print '--- ------ --- \\n'\n", "\n", "print '--- Case D ---'\n", "D = int(C); print D; print type(D)\n", "print '--- ------ --- \\n'\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n", "21.0\n", "21\n" ] } ], "source": [ "print int(20.9) # Truncation\n", "\n", "print round(20.9) # Rounding\n", "\n", "print int(round(20.9)) # Type conversion after rounding" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complex numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Complex arithmetics" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(4.5+3j)\n", "(-2+0.5j)\n", "(-2+0.5j)\n", "(-10.5-3.75j)\n", "(-0.25641025641+0.282051282051j)\n" ] } ], "source": [ "u = 2.5 + 3j # create a complex number\n", "v = 2 # this is an integer\n", "w = u+v # complex + integer\n", "print w\n", "\n", "a = -2 #\n", "b = 0.5 #\n", "s = a+b*1j # complex number from two floats\n", "print s\n", "\n", "import cmath\n", " \n", "s2 = complex(a,b) # alternative way\n", "print s2\n", "\n", "print s*w # complex*complex\n", "print s/w # complex/complex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **complex object** is equipped with the usual functionalities" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A complex number: \n", "(-2+0.5j) \n", "\n", "Its real part: -2.0\n", "Its imaginary part: 0.5\n", "\n", "The complex conjugate: \n", "(-2-0.5j)\n" ] } ], "source": [ "s = -2+0.5j; print 'A complex number: \\n', s, '\\n'\n", "\n", "print 'Its real part: ', s.real\n", "print 'Its imaginary part: ', s.imag\n", "\n", "print '\\n', 'The complex conjugate: \\n', s.conjugate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Complex functions\n", "\n", "The **cmath module** has functions that return complex numbers (objects)\n", "\n", "**NumPy** has mathematical functions that return a float object or a complex object when appropriate\n", "* **from numpy.lib.scimath import **\n", "* **from scipy import **\n", "* **from scitools.std import **" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--- MATH ---\n", "2.0\n", "\n", "--- ---- --- \n", "\n", "--- CMATH ---\n", "(2+0j)\n", "\n", "1j\n", "\n", "--- ----- --- \n", "\n", "--- NUMPY/SCIMATH ---\n", "2.0\n", "\n", "1j\n", "\n", "--- ------------- ---\n" ] } ], "source": [ "## MATH\n", "print '--- MATH ---'\n", "from math import sqrt\n", "print sqrt(4); print type(sqrt(4)) # Float\n", "#print sqrt(-1) # Illegal (uncomment to run)\n", "print '--- ---- --- \\n'\n", "\n", "\n", "## CMATH\n", "print '--- CMATH ---'\n", "from cmath import sqrt\n", "print sqrt(4); print type(sqrt(4)) # Complex\n", "print sqrt(-1); print type(sqrt(-1)) # Complex\n", "print '--- ----- --- \\n'\n", "\n", "## NUMPY/SCIMATH\n", "print '--- NUMPY/SCIMATH ---'\n", "from numpy.lib.scimath import *\n", "print sqrt(4); print type(sqrt(4)) # Float\n", "print sqrt(-1); print type(sqrt(-1)) #Complex\n", "print '--- ------------- ---'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider function $f(x) = ax^2 + bx + c$" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-1+9.94987437107j)\n", "(-1-9.94987437107j)\n" ] } ], "source": [ "a = 1; b = 2; c = 100 # polynomial coefficients\n", "\n", "from numpy.lib.scimath import sqrt\n", "\n", "r1 = (-b + sqrt(b**2 - 4*a*c))/(2*a)\n", "r2 = (-b - sqrt(b**2 - 4*a*c))/(2*a)\n", "\n", "print r1\n", "print r2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import sin\n", "#r = sin(w) # Uncomment to run" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1490.47882579j\n", "1490.47882579j\n" ] } ], "source": [ "from cmath import sin, sinh\n", "\n", "r1 = sin(8j)\n", "print r1\n", "\n", "r2 = 1j*sinh(8)\n", "print r2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another known relations is $e^{iq} = \\cos{(q)} + i\\sin{(q)}$" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(-0.145500033809+0.989358246623j)\n", "(-0.145500033809+0.989358246623j)\n" ] } ], "source": [ "from cmath import cos, exp\n", "\n", "q = 8 \n", "\n", "print exp(1j*q)\n", "print cos(q) + 1j*sin(q)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Symbolic mathematics\n", "\n", "Package **SymPy** allows performing symbolic operations, like differentiation and integration, equations solving, series expansions and so on\n", "\n", " * **symbols**, define symbols for symbolic math\n", " * **diff**, differentiate expressions\n", " * **integrate**, integrate expressions\n", " * **Rational**, define rational numbers\n", " * **lambdify**, turn symbolic expressions into Python functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Differentiation and integration\n", "\n", "Consider function $y=v_0t - \\cfrac{1}{2}g^2$ and its derivative with respect to the independent variable $t$" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-g*t**2/2 + t*v0\n", "-g*t + v0\n", "acceleration: -g\n", "-g*t**2/2 + t*v0\n" ] } ], "source": [ "from sympy import (symbols,diff,integrate,Rational,lambdify)\n", "\n", "t, v0, g = symbols('t v0 g')\n", "\n", "y = v0*t - Rational(1,2)*g*t**2; print y # Function (position)\n", "dydt = diff(y,t); print dydt # First derivative (velocity)\n", "\n", "print 'acceleration:', diff(y,t,t) # Second derivative (acceleration)\n", "\n", "y2 = integrate(dydt, t); print y2 # Integral of the first derivative (function/position)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SymPy allows symbolic expressions to be turned into ordinary numerical functions" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "-14.62\n", "-14.62\n" ] } ], "source": [ "v = lambdify([t, v0, g], dydt); # Arguments in v [t, v0, g] and symbolic expression dydt\n", "print type(v)\n", "\n", "print v(2,5,9.81) # Calculation\n", "print 5 - 9.81*2 # Manual check\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Equation solving\n", "\n", "Consider $y = v_0t - \\cfrac{1}{2}gt^2$ and symbolically solve for $t$ when $y=0$" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 2*v0/g]\n" ] } ], "source": [ "from sympy import solve\n", "roots = solve(y,t); print roots" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "0\n" ] } ], "source": [ "print y.subs(t, roots[0])\n", "print y.subs(t, roots[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Taylor series\n", "\n", "Consider functions $e^t$ and $e^{\\sin{(t)}}$ in the independent variable $t$ and calculate their Taylor's expansion around point $t_0$" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Third-order Taylor expansion of function exp(t) around t=0: \n", "1 + t + t**2/2 + O(t**3) \n", "\n", "Eighth-order Taylor expansion of function sin(x) around t=0: \n", "1 + t + t**2/2 - t**4/8 - t**5/15 - t**6/240 + t**7/90 + O(t**8)\n" ] } ], "source": [ "from sympy import exp, sin, cos\n", "\n", "f = exp(t); print 'Third-order Taylor expansion of function exp(t) around t=0: \\n', f.series(t,0,3), '\\n'\n", "f = exp(sin(t)); print 'Eighth-order Taylor expansion of function sin(x) around t=0: \\n', f.series(t,0,8)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "It is possible to output the results using LaTeX syntax" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Corresponding LaTeX sybtax: \n", "1 + t + \\frac{t^{2}}{2} - \\frac{t^{4}}{8} - \\frac{t^{5}}{15} - \\frac{t^{6}}{240} + \\mathcal{O}\\left(t^{7}\\right)\n" ] } ], "source": [ "from sympy import latex\n", "\n", "print 'Corresponding LaTeX sybtax: \\n', latex(f.series(t,0,7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That is, $1 + t + \\frac{t^{2}}{2} - \\frac{t^{4}}{8} - \\frac{t^{5}}{15} - \\frac{t^{6}}{240} + \\mathcal{O}\\left(t^{7}\\right)$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are also toosl for expand and simplify expressions" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Function f: -sin(x)*sin(y) + cos(x)*cos(y)\n", "Simplified function f: cos(x + y) \n", "\n", "Expanded function sin(x+y): sin(x)*cos(y) + sin(y)*cos(x)\n" ] } ], "source": [ "from sympy import simplify, expand\n", "\n", "x, y = symbols('x y')\n", "f = -sin(x)*sin(y) + cos(x)*cos(y); print 'Function f: ', f\n", "\n", "print 'Simplified function f: ', simplify(f), '\\n' # Known trigonometric identity\n", "print 'Expanded function sin(x+y): ', expand(sin(x + y), trig=True) # Needs trigonometric hint" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 1 }