python Numdifftool的有限差分导数(Fornberg方法)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Numdifftool的有限差分导数(Fornberg方法)相关的知识,希望对你有一定的参考价值。
import numpy as np
import numdifftools.fornberg as ndf
import matplotlib.pyplot as plt
from random import random
def plot(outfile, x, fx=None, dfx=None, dfx_ref=None):
plt.figure()
if fx is not None:
plt.plot(x, fx, label=r"$f(x)$", marker="*")
if dfx is not None:
plt.plot(x, dfx, label=r"$f'(x)$ (calculated)", marker="+")
if dfx_ref is not None:
plt.plot(x, dfx_ref, label=r"$f'(x)$ (reference)", marker="x")
plt.legend()
plt.xlabel("$x$")
plt.savefig(outfile)
plt.close()
def noisify(x, multiplier=1.):
xmin, xmax = x.min(), x.max()
deltax = xmax - xmin
n = len(x)
h = deltax / float(n-1)
for i in range(len(x)):
x[i] = x[i] * (1. + (2. * random() - 1.) / (float(n-1)) * multiplier)
return x
# 1st order differentiation with even grid spacing
x = np.linspace(-1, 1, 21)
fx = np.tanh(x)
dfx = ndf.fd_derivative(fx, x, n=1)
dfx_ref = 1. - np.power(np.tanh(x), 2)
plot("even_tanh.png", x, fx, dfx, dfx_ref)
x = np.linspace(-np.pi, np.pi, 21)
fx = np.sin(x)
dfx = ndf.fd_derivative(fx, x, n=1)
dfx_ref = np.cos(x)
plot("even_sin.png", x, fx, dfx, dfx_ref)
x = np.linspace(0, 1, 21)
fx = np.exp(x)
dfx = ndf.fd_derivative(fx, x, n=1)
plot("even_exp.png", x, fx, dfx)
# 1st order differentiation with uneven (randomized) grid spacing
x = np.sort(noisify(np.linspace(-1, 1, 21), 5.))
fx = np.tanh(x)
dfx = ndf.fd_derivative(fx, x, n=1)
dfx_ref = 1. - np.power(np.tanh(x), 2)
plot("uneven_tanh.png", x, fx, dfx, dfx_ref)
x = np.sort(noisify(np.linspace(-np.pi, np.pi, 21), 5.))
fx = np.sin(x)
dfx = ndf.fd_derivative(fx, x, n=1)
dfx_ref = np.cos(x)
plot("uneven_sin.png", x, fx, dfx, dfx_ref)
x = np.sort(noisify(np.linspace(0, 1, 21), 5.))
fx = np.exp(x)
dfx = ndf.fd_derivative(fx, x, n=1)
plot("uneven_exp.png", x, fx, dfx)
以上是关于python Numdifftool的有限差分导数(Fornberg方法)的主要内容,如果未能解决你的问题,请参考以下文章
通过有限差分求求解较复杂的微分方程及matlab仿真
FDM, FVM, FEM
通过有限差分求求解较复杂的微分方程及matlab仿真
边缘检测之孤立点检测及Python实现
用于计算光流空间导数的 Python 库
差分近似图像导数算子之Laplace算子