MATLAB:在使用 ode45 时是否可以有两个事件值? [英] MATLAB: Is it possible to have two event values whilst using ode45?

查看:35
本文介绍了MATLAB:在使用 ode45 时是否可以有两个事件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对我的 ode45 运动方程计算有两个限制:位置和时间.我已经让时间事件起作用了,但我不确定是否以及如何添加另一个事件来限制位置.我还有许多不同的粒子在一个 ODE 方程中耦合在一起,并且需要它们在到达屋顶"时单独停止,因为它们都以不同的速度行进......我能否通过事件实现这一目标?我有一个关于如何做到这一点的想法,但它非常复杂,而且可能会很慢......

I want two limitations to my ode45 calculation of a movement equation: position and time. I have already got the time event to work but I am not sure if and how I can add another event for limiting the position. I also have many different particles coupled together in one ODE equation and need them to stop individually once they reach a 'roof' as they all travel at different speeds... would I be able to achieve this through events? I have an idea on how I would do this but its very complicated and would probably be very slow...

推荐答案

我不确定您是否可以完全按照自己的意愿行事,但是可以用事件来做很多事情.首先,这听起来像是对第一次通过时间(又名第一次)的某种数值计算击球时间).如果这些粒子"是随机的,停止并不要使用 ode45 而是一种适合 SDE 的方法.

I'm not sure if you can do exactly what you want, but it is possible to do quite a lot with events. First, this sounds like some sort of numerical calculation of first passage time (a.k.a first hitting time). If these "particles" are stochastic, stop and don't use ode45 but rather a a method appropriate for SDEs.

据我所知,您可以拥有的事件函数的数量没有限制 - 或者更确切地说是事件函数的维度(类似于您的 ODE 函数的维度) - 它们的数量与 ODE 的数量无关你有的方程.事件函数接收当前时间和当前状态向量.您可以使用其中的任何或所有元素来创建每个事件.您是正确的,更多的事件函数和更复杂的事件会减慢集成速度.性能还取决于检测事件的频率.如果您的每个粒子都到达了您所说的屋顶",并且只触发了一个事件,那还不错.

As far as I know there is no limit on how many event functions you can have - or rather the dimension of the event function (similar to the dimension of your ODE function) - and their number is not tied to how many ODE equations you have. The events function receives both the current time and the current state vector. You can any or all of the elements of those to create each event. You are correct that more events functions and more complex events will slow down integration. The performance also depends on how often events are detected. If each of your particles reaches the "roof," as you call it, and triggers just a single event then that won't be too bad.

在实现方面,这里是一个基于 Matlab 的 ballode 示例的简单示例,该示例仅模拟垂直方向的 N 个弹道粒子.有 N 个非终止事件来捕捉每个粒子通过 y = 0 时的时间和速度.添加一个额外的终止事件来检查是否所有粒子都通过了 y = 0(如果我们知道这是哪个粒子)将是,就像我们在这里所做的那样,我们可以使该事件成为终止事件).

In terms of implementation, here a simple example based on Matlab's ballode example, that simulates N ballistic particles in the vertical alone. There are N non-terminating events to catch the time and speed of each particle as it passes through y = 0. One additional terminating event is added to check if all of the particles have passed through y = 0 (if we knew which particle this would be, as we do here, we could just make that event a terminating one).

function eventsdemo

% Initial conditions for n balls
n = 10;
y0(2*n,1) = 0;
y0(n+1:end) = linspace(20,40,n);

% Specify events function
options = odeset('Events',@(t,y)efun(t,y,n));

% Integrate
[t,y,te,ye,ie] = ode45(@(t,y)f(t,y,n),[0 10],y0,options);

figure;
plot(t,y(:,1:n),'b',te(1:n),ye(sub2ind(size(ye),ie(1:n),(1:n).')),'r.');

function dydt = f(t,y,n)
% Differential equations for ballistic motion
dydt = [y(n+1:end);zeros(n,1)-9.8];

function [value,isterminal,direction] = efun(t,y,n)
% Last event checks that all balls have hit ground and terminates integration
yn = y(1:n);
value = [yn;all(yn < 0)];
zn = zeros(n,1);
isterminal = [zn;1];
direction = [zn-1;1];

在某些方面,这有点低效,因为我们继续模拟所有 N 个系统,即使其中一些系统已经通过 y = 0.但是,它很简单,输出数组是矩形而不是参差不齐的.

In some ways this is slightly inefficient because we keep simulating all N systems even after some of them have passed through y = 0. However, it is simple and the output arrays are rectangular rather than ragged.

我不清楚您所说的耦合在一起"和需要粒子停止"的确切含义.如果您需要做的不仅仅是记录事件数据,例如更改系统参数或以其他方式更改微分方程,则您需要在每个事件后终止并重新开始积分.查看 ballode 示例(在 Matlab 命令窗口中键入 edit ballode),了解一些有关如何提高效率的建议.

I'm not clear what you mean precisely by "coupled together" and needing the particles to "stop." If you need to do more than just record the the event data, such as change system parameters or change the the differential equation(s) in some other way, then you'll need to terminate after each event and restart the integration. Look at the ballode example (type edit ballode in the Matlab command window) to see some suggestions on to make this a bit more efficient.

这篇关于MATLAB:在使用 ode45 时是否可以有两个事件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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