猿问

SciPy ODE 求解器忽略函数

我正在尝试让 Scipy 的 ODE 求解器求解洛伦兹力微分方程。它不能用 B 场分量正确求解方程,因为无论我做多大的 E 场,它都会完全忽略它(这也是我知道它忽略它的原因)。为什么是这样?我也已经尝试过修改 E-field 功能上的标志。


代码:


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



#Mirroring Angle to Recreate

ThetaMirror = 40

TStep = 1

TFinal = 10

P0 = [0.,0.02,0.]

V0 = [1,0,0]

t = np.linspace(0,TFinal,num=(TFinal/TStep))


#Physical/Natural Constants

q_e = -1

m_e = 1

QeMe = q_e/m_e

u0 = 1


#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



def EField(x,y,z):


    Ex = 0

    Ey = 0

    Ez = 2.8E8*z**4

    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,QeMe = Constants


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

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

    VelInput = np.array([vx,vy,vz])


    Accel = QeMe * (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,QeMe]

ParticleTrajectory = odeint(LorentzForce, ICs, t, args=(AllConstantInputs,))


print(ParticleTrajectory)

print(Bz)


fig = plt.figure()

particleplot = fig.add_subplot(111,projection='3d')

particleplot.plot(ParticleTrajectory[:, 0],ParticleTrajectory[:, 1],ParticleTrajectory[:, 2],'b')

particleplot.set_xlabel('x axis')

particleplot.set_ylabel('y axis')

particleplot.set_zlabel('z axis')

particleplot.legend(loc='best')

particleplot.grid()

plt.show()


墨色风雨
浏览 67回答 1
1回答

HUH函数

你定义P0[2] = 0.. 电场函数计算Ez = 2.8E8*z**4。因此,python 没有忽略 E-field 函数,您自己将其 z 分量置零:z = P0[2] = 0.
随时随地看视频慕课网APP

相关分类

Python
我要回答