如何在ode求解器中的每个时间步使用一个矩阵值 [英] How to use one value of a matrix for each time step inside ode solver

查看:162
本文介绍了如何在ode求解器中的每个时间步使用一个矩阵值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度为N的向量x,我想使用其值来求解微分方程:dy/dt = x - 4*y.对于每一步,我都希望ode求解器函数使用向量的一个值,然后将矩阵的下一个值用于下一步.

I have a vector x of length N and I want to use its values for solving the differential equation: dy/dt = x - 4*y. For each step I want the ode solver function to use one value of the vector and then use the next value of the matrix for the next step.

我尝试通过将向量声明为全局变量并在ode求解器中像这样使用它来实现此目的:

I tried doing so by declaring the vector a global variable and use it like this inside the ode solver:

global x
tspan = 0:0.01:10;
[t,filter_coef] = ode45(@ode_filter,tspan,0);

以及这样的求解函数:

function dx = ode_filter(t,fil)

    global x 
    dx = x - 4*fil(1);

end

产生以下错误

ODE_FILTER returns a vector of length 1002, but the length of initial conditions vector is 1. The vector returned by
ODE_FILTER and the initial conditions vector must have the same number of elements.

推荐答案

您的方法可能基于对显式Euler方法的理解.如果是这种情况,则最好实现(指数)显式Euler方法.

You might be basing your approach on your understanding of the explicit Euler method. If that is the case you would be best served by implementing the (exponential) explicit Euler method.

求解器ode45,与所有其他Matlab ODE求解器(无特定选项?)一样,步长可变,可以自动适应问题和当前状态.此外,在每个步骤中,都会对传递的右侧ODE函数进行多次评估.此外,步长调节器取决于右侧函数的高阶平滑度,不平滑的轨迹会导致步长急剧减小,并可能从同一当前状态多次重启,从而进一步与您的假设相矛盾.

The solver ode45, as all of the other matlab ODE solvers (without specific options?) has variable step size which is automatically adapted to the problem and the current state. Additionally, during each step the passed right side ODE function is evaluated multiple times. Further, the step size regulator depends on the smoothness to a high order of the right side function, non-smooth loci lead to a drastic reduction in step size and possibly multiple restarts from the same current state, further contradicting your assumptions.

因此,即使您成功实现了构想,也实际上在函数中添加了随机噪声,由于违反了基本假设,因此使求解器无用.即使产生了结果,它也几乎与您想要实现的目标无关.

Thus, even if you succeed in implementing your idea, you are adding essentially random noise to your function, rendering the solver useless as basic assumptions are violated. Even if a result is produced, it will have almost nothing to do with what you wanted to achieve.

实现类似您想要产生的结果的最快方法是确定与x值关联的时间,并使用一些插值函数(零阶保持或线性插值)来获取变量的正确值时代.

The fastest way to achieve something like what you want to produce is to identify the times at to which the x values are associated and use some interpolation function, zero order hold or linear interpolation, to get the correct value at variable times.

使用解决方案扩展来使用平滑的右侧,为每个段[ tx(k), tx(k+1) ]更改常数x(k).定义具有参数的函数,避免global变量的麻烦

Use solution extension to solve every segment using a smooth right side, changing the constant x(k) for each segment [ tx(k), tx(k+1) ]. Define the function to have a parameter, avoiding the hassle of global variables

function dy = ode_filter(t,y,x)
    dy = x - 4*y;
end

然后调用积分器以初始化第一个段,然后使用其常量和段末尾初始化所有其余段.

then call the integrator for the first segment to initialize and then for all the remaining segments using their constant and the segment end.

sol = ode45(@(t,y)ode_filter(t,y,x(1)), [ tx(1) tx(2) ], y0)
for k in 2:N
    sol = odextend(sol,@(t,y)ode_filter(t,y,x(k)),tx(k+1));
end

(始终考虑使用选项机制来设置特定问题的累积错误容忍度.)

(Always think about using the options mechanism to set costumized, problem specific error tolerances.)

这篇关于如何在ode求解器中的每个时间步使用一个矩阵值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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