Scipy ode根据解的大小积分到t的未知极限 [英] Scipy ode integrate to unknown limit of t, based on the size of solution
问题描述
我正在模拟在电磁场中移动的带电粒子,并且正在使用scipy ode.显然,此处的代码已简化,但仅作为示例.我的问题是我想在限制r而不是t之后结束积分.因此,将dx/dt积分到norm(x)> r.
I am modeling charged particles moving through an electromagnetic field and am using scipy ode. The code here is simplified, obviously, but works as an example. The problem I have is that I want to end the integration after a limit on r, not on t. So, integrate dx/dt up to the point where norm(x) > r.
但是,我不想仅更改函数以在r上积分,因为位置是t的函数.我可以对不相关的变量或某些东西进行定积分吗?
I don't want to just change the function to integrate over r, however, because the position is a function of t. Can I do a definite integral over an unrelated variable or something?
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def RHS(state, t, Efield, q, mp):
ds0 = state[3]
ds1 = state[4]
ds2 = state[5]
ds3 = q/mp * Efield*state[0]
ds4 = q/mp * Efield*state[1]
ds5 = q/mp * Efield*state[2]
r = np.linalg.norm((state[0], state[1], state[2]))
# if r > 30000 then do stop integration.....?
# return the two state derivatives
return [ds0, ds1, ds2, ds3, ds4, ds5]
ts = np.arange(0.0, 10.0, 0.1)
state0 = [1.0, 2.0, 3.0, 0.0, 0.0, 0.0]
Efield=1.0
q=1.0
mp=1.0
stateFinal = odeint(RHS, state0, ts, args=(Efield, q, mp))
print(np.linalg.norm(stateFinal[-1,0:2]))
推荐答案
您可以使用请注意,对于ode
,必须将RHS的签名更改为def RHS(t, state, Efield, q, mp):
,与odeint
不同,自变量是最先出现的.
Note that the signature of RHS will have to be changed to def RHS(t, state, Efield, q, mp):
for ode
, the independent variable comes first, unlike in odeint
.
输出是以自变量dt
的增量计算的解,直到循环结束为止(因为达到t_max
或积分器失败,或者遇到了break
的条件) ).
The output is the solution computed in the increments of dt
of independent variable, up to the time when the loop ends (either because t_max
is reached, or integrator fails, or the condition for break
was encountered).
这篇关于Scipy ode根据解的大小积分到t的未知极限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!