{ "metadata": { "name": "", "signature": "sha256:bec7cbf6bf5a2743f26f8494b78056ea168f06558b51516fa932958b691e990b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from sympy import *\n", "init_printing(use_latex='mathjax')\n", "n, m = symbols('n,m', integer=True)\n", "x, y, z = symbols('x,y,z')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Integrals\n", "\n", "In the last section we learned symbolic differentiation with `.diff`. Here we'll cover symbolic integration with `integrate`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is how we write the indefinite integral\n", "\n", "$$ \\int x^2 dx = \\frac{x^3}{3}$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Indefinite integral\n", "integrate(x**2, x)" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\frac{x^{3}}{3}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ " 3\n", "x \n", "\u2500\u2500\n", "3 " ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the definite integral\n", "\n", "$$ \\int_0^3 x^2 dx = \\left.\\frac{x^3}{3} \\right|_0^3 = \\frac{3^3}{3} - \\frac{0^3}{3} = 9 $$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Definite integral\n", "integrate(x**2, (x, 0, 3))" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$9$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "9" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As always, because we're using symbolics, we could use a symbol whenever we previously used a number\n", "\n", "$$ \\int_y^z x^n dx $$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "integrate(x**n, (x, y, z))" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\begin{cases} - \\log{\\left (y \\right )} + \\log{\\left (z \\right )} & \\text{for}\\: n = -1 \\\\- \\frac{y^{n + 1}}{n + 1} + \\frac{z^{n + 1}}{n + 1} & \\text{otherwise} \\end{cases}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "\u23a7-log(y) + log(z) for n = -1\n", "\u23aa \n", "\u23aa n + 1 n + 1 \n", "\u23a8 y z \n", "\u23aa- \u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500 otherwise \n", "\u23aa n + 1 n + 1 \n", "\u23a9 " ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Compute the following integrals:\n", "\n", "$$ \\int \\sin(x) dx $$\n", "$$ \\int_0^{\\pi} \\sin(x) dx $$\n", "$$ \\int_0^y x^5 + 12x^3 - 2x + 1 $$\n", "$$ \\int e^{\\frac{(x - \\mu)^2}{\\sigma^2}} $$\n", "\n", "Feel free to play with various parameters and settings and see how the results change." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Use `integrate` to solve the integrals above\n", "\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Are there some integrals that SymPy can't do? Find some." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Use `integrate` on other equations. Symbolic integration has it limits, find them." ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }