annotate scripts/ode/odeplot.m @ 29359:7854d5752dd2

maint: merge stable to default.
author John W. Eaton <jwe@octave.org>
date Wed, 10 Feb 2021 10:10:40 -0500
parents 3cac3ceb9629 0a5b15007766
children 796f54d4ddbf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
29358
0a5b15007766 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27978
diff changeset
3 ## Copyright (C) 2006-2021 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
7 ##
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
8 ## This file is part of Octave.
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22692
diff changeset
13 ## (at your option) any later version.
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
14 ##
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22692
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22692
diff changeset
18 ## GNU General Public License for more details.
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
19 ##
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
25
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
26 ## -*- texinfo -*-
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
27 ## @deftypefn {} {@var{stop_solve} =} odeplot (@var{t}, @var{y}, @var{flag})
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
28 ##
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
29 ## Open a new figure window and plot the solution of an ode problem at each
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
30 ## time step during the integration.
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
31 ##
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
32 ## The types and values of the input parameters @var{t} and @var{y} depend on
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
33 ## the input @var{flag} that is of type string. Valid values of @var{flag}
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
34 ## are:
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
35 ##
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
36 ## @table @option
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
37 ## @item @qcode{"init"}
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
38 ## The input @var{t} must be a column vector of length 2 with the first and
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
39 ## last time step (@code{[@var{tfirst} @var{tlast}]}. The input @var{y}
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
40 ## contains the initial conditions for the ode problem (@var{y0}).
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
41 ##
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
42 ## @item @qcode{""}
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
43 ## The input @var{t} must be a scalar double specifying the time for which
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
44 ## the solution in input @var{y} was calculated.
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
45 ##
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
46 ## @item @qcode{"done"}
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
47 ## The inputs should be empty, but are ignored if they are present.
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
48 ## @end table
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
49 ##
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
50 ## @code{odeplot} always returns false, i.e., don't stop the ode solver.
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
51 ##
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
52 ## Example: solve an anonymous implementation of the
21123
71408ab5071e doc: Periodic spellcheck of documentation.
Rik <rik@octave.org>
parents: 21122
diff changeset
53 ## @nospell{@qcode{"Van der Pol"}} equation and display the results while
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
54 ## solving.
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
55 ##
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
56 ## @example
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
57 ## @group
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
58 ## fvdp = @@(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)];
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
59 ##
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
60 ## opt = odeset ("OutputFcn", @@odeplot, "RelTol", 1e-6);
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
61 ## sol = ode45 (fvdp, [0 20], [2 0], opt);
21122
dba88797f69f doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents: 20935
diff changeset
62 ## @end group
20935
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
63 ## @end example
d9e05450936f cleanup odeplot style
Carlo de Falco <carlo.defalco@polimi.it>
parents: 20928
diff changeset
64 ##
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
65 ## Background Information:
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
66 ## This function is called by an ode solver function if it was specified in
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
67 ## the @qcode{"OutputFcn"} property of an options structure created with
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
68 ## @code{odeset}. The ode solver will initially call the function with the
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
69 ## syntax @code{odeplot ([@var{tfirst}, @var{tlast}], @var{y0}, "init")}. The
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
70 ## function initializes internal variables, creates a new figure window, and
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
71 ## sets the x limits of the plot. Subsequently, at each time step during the
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
72 ## integration the ode solver calls @code{odeplot (@var{t}, @var{y}, [])}.
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
73 ## At the end of the solution the ode solver calls
22684
ce0f54b63923 odeplot.m: Fix typo in documentation.
Rik <rik@octave.org>
parents: 22639
diff changeset
74 ## @code{odeplot ([], [], "done")} so that odeplot can perform any clean-up
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
75 ## actions required.
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
76 ## @seealso{odeset, odeget, ode23, ode45}
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
77 ## @end deftypefn
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
78
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
79 function stop_solve = odeplot (t, y, flag)
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
80
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
81 ## No input argument checking is done for better performance
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
82 persistent hlines num_lines told yold;
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
83
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
84 ## odeplot never stops the integration
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
85 stop_solve = false;
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
86
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
87 if (isempty (flag))
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
88 ## Default case, plot and return a value
22673
7c45d565d1b7 odeplot.m: Accept multiple input times (bug #49449).
Rik <rik@octave.org>
parents: 22639
diff changeset
89 told = [told; t(:)];
7c45d565d1b7 odeplot.m: Accept multiple input times (bug #49449).
Rik <rik@octave.org>
parents: 22639
diff changeset
90 yold = [yold, y];
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
91 for i = 1:num_lines
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
92 set (hlines(i), "xdata", told, "ydata", yold(i,:));
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
93 endfor
28892
3cac3ceb9629 maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents: 27978
diff changeset
94 drawnow ();
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
95
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
96 retval = false;
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
97
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
98 elseif (strcmp (flag, "init"))
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
99 ## t is either the time slot [tstart tstop] or [t0, t1, ..., tn]
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
100 ## y is the initial value vector for the ode solution
22684
ce0f54b63923 odeplot.m: Fix typo in documentation.
Rik <rik@octave.org>
parents: 22639
diff changeset
101 told = t(1);
22692
5e2cb8fbfec7 odeplot.m: Redo changes accidentally reverted through merges from stable to default.
Rik <rik@octave.org>
parents: 22689
diff changeset
102 yold = y(:);
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
103 figure ();
22673
7c45d565d1b7 odeplot.m: Accept multiple input times (bug #49449).
Rik <rik@octave.org>
parents: 22639
diff changeset
104 hlines = plot (told, yold, "o-");
22639
7efa2d0e22c9 More Matlab-compatible implementation of OutputFcn. Clean up odeplot.m.
Rik <rik@octave.org>
parents: 22638
diff changeset
105 xlim ([t(1), t(end)]); # Fix limits which also speeds up plotting
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
106 num_lines = numel (hlines);
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
107
20928
2b8447888e0a strip trailing whitespace from files
John W. Eaton <jwe@octave.org>
parents: 20900
diff changeset
108 elseif (strcmp (flag, "done"))
22626
869c02fde46c Further clean-up of ode functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
109 ## Cleanup after ode solver has finished.
22692
5e2cb8fbfec7 odeplot.m: Redo changes accidentally reverted through merges from stable to default.
Rik <rik@octave.org>
parents: 22689
diff changeset
110 hlines = num_lines = told = yold = [];
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
111
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
112 endif
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
113
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
114 endfunction
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
115
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
116
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
117 %!demo
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
118 %! ## Solve an anonymous implementation of the Van der Pol equation
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
119 %! ## and display the results while solving
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
120 %! fvdp = @(t,y) [y(2); (1 - y(1)^2) * y(2) - y(1)];
20899
59d49077825f 2015 Codesprint: make odeplot non private.
Stefan Miereis <stefan.miereis@gmx.de>
parents:
diff changeset
121 %! opt = odeset ("OutputFcn", @odeplot, "RelTol", 1e-6);
22638
0128795eeac6 odeplot.m: Recode for performance.
Rik <rik@octave.org>
parents: 22626
diff changeset
122 %! sol = ode45 (fvdp, [0 20], [2 0], opt);