integrate.cumtrapz返回空数组

时间:2017-12-01 12:56:13

标签: python numpy scipy integrate

我想集成一个函数,但integrate.cumtrapz函数返回一个空数组:

from scipy import integrate
import numpy as np

def f(x):
    return x**5/(np.exp(x)-1)

xmax = 20
dx = 0.1
x = np.arange(0, xmax+dx, dx)

def int_soleil(x, dx):
    y = f(x)
    y_int = integrate.cumtrapz(y, x)
    return y_int

当我去打印x数组和y数组时,它显示所有条目都很好;我不明白为什么y_int显示一个带有一堆nan条目的数组而不是一个简单的值。请帮忙!!!

1 个答案:

答案 0 :(得分:0)

f(0)NaN,因为您除以0。因此,跟随trapezian规则的每个后续值也将是NaN。以值>0开始集成是有意义的,例如x = np.arange(0.1, xmax+dx, dx)

import matplotlib.pyplot as plt
from scipy import integrate
import numpy as np

def f(x):
    return x**5/(np.exp(x)-1)

xmax = 20
dx = 0.1
x = np.arange(0.1, xmax+dx, dx)

def int_soleil(x, dx):
    y = f(x)
    y_int = integrate.cumtrapz(y, x)
    return y_int

y_int = int_soleil(x,dx)

plt.plot(x,f(x), label="f(x)")
plt.plot(x[:-1],y_int, label="$\int_{0.1}^{x} f(t) dt$")
plt.legend()
plt.show()

enter image description here