Last active
April 13, 2022 06:43
-
-
Save lykkin/41c765ad90d353315914107578fc782a to your computer and use it in GitHub Desktop.
euler method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "python", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8" | |
}, | |
"kernelspec": { | |
"name": "python", | |
"display_name": "Pyolite", | |
"language": "python" | |
} | |
}, | |
"nbformat_minor": 4, | |
"nbformat": 4, | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"source": "#Numerical Solutions to 1st-Order ODE via Euler's Method\n#Importing External Libraries\nimport numpy as np\nfrom matplotlib import pyplot as plt\nimport math\n\nfrom ipywidgets import interactive, fixed", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "def euler_method(init_t = 0., init_y = 0., final_t = 0., steps = 1):\n #Step size\n deltat = (final_t - init_t) / steps\n #Define t-values\n x = np.linspace(init_t, final_t, steps+1)\n\n #Initializing array for the slope (dy/dt = m) and y-values\n m = np.zeros([steps+1])\n y = np.zeros([steps+1])\n #For loop for the updated y-value\n y[0] = init_y\n for i in range(1,steps+1):\n m[i-1] = y[i-1] ** 2 - 4 * x[i-1] #dy/dt = your choice (right now, it's t*y)\n y[i] = y[i-1] + m[i-1] * deltat #y_new = y_old + (slope)*(step size)\n print()\n #Printing the data\n t = \"t\"\n Y = \"y\"\n slope = \"f(t,y)\"\n print(f\"{t : ^17}\", f\"{Y : ^12}\", f\"{slope : ^12}\")\n print(\"--------------------------------------------\")\n for i in range (steps+1):\n print(f\"{round(x[i],4) : >10}\",\" | \" , f\"{round(y[i],4) : >10}\", \" | \" , \n f\"{round(m[i],4) : >10}\")\n print(\"--------------------------------------------\")\n\n #Plotting the solution\n plt.plot(x,y, linestyle='-', marker='o')\n plt.locator_params(axis=\"x\", nbins = steps + 1)\n plt.grid(color = 'green', linestyle = '--', linewidth = 0.2)\n plt.xlabel(\"t\")\n plt.ylabel(\"y(t)\")\n plt.title(\"Approximate solution with Euler's Method\")\n plt.show()", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "w=interactive(euler_method, steps=(0, 10, 1))\nw", | |
"metadata": { | |
"trusted": true | |
}, | |
"execution_count": 13, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "interactive(children=(FloatSlider(value=0.0, description='init_t', max=1.0), FloatSlider(value=0.0, descriptio…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "89d5b6a613e44db3aae39a3db4187ecb" | |
} | |
}, | |
"metadata": {} | |
}, | |
{ | |
"execution_count": 13, | |
"output_type": "execute_result", | |
"data": {}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": "", | |
"metadata": {}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ipywidgets==7.7.0 | |
matplotlib==3.5.1 | |
numpy==1.18.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment