matlab/octave随机事件ode45 [英] matlab/octave random event ode45

查看:328
本文介绍了matlab/octave随机事件ode45的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在了解如何在八度/matlab中实现事件以及微分方程的解析方面遇到一些麻烦.

I have some troubles in understanding how to implement events in octave/matlab, in the resolution of differential equations.

例如,考虑以下简单代码来解决微分方程y'= -y:

Consider for example this simple code to solve the differential equation y' = -y:

function dy = odefun(t,y)
    dy = -y;
endfunction

options = odeset('RelTol',1e-4,'AbsTol',[1e-4]);
[T,Y] = ode45(@odefun,[0 12],[1],options);

现在,我想介绍一个随机事件.例如,在某个固定的时间步,我想随机更改y的值,然后根据微分方程继续进行演化.我该怎么办?

Now I would like to introduce a random event. For example at some fixed time step I would like to randomly change the value of y and then continue the evolution according to the differential equation. How can I do this?

推荐答案

做到这一点的方法是分段集成系统,并将每次运行的结果输出附加在一起:

The way to do this is to integrate the system piecewise and append the resultant outputs from each run together:

% t = 0 to t = 6
tspan = [0 6];
y0 = 1;
options = odeset('RelTol',1e-4,'AbsTol',1e-4);
[T,Y] = ode45(@odefun,tspan,y0,options);

% t = 6 to t = 12
tspan = [6 12];
y0 = Y(end,:)+randn; % Pertubation
[t,y] = ode45(@odefun,tspan,y0,options);
T = [T;t(2:end)];  % Remove first value as it will be same as last of previous run
Y = [Y;y(2:end,:)];

Matlab编辑器可能会抱怨数组TY没有预先分配和/或增长,但是在这种情况下还可以,因为它们只大块增长几次.

The Matlab editor may complain about the array T and Y not being preallocated and/or growing, but it's fine in this case as they're growing in large chunks only a few times.

您不想尝试(但尝试多次)是在集成函数内部添加扰动.首先,如果您希望它恰好在特定时间或满足特定条件时发生,则不能从ODE函数中完成此操作.其次,在ODE中插入较大的不连续点可能会降低精度并延长计算时间(尤其是对于ode45- ode15s 可能是一个更好的选择,或者至少要确保您的绝对公差和相对公差合适.您本可以有效地产生一个非常僵硬系统.

What you don't want to try to do (but many attempt) is add your perturbation inside the integration function. First, if you want it to occur exactly at a particular time or when particular conditions are met, this can't be done from within the ODE function. Second, inserting large discontinuities in your ODE can lead to less precision and longer computation times (especially with ode45 - ode15s might be a better option or at least make sure that your absolute and relative tolerances are suitable). You would have effectively produced a very stiff system.

如果您确实希望在满足特定条件时(而不是在特定时间)发生干扰,那么您将需要使用事件函数(请参见

If you do want your perturbation occur when particular conditions are met (rather than at a particular time) then you'll need to use an event function (see this question, for example). You'll need to use the events function to terminate integration when the conditions are met and then apply your perturbation as I've shown above.

这篇关于matlab/octave随机事件ode45的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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