我为pN生成了一个从N=0到N的变量名:
p0, p1, p2, p3, p4,....,pn
到目前为止我得到了:
P = ['p0', 'p1', 'p2',..., 'pn']
我现在要做的是将它们转换成变量名,以便在函数中调用和使用。 将“p0”转换为p0,依此类推
P = ['p0', 'p1', 'p2',..., 'pn']
所以它变成了
P = [p0, p1, p2,..., pn]
然后使用变量生成函数:
Fi = P[i]/2
即:
F0 = p0/2
F1 = p1/2
F2 = p2/2
问题是我不知道如何正确地将pn转换成pn,然后基于pn生成函数。我该怎么做?或者更确切地说,我做错了什么?你知道吗
我得到的是:
# Define the constants
# ====================
k = 1
m = 1
# Generate the names
# ==================
n = 10 # Number of terms
P = [] # Array for momenta names
Q = [] # Array for positon names
for i in range (n): # Generate the names for
p = "p"+str(i) # momentum
q = "q"+str(i) # positon
# Convert the names into variales
exec("%s = %d" % p)
exec("%s = %d" % q)
# Put the names into the arrays
P.append(p)
Q.append(q)
# Print the names out for error checking
print(P)
print(Q)
# Generate the functions
# ======================
Q_dot = []
for i in range(n):
def Q_dot(P[i]):
q_dot = P[i] / m
Q_dot.append(q_dot)
i += 1
注意:我想把变量作为变量,因为我用函数来解微分方程组。我的目标是创建一个具有相同格式的微分方程列表。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐