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

查看:362
本文介绍了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之后,我们也会继续模拟所有N个系统.但是,这很简单,输出数组是矩形而不是参差不齐.

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天全站免登陆