Is it possible to use an anonymous function as an event function when solving ODE in Matlab

Is it possible to use an anonymous function as an event function in Matlab. What I want to do is basically

opt = odeset('Events', @(t,y) (deal(y(end)-t^2,1,0)));
[T,Y] = ode45(@odefun,[tstart tend],y0,opt);

However, this returns an error, complaining that the amount of output should match exactly. Is there any other way to force an anonymous function to return multiple arguments?

+5
source share
3 answers

Also (it's very late in the game to add to this, but it disappoints me). Here is a solution with nested functions:

function [ dealfunchandle ] = dealwithit( arrayfunc )
% Takes as input an event function of (t,z) that returns a 3 array (in same order as event syntax).

    function [a, b, c] = dealfunc(t,z)
       output = arrayfunc(t,z);
       a = output(:,1);
       b = output(:,2);
       c = output(:,3);
    end

dealfunchandle = @dealfunc;
end

(, ). :

arrayfunc = @(t,y) [y(1), 0, 1];
events = dealwithit(arrayfunc);

opts = odeset('Events', events);

ode45 .

0

, . , . , , , "" .

, - , . :

evnt_fun = @(t, f) subsref ({' ', 1, 0}, struct ('type', '{}', 'subs', {{':'}}));

. R2011 MATLAB.

+2

, . Matlab - .

Instead, you can define a thin shell around dealand pass your shell as a handle:

function [a b c] = wrapper(t,y)
    [a b c] = deal('some stop condition', 1, 0);
end

opt = odeset('Events', @wrapper);

[T, Y] = ode45(@odefun, [tstart tend], y0, opt);
+1
source

All Articles