{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, x_i, h = symbols('x x_i h')\n",
    "init_printing(use_latex='mathjax')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{2 h}{3}$"
      ],
      "text/plain": [
       "2⋅h\n",
       "───\n",
       " 3 "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr1 = ((x-x_i+h)/h)**2\n",
    "expr2 = ((x_i+h-x)/h)**2\n",
    "result = integrate(expr1, (x, x_i-h, x_i))+integrate(expr2, (x, x_i, x_i+h))\n",
    "simplify(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{h}{6}$"
      ],
      "text/plain": [
       "h\n",
       "─\n",
       "6"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = (x_i+h-x)*(x-x_i)/(h**2)\n",
    "result = integrate(expr, (x, x_i, x_i+h))\n",
    "simplify(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{h}{6}$"
      ],
      "text/plain": [
       "h\n",
       "─\n",
       "6"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = (x_i-x)*(x-(x_i-h))/(h**2)\n",
    "result = integrate(expr, (x, x_i-h, x_i))\n",
    "simplify(result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
