英文:
Solving ODEs with parameters from an array
问题
I've already solved these 2 differential equations with constant coefficients for ne
, Zeff
, and Epar
using odeint
but now I want to solve them passing values at each time step for the coefficients from an array. How can this be done?
def f(Q, t):
# constants
ne = 5E19
ln = 17
permit = 8.8542E-12
m_e = 9.1094E-31
c = 2.9979E8
e = 1.6E-19
B0 = 6
R0 = 0.935
Zeff = 3
Epar = 0.2
Er = (e**3*ne*ln)/(4*np.pi*permit**2*m_e*c**2)
D = Epar/Er
alpha = 1+Zeff
Fgy = (2*permit*B0**2)/(3*ne*m_e*ln)
Fgc = Fgy*((m_e*c)/(e*B0*R0))**2
nur = ne*e**4*ln/(4*np.pi*permit**2*m_e**2*c**3)
# assign each ODE to a vector element
qp, q = Q
gamma = (1+q**2)**(1/2)
qpp2 = q**2-qp**2
# define each ODE
dqpdt = (D-gamma*(gamma+alpha)*qp/(q**3)-(Fgc+Fgy*qpp2/q**4)*gamma**4*(((gamma**2)-1)**(1/2)/gamma)**3*qp/q)*nur
dqdt = (D*qp/q-gamma**2/q**2-(Fgc+Fgy*qpp2/q**4)*gamma**4*(((gamma**2)-1)**(1/2)/gamma)**3)*nur
return [dqpdt, dqdt]
I've tried using args
and odeint
but with the simulation time equal to the time step but unsuccessfully.
英文:
I've already solved these 2 differential equations with constant coefficients for ne
, Zeff
and Epar
using odeint
but now I want to solve them passing values at each time step for the coefficients from an array. How can this be done?
def f(Q, t):
# constants
ne=5E19
ln=17
permit=8.8542E-12
m_e=9.1094E-31
c=2.9979E8
e=1.6E-19
B0=6
R0=0.935
Zeff=3
Epar=0.2
Er=(e**3*ne*ln)/(4*np.pi*permit**2*m_e*c**2)
D = Epar/Er
alpha = 1+Zeff
Fgy = (2*permit*B0**2)/(3*ne*m_e*ln)
Fgc = Fgy*((m_e*c)/(e*B0*R0))**2
nur=ne*e**4*ln/(4*np.pi*permit**2*m_e**2*c**3)
# assign each ODE to a vector element
qp, q = Q
gamma=(1+q**2)**(1/2)
qpp2=q**2-qp**2
# define each ODE
dqpdt = (D-gamma*(gamma+alpha)*qp/(q**3)-(Fgc+Fgy*qpp2/q**4)*gamma**4*(((gamma**2)-1)**(1/2)/gamma)**3*qp/q)*nur
dqdt = (D*qp/q-gamma**2/q**2-(Fgc+Fgy*qpp2/q**4)*gamma**4*(((gamma**2)-1)**(1/2)/gamma)**3)*nur
return [dqpdt,dqdt]
I've tried using args
and odeint
but with the simulation time equal to the time step but unsuccesfully.
答案1
得分: 1
无法完成,因为您无法控制积分中使用的时间步长。您只能控制输出的时间步长,那里的值是从内部积分步长进行插值的(odeint
在概念上是反向进行的,它试图插值下一个输出值,如果不可能,则将内部步长提前,直到可能为止。这样可以在调用底层Fortran例程时获得一个输出值,具有可变数量,可能为零,的内部步长)。
因此,如果您想要与时间有关的参数,您需要将它们作为时间相关的函数传递,以便积分器可以在积分区间的任何时间找到参数。如果参数以函数表的形式给出,您必须提供时间连续函数的最佳猜测(值连续是可选的,零保持或阶跃函数也是可能的),这个过程被称为插值。NumPy 和 SciPy 提供了多种不同插值方法的过程。
英文:
It can't be done because you have no control over the time steps used in the integration. You only control the time steps for the output, the values there get interpolated from the internal integration steps (odeint
does this "conceptually" backwards, it tries to interpolate the next output value, and if that is not possible advances the internal step until it is possible. This gives one output value per call of the underlying Fortran routine, with a variable number, possibly zero, of internal steps).
Thus if you want time-dependent parameters, you need to pass them as time-dependent functions, so that the integrator can find the parameters for any time in the integration interval. If the parameters are given as a function table, you have to provide the best guess for a time-continuous function (value-continuous is optional, zero-hold or step functions are possible), this process is called interpolation. Numpy and scipy provide several procedures for different interpolation methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论