{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Computing the weights in Simpson's rule" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import numpy.linalg as la" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We found the integrals:\n", "\n", "$$\n", "\\begin{align*}\n", "\\int_0^1 1 dx &= 1\\\\\n", "\\int_0^1 x dx &= \\frac 12 \\\\\n", "\\int_0^1 x^2 dx &= \\frac 13 \\\\\n", "\\end{align*}\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "integrals = np.array([1, 1/2, 1/3])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0. , 0.5, 1. ])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nodes = np.linspace(0, 1, 3)\n", "nodes" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 0. , 0. ],\n", " [ 1. , 0.5 , 0.25],\n", " [ 1. , 1. , 1. ]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "V = np.array([\n", "1+0*nodes,\n", "nodes,\n", "nodes**2\n", "]).T\n", "V" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now compute the weights:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "array([ 0.16666667, 0.66666667, 0.16666667])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "la.inv(V).T.dot(integrals)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 0 }