我有以下功能:
def sbeta(beta,gamma,y):
k = 2/(np.pi)**2.
return k * np.sqrt(1 - (np.sqrt(1-y**2.)*np.sin(beta)*np.cos(gamma) - y*np.cos(beta))**2.)
其中beta是常数,y定义在-1和1之间:
beta = 23.4
y = numpy.linspace(-1, 1, 100)
我想绘制此函数的积分,用于从0到2pi评估的伽玛:
def integral(beta,gamma,y):
for i in range(len(y)):
I = integrate.quad(sbeta, 0., 2*np.pi, args=(beta, y[i]))
print(I)
plt.plot(y[i],I[0])
gamma = np.linspace(0., 2*np.pi, 10)
integral(beta,gamma,y)
plt.show()
此时没有错误,但我认为这不正确。我想计算γ的积分作为0到2pi的变量,但y是一个离散数组。如何计算和绘制从-1和1的10 y值?我应该使用scipy.integrate.cumtrapz吗?
答案 0 :(得分:0)
这是我对答案的尝试:
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
def f(gamma, y):
k = 2/(np.pi)**2.
beta = 23.4*(np.pi/180.)
return k * np.sqrt(1 - (np.sqrt(1-y**2.)*np.sin(beta)*np.cos(gamma) - y*np.cos(beta))**2.)
y = np.linspace(-1., 1.,10)
low = 0.
high = 2*np.pi
Ivals = []
for i in range(len(y)):
I = integrate.quad(f, low, high, args=(y[i]))
plt.scatter(y[i],I[0])
Ivals.append(I)
plt.show()
显示以下图表:
任何人都可以确认这是否正确吗?