I've generated a variable names for pN from N=0 to n:
p0, p1, p2, p3, p4,....,pn
So far I got:
P = ['p0', 'p1', 'p2',..., 'pn']
What I want to do now is convert them into variable names to be called upon and used in a function. Convert 'p0' to p0, and so on for all of the values in
P = ['p0', 'p1', 'p2',..., 'pn']
So it becomes
P = [p0, p1, p2,..., pn]
Then generate functions using the variables:
Fi = P[i]/2
ie:
F0 = p0/2
F1 = p1/2
F2 = p2/2
Problem is I don't know how to convert 'pn' to pn properly, then generate functions based on pn. How do I do that? Or rather, what am I doing wrong?
Here's what I got:
# 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
Note: I want to keep my variables as variables because I'm using the functions to solve a system of differential equations. My goal is to create a list of differential equations with the same format.