]> Creatis software - CreaPhase.git/blob - octave_packages/odepkg-0.8.2/odephas2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / odepkg-0.8.2 / odephas2.m
1 %# Copyright (C) 2006-2012, Thomas Treichl <treichl@users.sourceforge.net>
2 %# OdePkg - A package for solving ordinary differential equations and more
3 %#
4 %# This program is free software; you can redistribute it and/or modify
5 %# it under the terms of the GNU General Public License as published by
6 %# the Free Software Foundation; either version 2 of the License, or
7 %# (at your option) any later version.
8 %#
9 %# This program is distributed in the hope that it will be useful,
10 %# but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 %# GNU General Public License for more details.
13 %#
14 %# You should have received a copy of the GNU General Public License
15 %# along with this program; If not, see <http://www.gnu.org/licenses/>.
16
17 %# -*- texinfo -*-
18 %# @deftypefn {Function File} {[@var{ret}] =} odephas2 (@var{t}, @var{y}, @var{flag})
19 %#
20 %# Open a new figure window and plot the first result from the variable @var{y} that is of type double column vector over the second result from the variable @var{y} while solving. The types and the values of the input parameter @var{t} and the output parameter @var{ret} depend on the input value @var{flag} that is of type string. If @var{flag} is
21 %# @table @option
22 %# @item  @code{"init"}
23 %# then @var{t} must be a double column vector of length 2 with the first and the last time step and nothing is returned from this function,
24 %# @item  @code{""}
25 %# then @var{t} must be a double scalar specifying the actual time step and the return value is false (resp. value 0) for 'not stop solving',
26 %# @item  @code{"done"}
27 %# then @var{t} must be a double scalar specifying the last time step and nothing is returned from this function.
28 %# @end table
29 %#
30 %# This function is called by a OdePkg solver function if it was specified in an OdePkg options structure with the @command{odeset}. This function is an OdePkg internal helper function therefore it should never be necessary that this function is called directly by a user. There is only little error detection implemented in this function file to achieve the highest performance.
31 %#
32 %# For example, solve an anonymous implementation of the "Van der Pol" equation and display the results while solving in a 2D plane
33 %# @example
34 %# fvdb = @@(vt,vy) [vy(2); (1 - vy(1)^2) * vy(2) - vy(1)];
35 %# 
36 %# vopt = odeset ('OutputFcn', @@odephas2, 'RelTol', 1e-6);
37 %# vsol = ode45 (fvdb, [0 20], [2 0], vopt);
38 %# @end example
39 %# @end deftypefn
40 %#
41 %# @seealso{odepkg}
42
43 function [varargout] = odephas2 (vt, vy, vflag)
44
45   %# No input argument check is done for a higher processing speed
46   persistent vfigure; persistent vyold; persistent vcounter;
47
48   if (strcmp (vflag, 'init'))
49     %# Nothing to return, vt is either the time slot [tstart tstop]
50     %# or [t0, t1, ..., tn], vy is the inital value vector 'vinit'
51     vfigure = figure; vyold = vy(:,1); vcounter = 1;
52
53   elseif (isempty (vflag))
54     %# Return something in varargout{1}, either false for 'not stopping
55     %# the integration' or true for 'stopping the integration'
56     vcounter = vcounter + 1; figure (vfigure);
57     vyold(:,vcounter) = vy(:,1); 
58     plot (vyold(1,:), vyold(2,:), '-o', 'markersize', 1);
59     drawnow; varargout{1} = false; 
60
61   elseif (strcmp (vflag, 'done'))
62     %# Cleanup has to be done, clear the persistent variables because
63     %# we don't need them anymore
64     clear ('vfigure', 'vyold', 'vcounter');
65
66   end
67
68 %# Local Variables: ***
69 %# mode: octave ***
70 %# End: ***