如何在 Python 中使用数组,类似于在 matlab 中使用数组

问题描述 投票:0回答:1

我目前正在尝试通过使用它来重新创建我在本科时所做的一些旧的 matlab 项目来提高我的 python 技能。我曾经使用 matlab 来模拟质子吸引电子,使用多维数组来存储每个时间步的位置和速度。现在我尝试在 Python 中做同样的事情,但是数组的工作方式与它们在 matlab 中的使用方式非常不同。我遇到了很多错误,并在解决问题的过程中遇到了很多问题,但我认为我一直在以“matlab 方式”思考数组,并且关于如何在 python 中实现这一点的一些观点将不胜感激。

#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 np
import matplotlib.pyplot as plt

re = 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 electron
k = 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 proton
dt = 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 angle

for 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 y
    
plt.scatter(re[0][:], re[1][:], s=2, c='b') #Plot electron's trajectory
plt.scatter(rp[0],rp[1], s=3, c='r') #Show proton's position
plt.show() #Show

基本上我想做的是在数组中每个“向量”的末尾添加系统的下一个“状态”(一个用于位置,一个用于速度,都包含 x 和 y 的分量),最后绘制每个时间状态以查看整个轨迹。然而,这会返回以下错误:

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 concatenate
numpy.AxisError: axis 1 is out of bounds for array of dimension 1
PS C:\Users\hecto\OneDrive\Documentos\ITESM\8vo semestre\Repaso Python> 

不能追加到数组中的特定“向量”吗?或者我错过了什么。任何指点帮助,提前感谢!

当我在没有“axis=1”的情况下尝试相同的代码时,我返回了有关数组尺寸的错误。这可能是因为没有它, np.append 会使向量变平。当我尝试“axis=0”时,错误变为:

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 concatenate
ValueError: 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)

这很奇怪,因为我试图附加的是一个值,而不是一个数组。它的维度为 0 是有道理的,但为什么需要将一个维度附加到数组末尾呢?这让我认为“axis=0”是错误的方法,但我也无法告诉你为什么。

python arrays numpy matlab
1个回答
0
投票

当有内置类可以为您完成所有工作时,尝试在 Python 中编写自定义数组类型没有什么意义。有关详细信息和示例代码,请参阅[数据结构]{https://docs.python.org/3.11/tutorial/datastructs.html)。您只需选择符合您要求的列表类型即可。

© www.soinside.com 2019 - 2024. All rights reserved.