Mercurial > octave
annotate 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 |
rev | line source |
---|---|
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
1 ######################################################################## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
2 ## |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
29359
diff
changeset
|
3 ## Copyright (C) 2006-2022 The Octave Project Developers |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
4 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
7 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
8 ## This file is part of Octave. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
9 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
11 ## under the terms of the GNU General Public License as published by |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
13 ## (at your option) any later version. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
14 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
15 ## Octave is distributed in the hope that it will be useful, but |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
18 ## GNU General Public License for more details. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
19 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
21 ## along with Octave; see the file COPYING. If not, see |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
23 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
24 ######################################################################## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
25 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
27 ## @deftypefn {} {[@var{t}, @var{y}] =} ode23s (@var{fcn}, @var{trange}, @var{init}) |
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
28 ## @deftypefnx {} {[@var{t}, @var{y}] =} ode23s (@var{fcn}, @var{trange}, @var{init}, @var{ode_opt}) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
29 ## @deftypefnx {} {[@var{t}, @var{y}] =} ode23s (@dots{}, @var{par1}, @var{par2}, @dots{}) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
30 ## @deftypefnx {} {[@var{t}, @var{y}, @var{te}, @var{ye}, @var{ie}] =} ode23s (@dots{}) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
31 ## @deftypefnx {} {@var{solution} =} ode23s (@dots{}) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
32 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
33 ## Solve a set of stiff Ordinary Differential Equations (stiff ODEs) with a |
28714
d8dcb36bb904
maint: spellcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28713
diff
changeset
|
34 ## @nospell{Rosenbrock} method of order (2,3). |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
35 ## |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
36 ## @var{fcn} is a function handle, inline function, or string containing the |
28332
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
37 ## name of the function that defines the ODE: @code{M y' = f(t,y)}. The |
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
38 ## function must accept two inputs where the first is time @var{t} and the |
28713
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28332
diff
changeset
|
39 ## second is a column vector of unknowns @var{y}. @var{M} is a constant mass |
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28332
diff
changeset
|
40 ## matrix, non-singular and possibly sparse. Set the field @qcode{"Mass"} in |
28332
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
41 ## @var{odeopts} using @var{odeset} to specify a mass matrix. |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
42 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
43 ## @var{trange} specifies the time interval over which the ODE will be |
28332
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
44 ## evaluated. Typically, it is a two-element vector specifying the initial |
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
45 ## and final times (@code{[tinit, tfinal]}). If there are more than two |
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
46 ## elements then the solution will also be evaluated at these intermediate |
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
47 ## time instances using an interpolation procedure of the same order as the |
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
48 ## one of the solver. |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
49 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
50 ## By default, @code{ode23s} uses an adaptive timestep with the |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
51 ## @code{integrate_adaptive} algorithm. The tolerance for the timestep |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
52 ## computation may be changed by using the options @qcode{"RelTol"} and |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
53 ## @qcode{"AbsTol"}. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
54 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
55 ## @var{init} contains the initial value for the unknowns. If it is a row |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
56 ## vector then the solution @var{y} will be a matrix in which each column is |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
57 ## the solution for the corresponding initial value in @var{init}. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
58 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
59 ## The optional fourth argument @var{ode_opt} specifies non-default options to |
28332
cf7edf5577c3
doc: Improve ODE solver documentation (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
28312
diff
changeset
|
60 ## the ODE solver. It is a structure generated by @code{odeset}. |
28713
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28332
diff
changeset
|
61 ## @code{ode23s} will ignore the following options: @qcode{"BDF"}, |
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28332
diff
changeset
|
62 ## @qcode{"InitialSlope"}, @qcode{"MassSingular"}, @qcode{"MStateDependence"}, |
28d2511f2af2
maint: grammarcheck documentation ahead of 6.1 release.
Rik <rik@octave.org>
parents:
28332
diff
changeset
|
63 ## @qcode{"MvPattern"}, @qcode{"MaxOrder"}, @qcode{"Non-negative"}. |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
64 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
65 ## The function typically returns two outputs. Variable @var{t} is a |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
66 ## column vector and contains the times where the solution was found. The |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
67 ## output @var{y} is a matrix in which each column refers to a different |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
68 ## unknown of the problem and each row corresponds to a time in @var{t}. If |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
69 ## @var{trange} specifies intermediate time steps, only those will be returned. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
70 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
71 ## The output can also be returned as a structure @var{solution} which has a |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
72 ## field @var{x} containing a row vector of times where the solution was |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
73 ## evaluated and a field @var{y} containing the solution matrix such that each |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
74 ## column corresponds to a time in @var{x}. Use |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
75 ## @w{@code{fieldnames (@var{solution})}} to see the other fields and |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
76 ## additional information returned. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
77 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
78 ## If using the @qcode{"Events"} option then three additional outputs may be |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
79 ## returned. @var{te} holds the time when an Event function returned a zero. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
80 ## @var{ye} holds the value of the solution at time @var{te}. @var{ie} |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
81 ## contains an index indicating which Event function was triggered in the case |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
82 ## of multiple Event functions. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
83 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
84 ## Example: Solve the stiff @nospell{Van der Pol} equation |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
85 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
86 ## @example |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
87 ## @group |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
88 ## f = @@(@var{t},@var{y}) [@var{y}(2); 1000*(1 - @var{y}(1)^2) * @var{y}(2) - @var{y}(1)]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
89 ## opt = odeset ('Mass', [1 0; 0 1], 'MaxStep', 1e-1); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
90 ## [vt, vy] = ode23s (f, [0 2000], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
91 ## @end group |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
92 ## @end example |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
93 ## @seealso{odeset, daspk, dassl} |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
94 ## @end deftypefn |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
95 |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
96 function varargout = ode23s (fcn, trange, init, varargin) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
97 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
98 if (nargin < 3) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
99 print_usage (); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
100 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
101 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
102 solver = "ode23s"; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
103 order = 2; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
104 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
105 if (nargin >= 4) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
106 if (! isstruct (varargin{1})) |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
107 ## varargin{1:len} are parameters for fcn |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
108 odeopts = odeset (); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
109 funarguments = varargin; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
110 elseif (numel (varargin) > 1) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
111 ## varargin{1} is an ODE options structure opt |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
112 odeopts = varargin{1}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
113 funarguments = {varargin{2:numel (varargin)}}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
114 else |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
115 ## varargin{1} is an ODE options structure opt |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
116 odeopts = varargin{1}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
117 funarguments = {}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
118 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
119 else # nargin == 3 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
120 odeopts = odeset (); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
121 funarguments = {}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
122 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
123 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
124 if (! isnumeric (trange) || ! isvector (trange)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
125 error ("Octave:invalid-input-arg", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
126 "ode23s: TRANGE must be a numeric vector"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
127 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
128 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
129 if (numel (trange) < 2) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
130 error ("Octave:invalid-input-arg", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
131 "ode23s: TRANGE must contain at least 2 elements"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
132 elseif (trange(2) == trange(1)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
133 error ("Octave:invalid-input-arg", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
134 "ode23s: invalid time span, TRANGE(1) == TRANGE(2)"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
135 else |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
136 direction = sign (trange(2) - trange(1)); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
137 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
138 trange = trange(:); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
139 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
140 if (! isnumeric (init) || ! isvector (init)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
141 error ("Octave:invalid-input-arg", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
142 "ode23s: INIT must be a numeric vector"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
143 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
144 init = init(:); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
145 |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
146 if (ischar (fcn)) |
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
147 if (! exist (fcn)) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
148 error ("Octave:invalid-input-arg", |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
149 ['ode23s: function "' fcn '" not found']); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
150 endif |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
151 fcn = str2func (fcn); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
152 endif |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
153 if (! is_function_handle (fcn)) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
154 error ("Octave:invalid-input-arg", |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
155 "ode23s: FCN must be a valid function handle"); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
156 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
157 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
158 ## Start preprocessing, have a look which options are set in odeopts, |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
159 ## check if an invalid or unused option is set. |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
160 [defaults, classes, attributes] = odedefaults (numel (init), |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
161 trange(1), trange(end)); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
162 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
163 persistent ode23s_ignore_options = ... |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
164 {"BDF", "InitialSlope", "MassSingular", "MStateDependence", ... |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
165 "MvPattern", "MaxOrder", "NonNegative"}; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
166 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
167 defaults = rmfield (defaults, ode23s_ignore_options); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
168 classes = rmfield (classes, ode23s_ignore_options); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
169 attributes = rmfield (attributes, ode23s_ignore_options); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
170 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
171 odeopts = odemergeopts ("ode23s", odeopts, defaults, classes, attributes); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
172 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
173 odeopts.funarguments = funarguments; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
174 odeopts.direction = direction; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
175 ## ode23s ignores "NonNegative" option, but integrate_adaptive needs it... |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
176 odeopts.havenonnegative = false; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
177 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
178 if (isempty (odeopts.OutputFcn) && nargout == 0) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
179 odeopts.OutputFcn = @odeplot; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
180 odeopts.haveoutputfunction = true; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
181 else |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
182 odeopts.haveoutputfunction = ! isempty (odeopts.OutputFcn); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
183 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
184 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
185 if (isempty (odeopts.InitialStep)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
186 odeopts.InitialStep = odeopts.direction * ... |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
187 starting_stepsize (order, fcn, trange(1), init, |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
188 odeopts.AbsTol, odeopts.RelTol, |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
189 strcmpi (odeopts.NormControl, |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
190 "on"), |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
191 odeopts.funarguments); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
192 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
193 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
194 if (! isempty (odeopts.Mass) && isnumeric (odeopts.Mass)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
195 havemasshandle = false; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
196 mass = odeopts.Mass; # constant mass |
31253
a40c0b7aa376
maint: changes to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
30893
diff
changeset
|
197 elseif (is_function_handle (odeopts.Mass)) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
198 havemasshandle = true; # mass defined by a function handle |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
199 odeopts.Mass = feval (odeopts.Mass, trange(1), init, |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
200 odeopts.funarguments{:}); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
201 else # no mass matrix - create a diag-matrix of ones for mass |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
202 havemasshandle = false; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
203 odeopts.Mass = diag (ones (length (init), 1), 0); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
204 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
205 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
206 ## Starting the initialization of the core solver ode23s |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
207 |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
208 if (numel (trange) > 2) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
209 odeopts.Refine = []; # disable Refine when specific times requested |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
210 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
211 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
212 solution = integrate_adaptive (@runge_kutta_23s, ... |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
213 order, fcn, trange, init, odeopts); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
214 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
215 ## Postprocessing, do whatever when terminating integration algorithm |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
216 if (odeopts.haveoutputfunction) # Cleanup plotter |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
217 feval (odeopts.OutputFcn, [], [], "done", odeopts.funarguments{:}); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
218 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
219 if (! isempty (odeopts.Events)) # Cleanup event function handling |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
220 ode_event_handler (odeopts.Events, solution.ode_t(end), ... |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
221 solution.ode_x(end,:).', "done", ... |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
222 odeopts.funarguments{:}); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
223 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
224 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
225 ## Print additional information if option Stats is set |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
226 if (strcmpi (odeopts.Stats, "on")) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
227 nsteps = solution.cntloop; # cntloop from 2..end |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
228 nfailed = solution.cntcycles - nsteps; # cntcycl from 1..end |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
229 nfevals = 5 * solution.cntcycles; # number of ode evaluations |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
230 ndecomps = nsteps; # number of LU decompositions |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
231 npds = 0; # number of partial derivatives |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
232 nlinsols = 3 * nsteps; # no. of solutions of linear systems |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
233 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
234 printf ("Number of successful steps: %d\n", nsteps); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
235 printf ("Number of failed attempts: %d\n", nfailed); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
236 printf ("Number of function calls: %d\n", nfevals); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
237 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
238 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
239 if (nargout == 2) |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
240 varargout{1} = solution.output_t; # Time stamps are first output argument |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
241 varargout{2} = solution.output_x; # Results are second output argument |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
242 elseif (nargout == 1) |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
243 varargout{1}.x = solution.ode_t.'; #Time stamps saved in field x (row vect.) |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
244 varargout{1}.y = solution.ode_x.'; #Results are saved in field y (row vect.) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
245 varargout{1}.solver = solver; # Solver name is saved in field solver |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
246 if (! isempty (odeopts.Events)) |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
247 varargout{1}.xe = solution.event{3}.'; # Time info when an event occurred |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
248 varargout{1}.ye = solution.event{4}.'; # Results when an event occurred |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
249 varargout{1}.ie = solution.event{2}.'; # Index info which event occurred |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
250 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
251 if (strcmpi (odeopts.Stats, "on")) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
252 varargout{1}.stats = struct (); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
253 varargout{1}.stats.nsteps = nsteps; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
254 varargout{1}.stats.nfailed = nfailed; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
255 varargout{1}.stats.nfevals = nfevals; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
256 varargout{1}.stats.npds = npds; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
257 varargout{1}.stats.ndecomps = ndecomps; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
258 varargout{1}.stats.nlinsols = nlinsols; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
259 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
260 elseif (nargout == 5) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
261 varargout = cell (1,5); |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
262 varargout{1} = solution.output_t; |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
263 varargout{2} = solution.output_x; |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
264 if (! isempty (odeopts.Events)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
265 varargout{3} = solution.event{3}; # Time info when an event occurred |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
266 varargout{4} = solution.event{4}; # Results when an event occurred |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
267 varargout{5} = solution.event{2}; # Index info which event occurred |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
268 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
269 endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
270 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
271 endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
272 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
273 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
274 %!demo |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
275 %! ## Demo function: stiff Van Der Pol equation |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
276 %! fcn = @(t,y) [y(2); 10*(1-y(1)^2)*y(2)-y(1)]; |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
277 %! ## Calling ode23s method |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
278 %! tic () |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
279 %! [vt, vy] = ode23s (fcn, [0 20], [2 0]); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
280 %! toc () |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
281 %! ## Plotting the result |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
282 %! plot (vt,vy(:,1),'-o'); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
283 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
284 %!demo |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
285 %! ## Demo function: stiff Van Der Pol equation |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
286 %! fcn = @(t,y) [y(2); 10*(1-y(1)^2)*y(2)-y(1)]; |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
287 %! ## Calling ode23s method |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
288 %! odeopts = odeset ("Jacobian", @(t,y) [0 1; -20*y(1)*y(2)-1, 10*(1-y(1)^2)], |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
289 %! "InitialStep", 1e-3) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
290 %! tic () |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
291 %! [vt, vy] = ode23s (fcn, [0 20], [2 0], odeopts); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
292 %! toc () |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
293 %! ## Plotting the result |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
294 %! plot (vt,vy(:,1),'-o'); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
295 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
296 %!demo |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
297 %! ## Demo function: stiff Van Der Pol equation |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
298 %! fcn = @(t,y) [y(2); 100*(1-y(1)^2)*y(2)-y(1)]; |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
299 %! ## Calling ode23s method |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
300 %! odeopts = odeset ("InitialStep", 1e-4); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
301 %! tic () |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
302 %! [vt, vy] = ode23s (fcn, [0 200], [2 0]); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
303 %! toc () |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
304 %! ## Plotting the result |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
305 %! plot (vt,vy(:,1),'-o'); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
306 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
307 %!demo |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
308 %! ## Demo function: stiff Van Der Pol equation |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
309 %! fcn = @(t,y) [y(2); 100*(1-y(1)^2)*y(2)-y(1)]; |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
310 %! ## Calling ode23s method |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
311 %! odeopts = odeset ("Jacobian", @(t,y) [0 1; -200*y(1)*y(2)-1, 100*(1-y(1)^2)], |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
312 %! "InitialStep", 1e-4); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
313 %! tic () |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
314 %! [vt, vy] = ode23s (fcn, [0 200], [2 0], odeopts); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
315 %! toc () |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
316 %! ## Plotting the result |
28912
0de38a6ef693
maint: Use Octave convention of space after function name in scripts dir.
Rik <rik@octave.org>
parents:
28896
diff
changeset
|
317 %! plot (vt,vy(:,1),'-o'); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
318 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
319 %!demo |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
320 %! ## Demonstrate convergence order for ode23s |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
321 %! tol = 1e-5 ./ 10.^[0:5]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
322 %! for i = 1 : numel (tol) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
323 %! opt = odeset ("RelTol", tol(i), "AbsTol", realmin); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
324 %! [t, y] = ode23s (@(t, y) -y, [0, 1], 1, opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
325 %! h(i) = 1 / (numel (t) - 1); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
326 %! err(i) = norm (y .* exp (t) - 1, Inf); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
327 %! endfor |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
328 %! |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
329 %! ## Estimate order visually |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
330 %! loglog (h, tol, "-ob", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
331 %! h, err, "-b", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
332 %! h, (h/h(end)) .^ 2 .* tol(end), "k--", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
333 %! h, (h/h(end)) .^ 3 .* tol(end), "k-"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
334 %! axis tight |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
335 %! xlabel ("h"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
336 %! ylabel ("err(h)"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
337 %! title ("Convergence plot for ode23s"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
338 %! legend ("imposed tolerance", "ode23s (relative) error", |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
339 %! "order 2", "order 3", "location", "northwest"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
340 %! |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
341 %! ## Estimate order numerically |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
342 %! p = diff (log (err)) ./ diff (log (h)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
343 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
344 %!test |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
345 %! [vt, vy] = ode23s (@(t,y) t - y + 1, [0 10], [1]); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
346 %! assert ([vt(end), vy(end)], [10, exp(-10) + 10], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
347 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
348 %!test |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
349 %! opts = odeset ('Mass', 5, 'Jacobian', -5, 'JConstant', 'on'); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
350 %! [vt, vy] = ode23s (@(t,y) 5 * (t - y + 1), [0 10], [1], opts); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
351 %! assert ([vt(end), vy(end)], [10, exp(-10) + 10], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
352 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
353 ## We are using the "Van der Pol" implementation for all tests that are done |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
354 ## for this function. For further tests we also define a reference solution |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
355 ## (computed at high accuracy). |
28563
5a07c798eb08
avoid function call input or output argument number mismatch
John W. Eaton <jwe@octave.org>
parents:
28332
diff
changeset
|
356 %!function ydot = fpol (t, y, varargin) # The Van der Pol ODE |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
357 %! ydot = [y(2); 10 * (1 - y(1)^2) * y(2) - y(1)]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
358 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
359 %!function ydot = jac (t, y) # The Van der Pol ODE |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
360 %! ydot = [0 1; -20 * y(1) * y(2) - 1, 10 * (1 - y(1)^2)]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
361 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
362 %!function ref = fref () # The computed reference sol |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
363 %! ref = [1.8610687248524305 -0.0753216319179125]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
364 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
365 %!function [val, trm, dir] = feve (t, y, varargin) |
28563
5a07c798eb08
avoid function call input or output argument number mismatch
John W. Eaton <jwe@octave.org>
parents:
28332
diff
changeset
|
366 %! val = fpol (t, y, varargin{:}); # We use the derivatives |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
367 %! trm = zeros (2,1); # that's why component 2 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
368 %! dir = ones (2,1); # does not seem to be exact |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
369 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
370 %!function [val, trm, dir] = fevn (t, y, varargin) |
28563
5a07c798eb08
avoid function call input or output argument number mismatch
John W. Eaton <jwe@octave.org>
parents:
28332
diff
changeset
|
371 %! val = fpol (t, y, varargin{:}); # We use the derivatives |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
372 %! trm = ones (2,1); # that's why component 2 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
373 %! dir = ones (2,1); # does not seem to be exact |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
374 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
375 %!function mas = fmas (t, y, varargin) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
376 %! mas = [1, 0; 0, 1]; # Dummy mass matrix for tests |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
377 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
378 %!function mas = fmsa (t, y, varargin) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
379 %! mas = sparse ([1, 0; 0, 1]); # A sparse dummy matrix |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
380 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
381 %!function out = fout (t, y, flag, varargin) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
382 %! out = false; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
383 %! if (strcmp (flag, "init")) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
384 %! if (! isequal (size (t), [2, 1])) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
385 %! error ('fout: step "init"'); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
386 %! endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
387 %! elseif (isempty (flag)) |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
388 %! # Multiple steps can be sent in one function call |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
389 %! if (! isequal ( size (t), size (y))) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
390 %! error ('fout: step "calc"'); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
391 %! endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
392 %! elseif (strcmp (flag, "done")) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
393 %! if (! isempty (t)) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
394 %! warning ('fout: step "done"'); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
395 %! endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
396 %! else |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
397 %! error ("fout: invalid flag <%s>", flag); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
398 %! endif |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
399 %!endfunction |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
400 %! |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
401 %!test # two output arguments |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
402 %! [t, y] = ode23s (@fpol, [0 2], [2 0]); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
403 %! assert ([t(end), y(end,:)], [2, fref], 1e-3); |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
404 %!test # correct number of steps with Refine |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
405 %! [t1, y1] = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 1)); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
406 %! [t2, y2] = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 4)); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
407 %! [t3, y3] = ode23s (@fpol, [0 2], [2 0]); #default Refine=1 |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
408 %! s = ode23s (@fpol, [0 2], [2 0], odeset ("Refine", 4)); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
409 %! assert (length (t1) == length (t3)); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
410 %! assert (length (t2) == 4*length (t1) - 3); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
411 %! assert (length (s.x) == length (t1)); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
412 %!test # anonymous function instead of real function |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
413 %! fvdp = @(t,y) [y(2); 10 * (1 - y(1)^2) * y(2) - y(1)]; |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
414 %! [t, y] = ode23s (fvdp, [0 2], [2 0]); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
415 %! assert ([t(end), y(end,:)], [2, fref], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
416 %!test # extra input arguments passed through |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
417 %! [t, y] = ode23s (@fpol, [0 2], [2 0], 12, 13, "KL"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
418 %! assert ([t(end), y(end,:)], [2, fref], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
419 %!test # empty OdePkg structure *but* extra input arguments |
28892
3cac3ceb9629
maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents:
28715
diff
changeset
|
420 %! opt = odeset (); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
421 %! [t, y] = ode23s (@fpol, [0 2], [2 0], opt, 12, 13, "KL"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
422 %! assert ([t(end), y(end,:)], [2, fref], 1e-2); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
423 %!test # InitialStep option |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
424 %! opt = odeset ("InitialStep", 1e-8); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
425 %! [t, y] = ode23s (@fpol, [0 0.2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
426 %! assert ([t(2)-t(1)], [1e-8], 1e-9); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
427 %!test # MaxStep option |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
428 %! opt = odeset ("MaxStep", 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
429 %! sol = ode23s (@fpol, [0 0.2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
430 %! assert ([sol.x(5)-sol.x(4)], [1e-3], 1e-4); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
431 %!test # AbsTol option |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
432 %! opt = odeset ("AbsTol", 1e-5); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
433 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
434 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
435 %!test # AbsTol and RelTol option |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
436 %! opt = odeset ("AbsTol", 1e-8, "RelTol", 1e-8); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
437 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
438 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
439 %!test # RelTol and NormControl option -- higher accuracy |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
440 %! opt = odeset ("RelTol", 1e-8, "NormControl", "on"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
441 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
442 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-4); |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
443 %!test # Details of OutputSel can't be tested |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
444 %! opt = odeset ("OutputFcn", @fout, "OutputSel", 1); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
445 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
446 %!test # Stats must add further elements in sol |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
447 %! opt = odeset ("Stats", "on"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
448 %! stat_str = evalc ("sol = ode23s (@fpol, [0 2], [2 0], opt);"); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
449 %! assert (strncmp (stat_str, "Number of successful steps:", 27)); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
450 %! assert (isfield (sol, "stats")); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
451 %! assert (isfield (sol.stats, "nsteps")); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
452 %!test # Events option add further elements in sol |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
453 %! opt = odeset ("Events", @feve); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
454 %! sol = ode23s (@fpol, [0 10], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
455 %! assert (isfield (sol, "ie")); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
456 %! assert (sol.ie(1), 2); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
457 %! assert (isfield (sol, "xe")); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
458 %! assert (isfield (sol, "ye")); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
459 %!test # Mass option as function |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
460 %! opt = odeset ("Mass", @fmas); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
461 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
462 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
463 %!test # Mass option as matrix |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
464 %! opt = odeset ("Mass", eye (2,2)); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
465 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
466 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
467 %!test # Mass option as sparse matrix |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
468 %! opt = odeset ("Mass", sparse (eye (2,2))); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
469 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
470 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
471 %!test # Mass option as function and sparse matrix |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
472 %! opt = odeset ("Mass", @fmsa); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
473 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
474 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
475 %!test # Jacobian option as function |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
476 %! opt = odeset ('Jacobian', @jac); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
477 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
478 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
28312
dddc6c2661bc
ode23s.m: Make tests with sparse Jacobian or JPattern conditional on UMFPACK.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28308
diff
changeset
|
479 %!testif HAVE_UMFPACK # Sparse Jacobian |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
480 %! jac = @(t, y) sparse ([0 1; -20*y(1)*y(2)-1, 10*(1-y(1)^2)]); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
481 %! opt = odeset ('Jacobian', jac); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
482 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
483 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
28312
dddc6c2661bc
ode23s.m: Make tests with sparse Jacobian or JPattern conditional on UMFPACK.
Markus Mützel <markus.muetzel@gmx.de>
parents:
28308
diff
changeset
|
484 %!testif HAVE_UMFPACK # Jpattern |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
485 %! S = sparse ([0 1; 1 1]); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
486 %! opt = odeset ("Jpattern", S); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
487 %! sol = ode23s (@fpol, [0 2], [2 0], opt); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
488 %! assert ([sol.x(end); sol.y(:,end)], [2; fref'], 1e-3); |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
489 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
490 ## Note: The following options have no effect on this solver |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
491 ## therefore it makes no sense to test them here: |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
492 ## |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
493 ## "BDF" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
494 ## "InitialSlope" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
495 ## "MassSingular" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
496 ## "MStateDependence" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
497 ## "MaxOrder" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
498 ## "MvPattern" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
499 ## "NonNegative" |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
500 |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
501 %!test # Check that imaginary part of solution does not get inverted |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
502 %! sol = ode23s (@(x,y) 1, [0 1], 1i); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
503 %! assert (imag (sol.y), ones (size (sol.y))); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
504 %! [x, y] = ode23s (@(x,y) 1, [0 1], 1i); |
28929
9e43deb9bfc3
maint: Use semicolon after assert statement inside %!test blocks.
Rik <rik@octave.org>
parents:
28912
diff
changeset
|
505 %! assert (imag (y), ones (size (y))); |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
506 |
31263
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
507 ## FIXME: convert to demo or a visible=off test with failable assert/error |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
508 ## statemments |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
509 ##%!test # Make sure odeplot works (default OutputFcn when no return value) |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
510 ##%! ode23s (@fpol, [0 2], [2 0]); |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
511 ##%! close all |
449ed6f427cb
ode45/23/23s: Implement Events, OutputFcn, & Refine options (bug #49408 and #63063)
Ken Marek <marek_ka@mercer.edu>
parents:
31253
diff
changeset
|
512 |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
513 ## Test input validation |
28896
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
514 %!error <Invalid call> ode23s () |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
515 %!error <Invalid call> ode23s (1) |
90fea9cc9caa
test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
516 %!error <Invalid call> ode23s (1,2) |
28308
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
517 %!error <TRANGE must be a numeric> ode23s (@fpol, {[0 25]}, [3 15 1]) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
518 %!error <TRANGE must be a .* vector> ode23s (@fpol, [0 25; 25 0], [3 15 1]) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
519 %!error <TRANGE must contain at least 2 elements> ode23s (@fpol, [1], [3 15 1]) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
520 %!error <invalid time span> ode23s (@fpol, [1 1], [3 15 1]) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
521 %!error <INIT must be a numeric> ode23s (@fpol, [0 25], {[3 15 1]}) |
8085ae13cc4a
ode23s.m: new function from former odepkg (bug #57309)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
diff
changeset
|
522 %!error <INIT must be a .* vector> ode23s (@fpol, [0 25], [3 15 1; 3 15 1]) |
30893
e1788b1a315f
maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents:
30564
diff
changeset
|
523 %!error <FCN must be a valid function handle> ode23s (1, [0 25], [3 15 1]) |