{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PDE session 1\n", "## Finite differences using python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this session we will use python to compute the derivative of a dependent variable $u$ as function of an independent variable $x$.\n", "\n", "The dependent variable $u$ could be interpreted as the temperature, a displacement or any other scalar variable of interest. In this python notebook we are concerned with how this variable evolves along a 1D bar of length 2 meters. We will compute the gradient of $u$ using backward, forward and centered finite differences.\n", "\n", "The independent variable $x$, the dependent variable $u$ and a reference derivative $du/dx$ are stored in a text file. The reference derivative will be used to determine the accuracy of the various finite difference schemes.\n", "\n", "You should carry the following tasks in this notebook:\n", "\n", "0. Import the necessary python modules\n", "1. Import the data needed in this session\n", "2. Split the data into the independent variable $x$, the dependent variable $u$ and its derivative $du/dx$ using array slicing\n", "3. Plot the dependent variable $u$ and its derivative $du/dx$ using a subplot (i.e. one plot per quantity)\n", "4. Write a function, called backward, which computes $du/dx$ using a backward finite difference where the inputs are the dependent variable $u$ and the independent variable $x$\n", "5. Write a function, called forward, which computes $du/dx$ using a forward finite difference where the inputs are the dependent variable $u$ and the independent variable $x$\n", "6. Write a function, called centered, which computes $du/dx$ using a centered finite difference where the inputs are the dependent variable $u$ and the independent variable $x$\n", "7. Plot the derivatives computed using the functions or questions 4,5 and 6 and plot them alongside the reference data in a subplot (i.e. one plot per method)\n", "8. What can you comment upon the results plotted in question 7?\n", "9. Write a function, called abs_error, which computes the absolute error between the reference derivative $du/dx$ and its numerical estimation\n", "10. Plot the errors obtained with the three different finite difference approximations using a subplot (i.e. one plot per method), what can you say about your results?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 0**\n", "\n", "We first start by importing the necessary modules for our notebook" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 1**\n", "\n", "Next, we need to import our dataset which is stored in the file named `data_1.csv`. This file should be stored in the same folder as this python notebook.\n", "\n", "To load this data file we use the package `loadtxt` from `numpy`. We will look a bit more closely to the import of data in a dedicated notebook. For the time being we will skip the first row of the text file and use a , as a delimeter. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = np.loadtxt('data_1.csv',delimiter=',',skiprows=1)" ] } ], "metadata": { "kernelspec": { "display_name": "env_tf", "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.9.10" } }, "nbformat": 4, "nbformat_minor": 2 }