将速率方程转换为python代码的问题

2024-06-28 15:21:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将这些速率方程转换为python代码,我做了大量的研究,但似乎无法获得任何明确的路径来实现这一点,请提供任何帮助将不胜感激

Image of Equations

这是一个最新更新的代码…我用Tom10的quide写的…请你怎么看

    import numpy as np
# import numpy as sum  # not necessary, just for convenience, and replaces the builtin

# set N_core value
N_CORE = 0

# set the initial conditions appropriately (you need to set these correctly)
N = np.ones(8)
r = np.ones((8, 8))
dN = np.zeros(8) # the value here is not important for your equations

# set constant for equation 1
R_P1abs37 = 20
F_P1 = 20
R_P1abs47 = 40
W_3317 = 1.0

# set constant for equation 2
W_6142 = 90
W_5362 = 80

# Set you constants appropriately for equation 3
R_P2abs35 = 30
F_P2 = 40
R_L2se34 = 50
F_L2 = 90

# equation 4 constants
W_2214 = 20

#equation 5 constants
R_P1abs13 = 30
R_L2se32 = 20
F_L1 = 10

# equation 1 formular
dN[7] =sum(r[7,:]*N[7]) + (R_P1abs37*F_P1) + (R_P1abs47*F_P1) + (W_3317*N[3]**2)

# equation 2 formular
dN[6] = (r[7,6]*N[7]) - sum(r[6,:]*N[6]) - (W_6142*N[6]*N[1]) + (W_5362*N[5]*N[3])

#equation 3 formular
dN[5] = sum(r[:,5]*N) - sum(r[5,:]*N[5]) + R_P2abs35*F_P2 - R_L2se34*F_L2 - W_5362*N[5]*N[3]

# equation 4 formular
dN[4] = sum(r[:,4]*N) - sum(r[4,:]*N[4]) - (R_P1abs47*F_P1) + (R_L2se34*F_L2) + (W_2214*N[2]**2)+ (W_6142*N[6]*N[1])

#equation 5 formular
dN[3] = sum(r[:,3]*N) - sum(r[3,:]*N[3]) + (R_P1abs13*F_P1) - (R_P1abs37*F_P1) - (R_P2abs35*F_P2)
-(R_L2se32*F_L1) - ((2*W_3317)*N[3]**2) - (W_5362*N[5]*N[3])

#equation 6 formular
dN[2] = sum(r[:,2]*N) - (r[2,1]*N[2]) + (R_L2se32*F_L1) - ((2*W_2214)*N[2]**2) + (W_6142*N[6]*N[1])+(W_5362*N[5]*N[3])


#equation 7 formular
dN[1] = sum(r[:,1] * N) - (R_P1abs13*F_P1) + (W_2214*N[2]**2) + (W_3317+N[3]**2) - (W_6142+N[6]*N[1])

#equation for N CORE
N_CORE = sum(dN)


print(N_CORE)

Tags: thecorefornpsump2dnset
1条回答
网友
1楼 · 发布于 2024-06-28 15:21:51

以下是根据您的问题和意见列出的相关问题:

  • 通常,如果总和超过i,那么没有i下标的所有东西对于该总和都是常数。(从数学上讲,这些常数项可以从和中带出来;因此第一个方程有点奇怪,其中N_7可以从和中移出,但我认为它们保持不变,以显示与其他方程的对称性,这些方程都有r*N项)

  • capitol sigma符号(∑)表示需要进行求和,这可以在循环中进行,但Python list和numpy都有求和函数。Numpy还有一个额外的优点,即乘法被解释为单个元素的乘法,使得表达式更容易。所以对于a[0]*[b0] + a[1]*b[1] + a[2]*b[2]和numpy数组,它只是sum(a*b),而对于Python列表,它是sum([a[i]*b[i] for in range(len(a))]

因此,使用numpy,设置和第三个等式如下所示:

import numpy as np
import numpy.sum as sum  # not necessary, just for convenience, and replaces the builtin

# set the initial conditions appropriately (you need to set these correctly)
N = np.ones(7, dtype=np.float)
# r seems to be a coupling matrix, and should be set according to your system
r = np.ones((7, 7), dtype = np.float) 
# the values for dN are not important for your equations because dN only appears on the left side of the equations, so we just make a place to store the results
dN = np.zeros(7, dtype=np.float) 

# Set you constants appropriate.y
R_P2abs35 = 1.0
F_P2 = 1.0
R_L2se34 = 1.0
F_L2 = 1.0
W_5362 = 1.0

dN[5] = sum(r[:,5]*N) - sum(r[5,:]*N[5]) + R_P2abs35*F_P2 - R_L2se34*F_L2 - W_5362*N[5]*N[3]

请注意,尽管总和中的表达式看起来相似,但第一个基本上是两个向量之间的点积,第二个是标量乘以向量,因此可以从总和中取出N[5](但我将其保留在此处以匹配方程式)

最后一点:我知道你是S.O.的新手,所以我想如果我能帮你回答这个问题会很有帮助。在将来,请展示一些代码的尝试,它真的很有帮助

相关问题 更多 >