[英]Python odeint function doesn't seem to work
I want to study a charged particle's motion while travelling through a magnetic field by modelling it with Python. 我想用Python对带电粒子在磁场中传播时的运动进行研究。 I tried to use odeint function from scipy.integrate and it doesn't seem to work as I expected.
我尝试使用scipy.integrate的odeint函数,但它似乎没有按我预期的那样工作。 Here is what I expected given the initial condition:
考虑到初始条件,这是我期望的:
But here is what I got with my simulation: 但是这是我通过模拟得到的结果:
The problem comes from my implementation of the odeint function. 问题来自我对odeint函数的实现。 Any help is apreciated.
任何帮助都非常感谢。
Here is my code: 这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from mpl_toolkits.mplot3d import Axes3D
def vect_prod(u, v):
return np.array([u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]])
def dy(y, t):
x1, Vx, y1, Vy, z1, Vz = y
F = q * (E + vect_prod(np.array([Vx, Vy, Vz]), B))
dy = [Vx, Vx - F[0] / m, Vy, Vy - F[1] / m, Vz, Vz - F[2] / m]
return dy
E = np.array([0, 0, 0])
B = np.array([0, 0, 1])
q = 1
m = 1
a = 0.04
cond = [0, 1, 0, 1, 0, 1]
t = np.arange(0, 100, 0.1)
sol = odeint(dy, cond, t)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.plot(sol[:, 0], sol[:, 2], sol[:, 4])
plt.show()
Any help will be appreciated! 任何帮助将不胜感激!
I think the mass is too large: 我认为质量太大:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from mpl_toolkits.mplot3d import Axes3D
def vect_prod(u, v):
return np.array([u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]])
def dy(y, t):
x1, Vx, y1, Vy, z1, Vz = y
F = q * (E + vect_prod(np.array([Vx, Vy, Vz]), B))
dy = [Vx, Vx - F[0] / m, Vy, Vy - F[1] / m, Vz, Vz - F[2] / m]
return dy
E = np.array([0, 0, 0])
B = np.array([0, 0, 1])
q = 1
m = 0.001
a = 0.04
cond = [0, 1, 0, 1, 0, 1]
t = np.arange(0, 0.05, 0.0001)
sol = odeint(dy, cond, t)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.plot(sol[:, 0], sol[:, 2], sol[:, 4])
plt.show()
the output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.