Scipy odeint - 每个时间步长 tx 中不同的参数值 [英] Scipy odeint - different values of arguments in each timestep tx

查看:64
本文介绍了Scipy odeint - 每个时间步长 tx 中不同的参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SciPy 模拟一些动态模型.

I am trying to simulate some dynamic models using SciPy.

我有模型定义:

def model(y, t, control_signal):
    dy/dt = some_function_of_time_and_y
    return dy

我定义了要模拟模型的时间戳列表:t_list=np.linspace(0, 5, 100).我想使用为每个时间戳定义的 control_signal 值来模拟模型.我试图通过使用:

I defined list of timestamps at which I want to simulate model: t_list=np.linspace(0, 5, 100). I would like to simulate model using control_signal values defined for each timestamp. I tried to achieve so by using:

controls = [list_of_values]
scipy.integrate.odeint(model, 0, t_list, args=(controls))

但是我得到func(5)返回的数组的大小与y0(1)的大小不匹配.好像我的 controls 被解释为模型的状态,而不是每个时间戳中的输入.我如何将 controls 作为每个时间戳的值传递?

But I get The size of the array returned by func (5) does not match the size of y0 (1). Seems like my controls is interpreted as states of model, not inputs in each timestamp. How I can pass controls as values for each timestamp?

谢谢!

推荐答案

scipy.interpolate.interp1d 中可以定义插值模式为zero-hold"或0阶样条,即分段常数,带有 `kind="zero".使用它来定义控件的时间依赖性.

In scipy.interpolate.interp1d you can define the interpolation mode as "zero-hold" or order 0 spline, that is, piecewise constant, with `kind="zero". Use that to define the time dependency of your control.

contfunc = interp1d(t_list,control, kind="zero");

def model(y, t, control_signal):
    u = contfunc(t);
    dydt = some_function_of_time_and_y_and_u
    return dydt

尺寸错误可能是另一个问题.要调试它,请使用调试器(如果可用)或为输入和输出的大小/形状添加打印语句.状态和导数应该是相同大小的平面数组.

The dimension error might be a different issue. To debug that, either use a debugger if available or add print statements for the size/shape of inputs and outputs. State and derivative should be flat arrays of the same size.

不要忘记将最大时间步长设置为小于控件的步长,这应该无关紧要,但可能会导致奇怪的错误.

Do not forget to set the maximal time step to smaller than the step size of the control, it should not matter but could be a source for strange errors.

这篇关于Scipy odeint - 每个时间步长 tx 中不同的参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆