'Problem to integrate a function with multiple array in python
I work on a Python Script and I need to integrate a very long function. The function contains several array and I don't succeed to integrate it. I need to integrate it on teta1 (80,) but it contains also w (22,),f (22,),Gam_Mag (22,). I tryed to decompose it but I'm not sure if it's good or not. Here it's my function:
def function1(teta1,w,f,Mag_Gam):
Zp1=i*w*ms1*(1-(1+i*Nu)*np.sin(teta1)**4*(f/fc1)**2)
a1=1
b1=Zp1
c1=0
d1=1
ac=np.cosh(Mag_Gam*d*np.cos(teta1))
bc=Zc*np.sinh(Mag_Gam*d*np.cos(teta1))/np.cos(teta1)
cc=np.sinh(Mag_Gam*d*np.cos(teta1))/Zc*np.cos(teta1)
dc=np.cosh(Mag_Gam*d*np.cos(teta1))
Zp2=i*w*ms2*(1-(1+i*Nu)*np.sin(teta1)**4*(f/fc2)**2)#B1/(i*w)*(k0**4*np.sin(teta)**4-kb1**4)
a2=1
b2=Zp2
c2=0
d2=1
at=a1*ac+b1*cc
bt=a1*bc+b1*dc
ct=c1*ac+d1*cc
dt=c1*bc+d1*dc
aT=at*a2+bt*c2
bT=at*b2+bt*d2
cT=ct*a2+dt*c2
dT=ct*b2+d2*dt
T=np.array([[aT,bT],[cT,dT]])
return 4*abs(T[0,0]+T[0,1]/Zc+Zc*T[1,0]+Zc*T[1,1]/Zc)**(-2)
Here the integration:
Tau=np.zeros(len(f))
err=np.zeros(len(f))
for n in range(len(f)):
Tau,err=quad(function1(teta1,w[n],f[n],Mag_Gam[n]),0,80,args=(1,)) #Integration on teta1
TL=10*np.log10(1/Tau)
Thank you in advance for your help and sorry for my english. Clément
Solution 1:[1]
If you were to do this part step-by-step:
Tau,err=quad(function1(teta1,w[n],f[n],Mag_Gam[n]),0,80,args=(1,)) #Integration on teta1
You'd notice that you're giving quad
(which I assume is scipy.integrate.quad
) not a callable function, but an array of already computed values to work with!
Try this, instead, passing the function object (and also actually remembering to set values within your output arrays, instead of overwriting them with single numbers):
Tau[n], err[n] = quad(function1, 0,80,args=(w[n],f[n],Mag_Gam[n]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dominik Sta?czak |