{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value. It is the most basic explicit method for numerical integration of ordinary differential equations and is the simplest Runge–Kutta method. The Euler method is named after Leonhard Euler, who treated it in his book Institutionum calculi integralis (published 1768–1870)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt \n",
"import numpy as np\n",
"import math\n",
"from math import tan"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def Euler(x,x1,y,n):\n",
" h=(x1-x)/n\n",
" for i in range(1,n+1):\n",
" F1=F(x,y)\n",
" y+=F1*h\n",
" x+=h\n",
" Xlist.append(float(x))\n",
" Ylist.append(y) \n",
" print(i,\" \",x,\" \",i,\" \",y,\" \",(tan(x)-x))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Euler method\n",
"1 1.1 1 1.3718281828459045 0.8647596572486522\n",
"2 1.2000000000000002 2 1.7821170033074434 1.37215162212632\n",
"3 1.3000000000000003 3 2.2370051138662332 2.3021024479679815\n",
"4 1.4000000000000004 4 2.7429592150697215 4.397883715482902\n",
"5 1.5000000000000004 5 3.306912277065599 12.601419947171808\n",
"6 1.6000000000000005 6 3.93638277612803 -35.832532735556796\n",
"7 1.7000000000000006 7 4.6395855802655275 -9.396602139459121\n",
"8 1.8000000000000007 8 5.425541043687798 -6.086261674628051\n",
"9 1.9000000000000008 9 6.304186673857298 -4.8270975146777655\n",
"10 2.000000000000001 10 7.28649452241562 -4.185039863261515\n"
]
}
],
"source": [
"#simple example for checking, how this method work\n",
"def F(X,Y):\n",
" #return X**2 - Y*2#\n",
" return Y + (math.e**X)/X\n",
"Xlist =[]\n",
"Ylist =[]\n",
"x=1.\n",
"x1=2.\n",
"n=10\n",
"y=1\n",
"print(\"Euler method\")\n",
"Euler(x,x1,y,n)"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
在数学和计算科学中,欧拉方法(也称为向前欧拉方法)是求解具有给定初始值的常微分方程(ODE)的一阶数值方法。它是常微分方程数值积分的最基本显式方法,也是最简单的龙格-库塔方法。欧拉方法以莱昂哈德·欧拉的名字命名,他在其著作《积分计算机构》(出版于 1768-1870 年)中论述了该方法。
import matplotlib.pyplot as plt
import numpy as np
import math
from math import tan
def Euler(x,x1,y,n):
h=(x1-x)/n
for i in range(1,n+1):
F1=F(x,y)
y+=F1*h
x+=h
Xlist.append(float(x))
Ylist.append(y)
print(i," ",x," ",i," ",y," ",(tan(x)-x))
#simple example for checking, how this method work
def F(X,Y):
#return X**2 - Y*2#
return Y + (math.e**X)/X
Xlist =[]
Ylist =[]
x=1.
x1=2.
n=10
y=1
print("Euler method")
Euler(x,x1,y,n)
Euler method
1 1.1 1 1.3718281828459045 0.8647596572486522
2 1.2000000000000002 2 1.7821170033074434 1.37215162212632
3 1.3000000000000003 3 2.2370051138662332 2.3021024479679815
4 1.4000000000000004 4 2.7429592150697215 4.397883715482902
5 1.5000000000000004 5 3.306912277065599 12.601419947171808
6 1.6000000000000005 6 3.93638277612803 -35.832532735556796
7 1.7000000000000006 7 4.6395855802655275 -9.396602139459121
8 1.8000000000000007 8 5.425541043687798 -6.086261674628051
9 1.9000000000000008 9 6.304186673857298 -4.8270975146777655
10 2.000000000000001 10 7.28649452241562 -4.185039863261515