首页 > 解决方案 > 将速率方程转换为python代码的问题

问题描述

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

方程的图像

这是一个新更新的代码......我使用 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)

标签: pythonscipynumerical-computing

解决方案


以下是根据您的问题和评论列出的相关问题:

  • 通常,如果总和超过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]可以从总和中取出(但我把它留在那里以匹配方程)。

最后说明:我看到您是 SO 的新手,所以我认为如果我为您回答这个问题会有所帮助。将来,请对代码进行一些尝试——这真的很有帮助。


推荐阅读