微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将函数作为数组元素传递

如何解决如何将函数作为数组元素传递

这是更新后的帖子:我提供了完整的代码在这代码中,我是常数。我试图使 I 成为可以使用此代码生成的高斯函数

高斯代码

mean = 0

standard_deviation = 1
x_values = np.arange(-5,5,0.1)
y_values = scipy.stats.norm(mean,standard_deviation)
plt.plot(x_values,y_values.pdf(x_values))

我想将常量 I 更改为上面的曲线的主要代码,即 I 不再是常量而是 I(t) 形式的高斯曲线:

from scipy import *

from scipy.integrate import ode

from pylab import *

import matplotlib.pyplot as plt

 

### Simualtion Outputs ###

N = []      # y[0] Carrier concentration

S = []      # y[1] Photon concentration

 

 

 

### Simualtion input  parameters ###

I = 2.0e-2                                      # p[0] Pumping current (A)

q = 1.6e-19                                     # p[1] Electron charge (C)

V = 9e-11                                       # p[2] Device volume (cm^3)

tn = 1.0e-9                                     # p[3] Carrier relaxation time in seconds (s)

g0 = 1.5e-5                                     # p[4] Gain slope constant (cm^3/s)

Nth = 1e18                                      # p[5] Threshold carrier density (cm^-3)

EPS = 1.5e-17                                   # p[6] Gain compression factor (cm^3)

Gamma = 0.3                                     # p[7] Confinement factor

tp = 1.0e-12                                    # p[8] Photon lifetime in cavity (s)

Beta = 1.0e-4                                   # p[9] Spontaneous Emission Factor

 

### Define equations to be solved

def Laser_rates(t,y,p):

   

    I = p[0]; q = p[1]; V = p[2]; tn = p[3]; g0 = p[4]; Nth = p[5]; EPS = p[6];  Gamma = p[7]; tp = p[8]; Beta = p[9]

    dy = zeros([2])

    dy[0] = (I/(q* V)) - (y[0]/tn) -  g0*(y[0] - Nth)*(y[1]/(1 + EPS* y[1]))

    dy[1] = Gamma* g0* (y[0] - Nth)*(y[1]/(1 + EPS* y[1])) - y[1]/tp + (Gamma* Beta* y[0]) / tn

   

    return dy

   

 

### Time and initial conditions ### 

t0 = 0; tEnd = 1e-9; dt = 1e-12             #Time constraints

y0 = [4.78e18,1e12]                                 #Initial conditions

Y=[]; T=[]                                  #Create empty lists

 

p = [41.8742e-2,1.6e-19,9e-11,5e-9,1.5e-5,5.75e18,1.5e-17,0.2,1.33e-12,0.0001]             #Parameters for odes

#p = [ I,q,V,tn,g0,Nth,EPS,Gamma,tp,Beta]

 

#Setup integrator with desired parameters

r = ode(Laser_rates).set_integrator('vode',method = 'bdf')

r = ode(Laser_rates).set_integrator('dopri5',nsteps = 1e4)

r.set_f_params(p).set_initial_value(y0,t0)

 

### Simualtion check ###

while r.successful() and r.t+dt < tEnd:

    r.integrate(r.t + dt)

    Y.append(r.y)        #Makes a list of 1d arrays

    T.append(r.t)

 

### Format output ###

Y = array(Y)             #Convert from list to 2d array

N = Y[:,0]

S = Y[:,1]

 

 

### Plotting ###

f,axarr = plt.subplots(2,sharex=True) # Two subplots,the axes array is 1-d

axarr[0].plot(T,N)

axarr[0].set_ylabel("Carrier Conc (cm^-3)")

axarr[0].set_title('Laser-Rate Simulation')

axarr[1].plot(T,S)

axarr[1].set_ylabel("Photon Conc (cm^-3)")

axarr[1].set_xlabel("Time (s)")

show()

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。