{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture 2 Part 1 - Integration - Examples and In-Class Exercises\n", "\n", "\n", "\n", "We can now write a little python program that evaluates the sum for the integral. We do this for the function $f(x)=x^4-2x+1$ from $a=0$ to $b=2$. We know the correct answer for this integral and compare the result of our program to it $$\\int_0^2 (x^4-2x+1)dx = \\left[\\frac{1}{5}x^5-x^2+x\\right]_0^2=4.4 .$$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.3467600000000015\n" ] } ], "source": [ "def f(x):\n", " return x**4 - 2*x + 1\n", "\n", "N = 10\n", "a = 0.0\n", "b = 2.0\n", "h = (b-a)/N\n", "\n", "s = 0.0\n", "for k in range(1,N+1):\n", " s += f(a+(k-0.5)*h)\n", "\n", "print(h*s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With only 10 points, the value of the integral is ok, but not very good, because the constant value of the function that we assume in each interval does not reflect the curvature of the function 𝑓(𝑥) very well." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1 - midpoint integration\n", "\n", "1. Plot the function $ f(x)=x^4-2x+1 $.\n", "2. Change the example program to add a loop over the number of discretisation points. \n", "3. Plot the value of the integral and/or the integration error as a function of integration points.\n", "\n", "## Talking points\n", "\n", "1. What do you observe?\n", "2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? \n", "3. How can we do better?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2 - trapezoidal rule\n", "\n", "The trapezoidal rule is $I(a,b) \\approx h \\left[ \\frac{1}{2}f(a)+\\frac{1}{2}f(b)+\\sum_{k=1}^{N-1} f(a+kh) \\right]$.\n", "\n", "1. Change your integration program to the trapezoidal rule. Loop over the number of discretisation points. \n", "2. Plot the value of the integral and/or the integration error as a function of integration points.\n", "\n", "## Talking points\n", "\n", "1. What changes with the trapezoidal rule?\n", "2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? \n", "3. How could we do even better?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3 - Simpon's rule\n", "\n", "Simpson's rule leads to the following integral expression: $I(a,b)\\approx \\frac{h}{3} \\left[f(a)+f(b)+4\\sum_{k=1}^{N/2} f(a+(2k-1)h)+2\\sum_{k=1}^{N/2-1}f(a+2kh) \\right]$\n", "\n", "1. Change your integration program to the Simpson's rule. Loop over the number of discretisation points. \n", "2. Plot the value of the integral and/or the integration error as a function of integration points.\n", "\n", "## Talking points\n", "\n", "1. What changes with the Simpson's rule?\n", "2. How many points do you need for 1% accuracy and how many for 0.1% accuracy? \n", "3. How could we do even better?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4\n", "\n", "The python file gaussxw.py is included in the lecture folder. It contains the routine gaussxw that generates the integration grid points and corresponding weights. It use is shown below.\n", "\n", "1. Change your integration program to use Gauss-Legendre integration. Use $N=3$ integration points.\n", "2. Test what happens when you increase the number of integration points.\n", "\n", "## Talking points\n", "\n", "1. What changes with Gauss-Legendre integration?\n", "2. Do you still need to verify convergence?\n", "\n", "How to use the gaussxw routine:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import the routine (make sure that gaussxw.py is in the same directory as your notebook)\n", "from gaussxw import gaussxw\n", "\n", "# Calculate the sample points (x) and weights (w) in the interval [-1,1]. x and w are arrays with N entries.\n", "x,w = gaussxw(N)\n", "\n", "# Note, if you are not integrating from -1 to 1, you need to map x and w to the required integration domain." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bonus exercise 5\n", "\n", "1. Plot the function $ f(x)=0.2 x^5 - 0.3 x^4.0+2.3 x^3-3.1 x^2 + 1.7 $.\n", "2. Integrate $f(x)$ from -1.0 to 2.0 using both Gauss-Legendre integration and the Simpson's rule.\n", "3. Define your own function to integrate." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }