I'm currently trying to improve my python skills by using it to recreate some old matlab projects I did in undergrad. I once used matlab to simulate a proton attracting an electron, using multidimensional arrays to store the positions and velocities for each time step. Now I'm trying to do the same thing in Python, but the way in which arrays work is very different from the way they're used in matlab. I'm running into a lot of errors and figuring things out as I go, but I think I'm stuck thinking about arrays in the "matlab way" and some points on how to accomplish this in python would be greatly appreciated.
#This code is for plotting the trajectory of an electron travelling through space#The particle is in the vacinity of a central force (A proton at the origin)import numpy as npimport matplotlib.pyplot as pltre = np.array([[1e-11],[1e-11]]) #let re denote the trajectory of the electron with x = r[0] and y = r[1]m = 9.11e-31 #mass of electronk = 8.99e9 #Coulomb's constant [N m^2/C^2]q = 1.6e-19 #charge of electron and proton [C]rp = [0,0] #rp is the position of the protondt = 0.001 #time differential [s]v = np.array([[-3e12],[0]]) #the electron has initial velocity of v = (-3, 0) [m/s]phi = np.arctan2(re[1][0], re[0][0]) #starting anglefor i in range(1,10): # nrex = (re[0][i-1])+v[0][i-1]*dt #nuew position in x # nrey = (re[1][i-1])+v[1][i-1]*dt #new position in y re[0] = np.append(re[0], ((re[0][i-1])+v[1][i-1]*dt), axis=1) #, axis=1) #for each timestep move the velocity in x re[1] = np.append(re[1], ((re[1][i-1])+v[1][i-1]*dt), axis=1) #for each timestep mobe the velocity in y phi = np.arctan2(re[1][i],re[0][i]) #update the angle rho = np.sqrt(re[0][i]**2 + re[1][i]**2) #update separation from proton v[0] = np.append(n[0], (v[0][i-1]+((k*(q**2)/(rho**2))/m)*np.cos(phi)*dt), axis=1) #update velocity in x v[1] = np.append(v[1], (v[1][i-1]+((k*(q**2)/(rho**2))/m)*np.sin(phi)*dt), axis=1) #update velocity in yplt.scatter(re[0][:], re[1][:], s=2, c='b') #Plot electron's trajectoryplt.scatter(rp[0],rp[1], s=3, c='r') #Show proton's positionplt.show() #Show
Basically what I'm trying to do is add the next "state" of the system at the end of every "vector" within an array (one for position and one for velocity, both containing components for x and y), and finally plotting each time state to see the entire trajectory. This however returns the following error:
Traceback (most recent call last): File "c:\Users\hecto\OneDrive\Documentos\ITESM\8vo semestre\Repaso Python\ParticleInElectricField.py", line 20, in <module> re[0] = np.append(re[0], ((re[0][i-1])+v[1][i-1]*dt), axis=1) #, axis=1) #for each timestep move the velocity in x File "<__array_function__ internals>", line 200, in append File "C:\Users\hecto\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\function_base.py", line 5499, in append return concatenate((arr, values), axis=axis) File "<__array_function__ internals>", line 200, in concatenatenumpy.AxisError: axis 1 is out of bounds for array of dimension 1PS C:\Users\hecto\OneDrive\Documentos\ITESM\8vo semestre\Repaso Python>
Can you not append to a specific "vector" within an array? Or am I missing something. Any pointers help, thanks in advance!
When I tried the same code withoue the "axis=1" i returned an erro about the dimensions of the array. This is probably because without it, np.append flattens the vector. When I try "axis=0", the error becomes:
Traceback (most recent call last): File "c:\Users\hecto\OneDrive\Documentos\ITESM\8vo semestre\Repaso Python\ParticleInElectricField.py", line 20, in <module> re[0] = np.append(re[0], ((re[0][i-1])+v[1][i-1]*dt), axis=0) #, axis=1) #for each timestep move the velocity in x File "<__array_function__ internals>", line 200, in append File "C:\Users\hecto\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\function_base.py", line 5499, in append return concatenate((arr, values), axis=axis) File "<__array_function__ internals>", line 200, in concatenateValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)
This is weird because what I'm trying to append is a value, not an array. It makes sense that it has dimension 0, but why would it need to have a dimension to be appended to the end of the array? This makes me think that the "axis=0" is the wrong way to go, but I couldn't tell you why either.