为什么SciPy的ODE解算器不能产生正确的解决方案?

2024-10-03 11:13:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用scipy的ode解算器来计算电子在B和E场中的轨迹(但目前所有组件的E场均为零)。我首先对常数和字段使用了非常简单的数字,即q=m=Bz=Ez=vy=1,得到了正确的解。然而,当我试图为q和m输入更现实的值时,它给我的解决方案是完全错误的。我不认为这是ode解算器的错,但我自己的错在这里,所以我想问一下,是否有人能看到我的初始条件和时间步长可能有什么问题,看看为什么会发生这种情况。代码:

import numpy as np
import pylab
from scipy.integrate import odeint
import matplotlib.pyplot as plt
#import random
import mpl_toolkits.mplot3d.axes3d as p3

P0 = [0,0,0]
V0 = [1.759E+11,0,0]
t = np.linspace(0,9E-8,num=10E-8)

#Physical/Natural Constants
q_e = -1.602E-19
m_e = 9.11E-31

#Math
ICs = np.concatenate((P0,V0),axis=0)

def BField(x,y,z):

    Bx = 0
    By = 0
    Bz = 1

    BVec = np.array([Bx,By,Bz])

    return BVec

Bout = BField(0,0,0.02)


def EField(x,y,z):

    Ex = 0
    Ey = 0
    Ez = 0
    EVec = np.array([Ex,Ey,Ez])

    return EVec


def LorentzForce(PosVel,t,Constants):

    x,y,z,vx,vy,vz = PosVel
    Ex,Ey,Ez,Bx,By,Bz,q_e,m_e = Constants

    EFInput = np.array([Ex,Ey,Ez])
    BFInput = np.array([Bx,By,Bz])
    VelInput = np.array([vx,vy,vz])

    Accel = (q_e/m_e) * (EFInput + np.cross(VelInput, BFInput))   

    LFEqs = np.concatenate((VelInput, Accel), axis = 0)


    return LFEqs


Ex,Ey,Ez = EField(P0[0],P0[1],P0[2])
Bx,By,Bz = BField(P0[0],P0[1],P0[2])
#Ex = Ey = Ez = 0
AllConstantInputs = [Ex,Ey,Ez,Bx,By,Bz,q_e,m_e]
ParticleTrajectory = odeint(LorentzForce, ICs, t, args=(AllConstantInputs,))

print(ParticleTrajectory)


fig = pylab.figure()
particleplot = p3.Axes3D(fig)
plt.plot(ParticleTrajectory[:, 0],ParticleTrajectory[:, 1],ParticleTrajectory[:, 2],'b')
plt.legend(loc='best')
plt.grid()
plt.show()

Tags: importbyasnppltarraybzex