Skip to content

Instantly share code, notes, and snippets.

@KhanradCoder
Created April 17, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KhanradCoder/64e79ae98f48768ada0b78417746624f to your computer and use it in GitHub Desktop.
Save KhanradCoder/64e79ae98f48768ada0b78417746624f to your computer and use it in GitHub Desktop.
Euler's Method.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Euler's Method.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPaq4nKqYUGjqYBroTeTTeo",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/KhanradCoder/64e79ae98f48768ada0b78417746624f/euler-s-method.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "87MX6R19vPo1",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np \n",
"import matplotlib.pyplot as plt"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ItHt1o_2m7_Z",
"colab_type": "text"
},
"source": [
"In the y_actual function where it says return, you can input the function you want to estimate. In this case, I'm using x^2 for simplicity, however you can also use an equation in terms of y and x. The y_derivative function is simply the derivative of the y_actual function, which in this case is 2x "
]
},
{
"cell_type": "code",
"metadata": {
"id": "Zs6uh3CXvscc",
"colab_type": "code",
"colab": {}
},
"source": [
"def y_actual(x=0,y=0):\n",
" return(x**2)\n",
"\n",
"def y_derivative(x=0, y=0):\n",
" return (2*x)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "GHT0SI9Bnw5j",
"colab_type": "text"
},
"source": [
"Next, I wrote the function for Euler's method.there are many parameters I could have taken in, but I ultimately decided on the x value we want to estimate, the number of steps we want to take, and an initial y value."
]
},
{
"cell_type": "code",
"metadata": {
"id": "gEhJ4_HBxvkk",
"colab_type": "code",
"colab": {}
},
"source": [
"def eulers_method(x, n):\n",
" y_hat = 0\n",
" h = x/n\n",
" for i in range(n):\n",
" y_hat += h*y_derivative(h*i,0,0)\n",
" return(y_hat)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "KApZv7XFoMy5",
"colab_type": "text"
},
"source": [
"Finally, I printed the results of Euler's approximation, the actual solution and the percent error. You can see that as the steps increase, the error decreases."
]
},
{
"cell_type": "code",
"metadata": {
"id": "ueHLgZ393HKM",
"colab_type": "code",
"outputId": "0f757d30-f1ed-4212-9af9-3c0634562f44",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 71
}
},
"source": [
"x = 5\n",
"steps = 1000000\n",
"\n",
"print(\"Euler's method approximation: \"+str(eulers_method(x, steps)))\n",
"print(\"Actual solution: \"+str(y_actual(x)))\n",
"print(\"Error: \"+str(np.abs((y_actual(x)-eulers_method(x, steps))/eulers_method(x, steps))*100)+\"%\")"
],
"execution_count": 53,
"outputs": [
{
"output_type": "stream",
"text": [
"Euler's method approximation: 24.999975000000006\n",
"Actual solution: 25\n",
"Error: 0.00010000009997499789%\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment