嗨,我想整合一个从0到几个不同上限(大约1000)的函数.我已经使用for循环编写了一段代码来执行此操作,并将每个值附加到空数组.然而,我意识到我可以通过做更小的积分然后将前一个积分结果添加到刚刚计算的结果来使代码更快.所以我会做相同数量的积分,但是在更小的间隔内,然后只需添加前一个积分就可以得到从0到上限的积分.这是我的代码:
import numpy as np #importing all relevant modules and functions from scipy.integrate import quad import pylab as plt import datetime t0=datetime.datetime.now() #initial time num=np.linspace(0,10,num=1000) #setting up array of values for t Lt=np.array([]) #empty array that values for L(t) are appended to def L(t): #defining function for L return np.cos(2*np.pi*t) for g in num: #setting up for loop to do integrals for L at the different values for t Lval,x=quad(L,0,g) #using the quad function to get the values for L. quad takes the function, where to start the integral from, where to end the integration Lv=np.append(Lv,[Lval]) #appending the different values for L at different values for t
我建议的优化技术需要做哪些更改?