'Matrix infexing in for loop
I have been trying to write a code that simulated the trajectory of certain fluxies, and to analyse how the distance after 5 periods changes depending on the initial vertical position (in the code, zL[0]) and the period of oscillation (in the code, T). The distances are recorded in 12x8 matrix (DeltaX matrix), with the columns representing the initial z, and the rows the periods. I ran across a problem that I can't seem to understand: it seems as if thw rows of the final matrix are all identical. With a debugger, it looks like at each iteration, the code is writing the result not just in the matrix point (DeltaX[i][j]) but in a whole column. This is the code
T_arr=np.arange(4,20,2) #8
z0_arr=np.arange(-3,-0.1,0.25) #12
DeltaX=[[0]*12]*8 #columns are the initial z, rows are the periods
dt=0.01
t=0
i=0
while i<=7: #iteration over T
for j in range(12): #iteration over z0
n_points=int((T_arr[i]*5)/dt)
Omega=(2*np.pi)/T_arr[i]
k=(Omega**2)/g
xL=[0]*n_points
zL=[0]*n_points
xL[0]=0
zL[0]=z0_arr[j]
t=0
l=0
for l in range(1,n_points) :
xL[l]=xL[l-1]+Omega*(np.exp(k*zL[l-1]))*np.cos(k*xL[l-1]-Omega*t)*dt
zL[l]=zL[l-1]+Omega*(np.exp(k*zL[l-1]))*np.sin(k*xL[l-1]-Omega*t)*dt
t=t+dt
DeltaX[i][j]=xL[-1]-xL[0]
i=i+1
DeltaX
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
