欧拉在python中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27994660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Euler's method in python
提问by newpythonuser
I'm trying to implement euler's methodto approximate the value of e in python. This is what I have so far:
我正在尝试实现euler 的方法来逼近 python 中 e 的值。这是我到目前为止:
def Euler(f, t0, y0, h, N):
t = t0 + arange(N+1)*h
y = zeros(N+1)
y[0] = y0
for n in range(N):
y[n+1] = y[n] + h*f(t[n], y[n])
f = (1+(1/N))^N
return y
However, when I try to call the function, I get the error "ValueError: shape <= 0". I suspect this has something to do with how I defined f? I tried inputting f directly when euler is called, but gave me errors related to variables not being defined. I also tried defining f as its own function, which gave me a division by 0 error.
但是,当我尝试调用该函数时,出现错误“ValueError: shape <= 0”。我怀疑这与我如何定义 f? 我尝试在调用 euler 时直接输入 f,但给了我与未定义变量相关的错误。我还尝试将 f 定义为它自己的函数,这给了我除以 0 的错误。
def f(N):
for n in range(N):
return (1+(1/n))^n
(not sure if N was the appropriate variable to use here...)
(不确定 N 是否适合在这里使用的变量...)
回答by mty
Are you sure you are not trying to implement the Newton's method? Because Newton's method is used to approximate the roots.
您确定您不是在尝试实施牛顿法吗?因为牛顿法是用来逼近根的。
In case you decide to go with Newton's method, here is a slightly changed version of your code that approximates the square-root of 2. You can change f(x)
and fp(x)
with the function and its derivative you use in your approximation to the thing you want.
如果您决定采用牛顿方法,这里是您的代码的稍微更改版本,它近似于 2 的平方根。您可以更改f(x)
并fp(x)
使用您在近似中使用的函数及其导数来接近您想要的东西。
import numpy as np
def f(x):
return x**2 - 2
def fp(x):
return 2*x
def Newton(f, y0, N):
y = np.zeros(N+1)
y[0] = y0
for n in range(N):
y[n+1] = y[n] - f(y[n])/fp(y[n])
return y
print Newton(f, 1, 10)
gives
给
[ 1. 1.5 1.41666667 1.41421569 1.41421356 1.41421356
1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]
[ 1. 1.5 1.41666667 1.41421569 1.41421356 1.41421356
1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]
which are the initial value and the first ten iterations to the square-root of two.
它们是初始值和前十次迭代到 2 的平方根。
Besides this a big problem was the usage of ^
instead of **
for powers which is a legal but a totally different (bitwise) operation in python.
除此之外,一个大问题是在 python 中使用^
而不是**
for powers 是合法但完全不同的(按位)操作。
回答by Paul
The formula you are trying to use is not Euler's method, but rather the exact value of e as n approaches infinity wiki,
您尝试使用的公式不是欧拉的方法,而是当 n 接近无穷大时 e 的确切值wiki,
$n = \lim_{n\to\infty} (1 + \frac{1}{n})^n$
Euler's methodis used to solve first order differential equations.
欧拉方法用于求解一阶微分方程。
Here are two guides that show how to implement Euler's method to solve a simple test function: beginner's guideand numerical ODE guide.
这里有两个指南,展示了如何实现欧拉方法来求解一个简单的测试函数:初学者指南和数值 ODE 指南。
To answer the title of this post, rather than the question you are asking, I've used Euler's method to solve usual exponential decay:
为了回答这篇文章的标题,而不是你问的问题,我使用欧拉的方法来解决通常的指数衰减:
$\frac{dN}{dt} = -\lambda N$
Which has the solution,
哪位有解决办法,
$N(t) = N_0 e^{-\lambda t}$
Code:
代码:
import numpy as np
import matplotlib.pyplot as plt
from __future__ import division
# Concentration over time
N = lambda t: N0 * np.exp(-k * t)
# dN/dt
def dx_dt(x):
return -k * x
k = .5
h = 0.001
N0 = 100.
t = np.arange(0, 10, h)
y = np.zeros(len(t))
y[0] = N0
for i in range(1, len(t)):
# Euler's method
y[i] = y[i-1] + dx_dt(y[i-1]) * h
max_error = abs(y-N(t)).max()
print 'Max difference between the exact solution and Euler's approximation with step size h=0.001:'
print '{0:.15}'.format(max_error)
Output:
输出:
Max difference between the exact solution and Euler's approximation with step size h=0.001:
0.00919890254720457
Note: I'm not sure how to get LaTeX displaying properly.
注意:我不确定如何正确显示 LaTeX。