dissertation_work/Programming/Python files/Backward_Euler.ipynb

158 lines
22 KiB
Plaintext
Raw Normal View History

2024-02-16 00:55:42 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAA5S0lEQVR4nO3dd3yV5f3/8deVTUII2YwQEvZKgCSy95AhooiK1lm1Vutq1TpbR+2wav1WxVatA/Vn0RYQsUWrCCggIAlbhjIlAbKAkJ2cnM/vj/skhsxDcrJOPs/H4zw4977uJLxz5TrXfV1GRFBKKdX2ebR0AZRSSrmGBrpSSrkJDXSllHITGuhKKeUmNNCVUspNaKArpZSb0EBXrZox5gljzP9z0bluNMasb+Q5xhtj9texPcYYI8YYr8ZcR6mG0EBXLmGMOWKMKTHGhFVZv80RcDFOnGOSMSa1yQrpAiKyTkT6ly877ntaQ89njFnk+PqMqLSujzHmvB8QcZyrxBiTV+nl2dCyqbZHA1250mHg6vIFY0wc4N9yxWkzTgG/d9G5nhGRjpVeZS46r2oDNNCVK70LXF9p+Qbgnco7GGN8jTHPGWN+MMakG2NeMcZ0MMYEAJ8A3SrVLrs5DvMxxrxjjMk1xnxrjEmqdL6Bxpi1xpgzjm1zK20LNcasMMacNcZ8A/SureDGmLeNMfc53nd31JrvcCz3NsacMsZ4VP4rwhjzLhANfOwo7wOVTnmN4x6zjDGP1vN1exuIN8ZMrGc/peqkga5caRPQyRGynsBVQNX276eBfsAwoA/QHXhMRPKBWcDxSrXL445j5gLvA52BFcBCAGOMN/Ax8BkQAdwFvGeMKW8SeRkoAroCNzletfkSmOR4PxE4BEyotLxOROyVDxCR64AfgIsd5X2m0uZxQH9gKvCYMWZgHdcuAP4I/KGmjcaYvzl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z50thYaEMHTq04n5FRB5++GF59913RUTk9OnT0rdvX8nLy5O33npL7rjjjnPKNnr0aCkqKpLMzEwJCQmRkpISSU5OliFDhkheXp7k5ubKoEGDZOvWrRXr8/PzJScnR3r37l1xb5XdcMMNsmDBArHb7bJ8+XIJDAyUnTt3SllZmSQkJMi2bdskMzNTxo8fL3l5eSIi8vTTT8uTTz5Z7XsjIgLIihUrRETk17/+tTz11FMiIjJnzhxZtGiRiIi88cYbcskll4iIyMUXXyxvv/22iIgsXLiw1q9j5Z9x5aZKi0W+Xijyxx4iTwSLrHxAJD/bZacHkqWWXG1V/dBbWkxMDKGhoWzbto309HSGDx9OaGjoeZ9nwoQJAKxfv97pY2bNmoW3tzdxcXGUlZUxc+ZMAOLi4jhy5Aj79+9n9+7dTJ8+HYCysjK6du1acfyCBQuqnTM3N5e0tDTmzZsHWA+2gPUA1yOPPMJXX32Fh4cHaWlppKen06VLlzrLGB8fz5EjR1i8eDGzZ88+Z9tnn33GihUrKv4yKCoqqqi9VnXRRRfh6+uLr68vERERpKens379eubNm0dAQAAAl112GevWrcNutzNv3jz8/f0BmDt3bq3lu/jiizHGEBcXR2RkJHFxcQAMHjyYI0eOkJqayp49exg7diwAJSUljB49usZz+fj4VHw+kJiYWPEX18aNG1m2bBkA1113HQ888AAAGzZsYOnSpRXrH3zwwRrOqtyaCHz/GfzvEcg+AL2nwsw/QXj/ZitCqw30xy8eXP9OTeCWW25h0aJFnDx5kptuuqna9pdffpl//OMfAKxcuZJu3Wqex/jRRx/l97//PV5eP36Jvby8sNvtANX6I/v6+gLg4eGBt7d3RRc3Dw8PbDYbIsLgwYPZuHFjjdc
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math\n",
"\n",
"#We solve the same problem as before\n",
"\n",
"\n",
"\n",
"t0 = 0\n",
"tf = 1\n",
"N = 5\n",
"\n",
"t = np.linspace(t0,tf,N)\n",
"y = np.zeros(N)\n",
"z = np.zeros(N)\n",
"w = np.zeros(N)\n",
"h = (tf-t0)/N\n",
"\n",
"y[0] = 1\n",
"\n",
"for i in range(0,N-1):\n",
" y[i+1] = y[i]/(1-h)\n",
"\n",
"w = np.exp(t)\n",
"z = abs(y-w)\n",
"\n",
"plt.plot(t,y, label = \"y - Numerical Method method\") #very simple plot of y,z,w\n",
"plt.plot(t,w,label = \"w - Actual\")\n",
"plt.plot(t,z,label = \"z - Error\")\n",
"plt.title(\"Method with N=%d\"%N)\n",
"plt.legend()\n",
"plt.show()\n",
"\n",
"\n",
" \n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0. , 0.25, 0.5 , 0.75, 1. ])"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0\n",
"0.25\n",
"0.5\n",
"0.75\n",
"1.0\n"
]
}
],
"source": [
"for i in t:\n",
" print(i)\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.25, 0.5 , 0.75, 1. ])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t[1:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "7e1998ff7f8aa20ada591c520b972326324e5ea05489af9e422744c7c09f6dad"
},
"kernelspec": {
"display_name": "Python 3.10.1 64-bit",
"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"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}