不同分数阶导数程序
时间: 2023-12-18 14:02:17 浏览: 55
关于分数阶的matlab程序
4星 · 用户满意度95%
以下是不同分数阶导数的程序实现:
1. 一阶导数:
```python
import numpy as np
def first_derivative(f, h):
n = len(f)
df = np.zeros(n)
for i in range(1, n-1):
df[i] = (f[i+1] - f[i-1]) / (2*h)
df[0] = (f[1] - f[0]) / h
df[n-1] = (f[n-1] - f[n-2]) / h
return df
```
2. 二阶导数:
```python
import numpy as np
def second_derivative(f, h):
n = len(f)
d2f = np.zeros(n)
for i in range(1, n-1):
d2f[i] = (f[i+1] - 2*f[i] + f[i-1]) / (h**2)
d2f[0] = (f[2] - 2*f[1] + f[0]) / (h**2)
d2f[n-1] = (f[n-3] - 2*f[n-2] + f[n-1]) / (h**2)
return d2f
```
3. 分数阶导数:
```python
import numpy as np
from scipy.special import gamma
def frac_derivative(f, alpha, h):
n = len(f)
df = np.zeros(n)
for i in range(1, n-1):
df[i] = sum([(f[i+j+1] - f[i+j-1]) / (2*h) for j in range(-i, i)]) / (h**alpha * gamma(alpha+1))
df[0] = sum([(f[i+1] - f[i-1]) / (2*h) for i in range(alpha)]) / (h**alpha * gamma(alpha+1))
df[n-1] = sum([(f[n-1-i-1] - f[n-1-i+1]) / (2*h) for i in range(alpha)]) / (h**alpha * gamma(alpha+1))
return df
```
其中,alpha 表示分数阶导数的阶数,gamma 函数为阶乘的推广。
阅读全文