1 %# Copyright (C) 2006-2012, Thomas Treichl <treichl@users.sourceforge.net>
2 %# OdePkg - A package for solving ordinary differential equations and more
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.
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.
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/>.
18 %# @deftypefn {Function File} {[@var{ret}] =} odephas3 (@var{t}, @var{y}, @var{flag})
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 and the third 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
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,
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.
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.
32 %# For example, solve the "Lorenz attractor" and display the results while solving in a 3D plane
34 %# function vyd = florenz (vt, vx)
35 %# vyd = [10 * (vx(2) - vx(1));
36 %# vx(1) * (28 - vx(3));
37 %# vx(1) * vx(2) - 8/3 * vx(3)];
40 %# vopt = odeset ('OutputFcn', @@odephas3);
41 %# vsol = ode23 (@@florenz, [0:0.01:7.5], [3 15 1], vopt);
47 function [varargout] = odephas3 (vt, vy, vflag)
49 %# vt and vy are always column vectors, vflag can be either 'init'
50 %# or [] or 'done'. If 'init' then varargout{1} = [], if [] the
51 %# varargout{1} either true or false, if 'done' then varargout{1} = [].
52 persistent vfigure; persistent vyold; persistent vcounter;
54 if (strcmp (vflag, 'init'))
55 %# vt is either the time slot [tstart tstop] or [t0, t1, ..., tn]
56 %# vy is the inital value vector vinit from the caller function
57 vfigure = figure; vyold = vy(:,1); vcounter = 1;
59 elseif (isempty (vflag))
60 %# Return something in varargout{1}, either false for 'not stopping
61 %# the integration' or true for 'stopping the integration'
62 vcounter = vcounter + 1; figure (vfigure);
63 vyold(:,vcounter) = vy(:,1);
64 plot3 (vyold(1,:), vyold(2,:), vyold (3,:), '-o', 'markersize', 1);
65 drawnow; varargout{1} = false;
67 elseif (strcmp (vflag, 'done'))
68 %# Cleanup has to be done, clear the persistent variables because
69 %# we don't need them anymore
70 clear ('vfigure', 'vyold', 'vcounter');
74 %# Local Variables: ***