Mercurial > octave
diff scripts/ode/ode23s.m @ 31263:449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
* scripts/ode/ode23.m: Remove disabling of Refine option with struct output.
Modify solution struct to output two sets of solution variables: output_t,
output_x and ode_t and ode_x, and transpose struct output variables for
improved Matlab compatibility. Update BISTs and perform minor code formatting.
* scripts/ode/ode23s.m: Make same changes as ode23.m.
* scripts/ode/ode45.m: Make same changes as ode23.m. Remove comment indicating
that Refine is not implemented.
* scripts/ode/private/integrate_adaptive.m: Update internal handling of
variables t and x, separating them into ode_t & ode_x for internal integration
and output_t & output_x for function output or calls to OutputFcn. Replace
prior attempt at Refine option with new implementation. Specify time output or
Refine != 0 are both interpolated from internal variables (ode_t, ode_x) for
output of non-struct variables and/or for use with OutputFcn. Improve event
handling when multiple Events (including at least one terminal Event) are
detected in a single simulation step so that all Events up to and including the
first terminal one are reported, and final data point is set to that of
terminal Event. Send multiple data points in a single call to OutputFcn if they
are all interpolated from a single integration step. Remove warning for
termination when term signal is received from Events or OutputFcn. Return both
internal variables (ode_t, ode_x) and interpolated variables (output_t,
output_x) to allow calling function to correctly return either struct or
separate variables.
* scripts/ode/private/ode_event_handler.m: Sort multiple Events in ascending
time order when they are encountered in one integration step. Remove any events
after the time of a terminal Event.
* scripts/ode/odeset.m: Update docstring to remove indication that Refine is
not implemented
* scripts/ode/odeplot.m: Update docstring to indicate that input t can be a
scalar or vector. Add file test.
* etc/NEWS.8.md: Add descriptions of changes under General improvements and
Matlab compatibility.
author | Ken Marek <marek_ka@mercer.edu> |
---|---|
date | Wed, 05 Oct 2022 16:53:01 -0400 |
parents | a40c0b7aa376 |
children | c332a2f2959f |
line wrap: on
line diff
--- a/scripts/ode/ode23s.m Wed Oct 05 12:03:52 2022 -0700 +++ b/scripts/ode/ode23s.m Wed Oct 05 16:53:01 2022 -0400 @@ -205,12 +205,7 @@ ## Starting the initialization of the core solver ode23s - if (nargout == 1) - ## Single output requires auto-selected intermediate times, - ## which is obtained by NOT specifying specific solution times. - trange = [trange(1); trange(end)]; - odeopts.Refine = []; # disable Refine when single output requested - elseif (numel (trange) > 2) + if (numel (trange) > 2) odeopts.Refine = []; # disable Refine when specific times requested endif @@ -222,8 +217,9 @@ feval (odeopts.OutputFcn, [], [], "done", odeopts.funarguments{:}); endif if (! isempty (odeopts.Events)) # Cleanup event function handling - ode_event_handler (odeopts.Events, solution.t(end), ... - solution.x(end,:).', "done", odeopts.funarguments{:}); + ode_event_handler (odeopts.Events, solution.ode_t(end), ... + solution.ode_x(end,:).', "done", ... + odeopts.funarguments{:}); endif ## Print additional information if option Stats is set @@ -241,16 +237,16 @@ endif if (nargout == 2) - varargout{1} = solution.t; # Time stamps are first output argument - varargout{2} = solution.x; # Results are second output argument + varargout{1} = solution.output_t; # Time stamps are first output argument + varargout{2} = solution.output_x; # Results are second output argument elseif (nargout == 1) - varargout{1}.x = solution.t.'; # Time stamps saved in field x (row vector) - varargout{1}.y = solution.x.'; # Results are saved in field y (row vector) + varargout{1}.x = solution.ode_t.'; #Time stamps saved in field x (row vect.) + varargout{1}.y = solution.ode_x.'; #Results are saved in field y (row vect.) varargout{1}.solver = solver; # Solver name is saved in field solver if (! isempty (odeopts.Events)) - varargout{1}.xe = solution.event{3}; # Time info when an event occurred - varargout{1}.ye = solution.event{4}; # Results when an event occurred - varargout{1}.ie = solution.event{2}; # Index info which event occurred + varargout{1}.xe = solution.event{3}.'; # Time info when an event occurred + varargout{1}.ye = solution.event{4}.'; # Results when an event occurred + varargout{1}.ie = solution.event{2}.'; # Index info which event occurred endif if (strcmpi (odeopts.Stats, "on")) varargout{1}.stats = struct (); @@ -263,8 +259,8 @@ endif elseif (nargout == 5) varargout = cell (1,5); - varargout{1} = solution.t; - varargout{2} = solution.x; + varargout{1} = solution.output_t; + varargout{2} = solution.output_x; if (! isempty (odeopts.Events)) varargout{3} = solution.event{3}; # Time info when an event occurred varargout{4} = solution.event{4}; # Results when an event occurred @@ -389,7 +385,8 @@ %! error ('fout: step "init"'); %! endif %! elseif (isempty (flag)) -%! if (! isequal (size (t), [1, 1])) +%! # Multiple steps can be sent in one function call +%! if (! isequal ( size (t), size (y))) %! error ('fout: step "calc"'); %! endif %! elseif (strcmp (flag, "done")) @@ -404,6 +401,14 @@ %!test # two output arguments %! [t, y] = ode23s (@fpol, [0 2], [2 0]); %! assert ([t(end), y(end,:)], [2, fref], 1e-3); +%!test # correct number of steps with Refine +%! [t1, y1] = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 1)); +%! [t2, y2] = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 4)); +%! [t3, y3] = ode23s (@fpol, [0 2], [2 0]); #default Refine=1 +%! s = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 4)); +%! assert (length (t1) == length (t3)); +%! assert (length (t2) == 4*length (t1) - 3); +%! assert (length (s.x) == length (t1)); %!test # anonymous function instead of real function %! fvdp = @(t,y) [y(2); 10 * (1 - y(1)^2) * y(2) - y(1)]; %! [t, y] = ode23s (fvdp, [0 2], [2 0]); @@ -435,8 +440,8 @@ %! opt = odeset ("RelTol", 1e-8, "NormControl", "on"); %! sol = ode23s (@fpol, [0 2], [2 0], opt); %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-4); -%!test # Details of OutputSel and Refine can't be tested -%! opt = odeset ("OutputFcn", @fout, "OutputSel", 1, "Refine", 5); +%!test # Details of OutputSel can't be tested +%! opt = odeset ("OutputFcn", @fout, "OutputSel", 1); %! sol = ode23s (@fpol, [0 2], [2 0], opt); %!test # Stats must add further elements in sol %! opt = odeset ("Stats", "on"); @@ -499,6 +504,12 @@ %! [x, y] = ode23s (@(x,y) 1, [0 1], 1i); %! assert (imag (y), ones (size (y))); +## FIXME: convert to demo or a visible=off test with failable assert/error +## statemments +##%!test # Make sure odeplot works (default OutputFcn when no return value) +##%! ode23s (@fpol, [0 2], [2 0]); +##%! close all + ## Test input validation %!error <Invalid call> ode23s () %!error <Invalid call> ode23s (1)