Mercurial > octave
annotate scripts/ode/private/ode_event_handler.m @ 23079:17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
* integrate_adaptive.m: new variable have_EventFcn to make code more readable.
* ode_event_handler.m: New persistent variable firstrun. Set to true when
called with 'init'. Toggled back to false after one normal invocation.
Ignore terminating the ODE integration if it occurs on the first time step.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 21 Jan 2017 16:19:38 -0800 |
parents | 3a2b891d0b33 |
children | ef4d915df748 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
20852
diff
changeset
|
1 ## Copyright (C) 2006-2016 Thomas Treichl <treichl@users.sourceforge.net> |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
2 ## |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
3 ## This file is part of Octave. |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
4 ## |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22655
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22655
diff
changeset
|
8 ## (at your option) any later version. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
9 ## |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22655
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22655
diff
changeset
|
13 ## GNU General Public License for more details. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
14 ## |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
18 |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
19 ## -*- texinfo -*- |
22626 | 20 ## @deftypefn {} {@var{retval} =} ode_event_handler (@var{@@evtfun}, @var{t}, @var{y}, @var{flag}, @var{par1}, @var{par2}, @dots{}) |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
21 ## |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
22 ## Return the solution of the event function (@var{@@evtfun}) which is |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
23 ## specified in the form of a function handle. |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
24 # |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
25 ## The second input argument @var{t} is a scalar double and specifies the time |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
26 ## of the event evaluation. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
27 ## |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
28 ## The third input argument @var{y} may be a column vector of type double |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
29 ## (for ODEs and DAEs) which specifies the solutions. Alternatives, @var{y} |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
30 ## may be a cell array (for IDEs and DDEs) which specifies the solutions and |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
31 ## derivatives. |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
32 ## |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
33 ## The fourth input argument @var{flag} is of type string. Valid values are: |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
34 ## |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
35 ## @table @option |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
36 ## @item @qcode{"init"} |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
37 ## Initialize internal persistent variables of the function |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
38 ## @code{ode_event_handler} and return an empty cell array of size 4. |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
39 ## |
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
40 ## @item @qcode{"calc"} |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
41 ## Evaluate the event function and return the solution @var{retval} as a cell |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
42 ## array of size 4. |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
43 ## |
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
44 ## @item @qcode{"done"} |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
45 ## Clean up internal variables of the function @code{ode_event_handler} and |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
46 ## return an empty cell array of size 4. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
47 ## @end table |
20548
25623ef2ff4f
doc: Rewrite docstrings for ode* family of functions.
Rik <rik@octave.org>
parents:
20536
diff
changeset
|
48 ## |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
49 ## If additional input arguments @var{par1}, @var{par2}, @dots{} are given |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
50 ## these parameters are passed directly to the event function. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
51 ## |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
52 ## This function is an ODE internal helper function and it should never be |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
53 ## necessary to call it directly. |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
54 ## @end deftypefn |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
55 |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
56 function retval = ode_event_handler (evtfun, t, y, flag = "", varargin) |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
57 |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
58 ## No error handling has been implemented in this function to achieve |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
59 ## the highest performance possible. |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
60 |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
61 ## retval{1} is true (to terminate) or false (to continue) |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
62 ## retval{2} is the index information for which an event occurred |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
63 ## retval{3} is the time information column vector |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
64 ## retval{4} is the line by line result information matrix |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
65 |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
66 ## These persistent variables store the results and time value from the |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
67 ## processing in the previous time stamp. |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
68 ## evtold the results from the event function |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
69 ## told the time stamp |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
70 ## yold the ODE result |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
71 ## retcell the return values cell array |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
72 ## evtcnt the counter for how often this function has been called |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
73 persistent evtold told yold retcell; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
74 persistent evtcnt = 1; # Don't remove. Required for Octave parser. |
23079
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
75 persistent firstrun = true; |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
76 |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
77 if (isempty (flag)) |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
78 ## Process the event, i.e., |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
79 ## find the zero crossings for either a rising or falling edge |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
80 if (! iscell (y)) |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
81 inpargs = {evtfun, t, y}; |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
82 else |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
83 inpargs = {evtfun, t, y{1}, y{2}}; |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
84 y = y{1}; # Delete cell element 2 |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
85 endif |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
86 if (nargin > 4) |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
87 inpargs = {inpargs{:}, varargin{:}}; |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
88 endif |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
89 [evt, term, dir] = feval (inpargs{:}); |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
90 |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
91 ## We require that all return values be row vectors |
22655
6b134d294d61
ode solvers: use ordinary transpose instead of Hermitian conjugate (bug #49410).
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22648
diff
changeset
|
92 evt = evt(:).'; term = term(:).'; dir = dir(:).'; |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
93 |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
94 ## Check if one or more signs of the event has changed |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
95 signum = (sign (evtold) != sign (evt)); |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
96 if (any (signum)) # One or more values have changed |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
97 idx = find (signum); # Get the index of the changed values |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
98 |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
99 if (any (dir(idx) == 0)) |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
100 ## Rising or falling (both are possible) |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
101 ## Don't change anything, keep the index |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
102 elseif (any (dir(idx) == sign (evt(idx)))) |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
103 ## Detected rising or falling, need a new index |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
104 idx = find (dir == sign (evt)); |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
105 else |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
106 ## Found a zero crossing but must not be notified |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
107 idx = []; |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
108 endif |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
109 |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
110 ## Create new output values if a valid index has been found |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
111 if (! isempty (idx)) |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
112 ## Change the persistent result cell array |
23079
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
113 if (firstrun) |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
114 ## Matlab compatibility requires ignoring condition on first run. |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
115 retcell{1} = false; |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
116 else |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
117 retcell{1} = any (term(idx)); # Stop integration or not |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
118 endif |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
119 retcell{2}(evtcnt,1) = idx(1,1); # Take first event found |
20536
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
120 ## Calculate the time stamp when the event function returned 0 and |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
121 ## calculate new values for the integration results, we do both by |
6256f6e366ac
Fix copyright text in private ode functions
Carlo de Falco <carlo.defalco@polimi.it>
parents:
20533
diff
changeset
|
122 ## a linear interpolation |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
123 tnew = t - evt(1,idx) * (t - told) / (evt(1,idx) - evtold(1,idx)); |
22655
6b134d294d61
ode solvers: use ordinary transpose instead of Hermitian conjugate (bug #49410).
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22648
diff
changeset
|
124 ynew = (y - (t - tnew) * (y - yold) / (t - told)).'; |
20634
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
125 retcell{3}(evtcnt,1) = tnew; |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
126 retcell{4}(evtcnt,:) = ynew; |
80e630b37ba1
maint: Remove unnecessary 'v' prefix before variables in ODE m-files.
Rik <rik@octave.org>
parents:
20631
diff
changeset
|
127 evtcnt += 1; |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
128 endif |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
129 |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
130 endif |
23079
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
131 |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
132 firstrun = false; |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
133 evtold = evt; told = t; yold = y; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
134 retval = retcell; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
135 |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
136 elseif (strcmp (flag, "init")) |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
137 ## Call the event function if an event function has been defined to |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
138 ## initialize the internal variables of the event function and to get |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
139 ## a value for evtold. |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
140 |
23079
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
141 firstrun = true; |
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
142 |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
143 if (! iscell (y)) |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
144 inpargs = {evtfun, t, y}; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
145 else |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
146 inpargs = {evtfun, t, y{1}, y{2}}; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
147 y = y{1}; # Delete cell element 2 |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
148 endif |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
149 if (nargin > 4) |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
150 inpargs = {inpargs{:}, varargin{:}}; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
151 endif |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
152 [evtold, ~, ~] = feval (inpargs{:}); |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
153 |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
154 ## We require that all return values be row vectors |
22655
6b134d294d61
ode solvers: use ordinary transpose instead of Hermitian conjugate (bug #49410).
Carlo de Falco <carlo.defalco@polimi.it>
parents:
22648
diff
changeset
|
155 evtold = evtold(:).'; told = t; yold = y; |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
156 evtcnt = 1; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
157 retval = retcell = cell (1,4); |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
158 |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
159 elseif (strcmp (flag, "done")) |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
160 ## Clear this event handling function |
23079
17dc6c7ef427
Ignore ODE event function at t==0 for Matlab compatibility (bug #49919).
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
161 firstrun = true; |
22648
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
162 evtold = told = yold = evtcnt = []; |
9bc03a3f7a34
ode_event_handler.m: Overhaul function.
Rik <rik@octave.org>
parents:
22626
diff
changeset
|
163 retval = retcell = cell (1,4); |
20533
fcb792acab9b
Moving ode45, odeset, odeget, and levenshtein from odepkg to core.
jcorno <jacopo.corno@gmail.com>
parents:
diff
changeset
|
164 |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
165 endif |
20621
b92f8e148936
maint: Continued clean-up of functions in ode/private dir.
Rik <rik@octave.org>
parents:
20554
diff
changeset
|
166 |
20552
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
167 endfunction |
eb9e2d187ed2
maint: Use Octave coding conventions in scripts/ode/private dir.
Rik <rik@octave.org>
parents:
20548
diff
changeset
|
168 |