]> Creatis software - CreaPhase.git/blob - octave_packages/odepkg-0.8.2/odepkg_examples_dde.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / odepkg-0.8.2 / odepkg_examples_dde.m
1 %# Copyright (C) 2008-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{}] =} odepkg_examples_dde (@var{})
19 %# Open the DDE examples menu and allow the user to select a demo that will be evaluated.
20 %# @end deftypefn
21
22 function [] = odepkg_examples_dde ()
23
24   vode = 1; while (vode > 0)
25     clc;
26     fprintf (1, ...
27       ['DDE examples menu:\n', ...
28        '==================\n', ...
29        '\n', ...
30        '   (1) Solve a simple "exp(...)" example with solver "ode23d"\n', ...
31        '   (2) Solve an example from Wille and Baker with solver "ode45d"\n', ...
32        '   (3) Solve an example from Hu and Wang with solver "ode54d"\n', ...
33        '   (4) Solve the "infectious disease model" with solver "ode78d"\n', ...
34        '\n', ...
35        '   Note: There are further DDE examples available with the OdePkg\n', ...
36        '         testsuite functions.\n', ...
37        '\n', ...
38        '   If you have another interesting DDE example that you would like\n', ...
39        '   to share then please modify this file, create a patch and send\n', ...
40        '   your patch with your added example to the OdePkg developer team.\n', ...
41        '\n' ]);
42     vode = input ('Please choose a number from above or press <Enter> to return: ');
43     clc; if (vode > 0 && vode < 5)
44       %# We can't use the function 'demo' directly here because it does
45       %# not allow to run other functions within a demo.
46       vexa = example (mfilename (), vode);
47       disp (vexa); eval (vexa);
48       input ('Press <Enter> to continue: ');
49     end %# if (vode > 0)
50   end %# while (vode > 0)
51
52 %!demo
53 %! # Solves a simple example where the delay differential equation is
54 %! # of the form yd = e^(-lambda*t) - y(t-tau).
55 %! 
56 %! function [vyd] = fexp (vt, vy, vz, varargin)
57 %!   vlambda = varargin{1};
58 %!   vyd = exp (- vlambda * vt) - vz(1);
59 %! endfunction
60 %!
61 %! vtslot = [0, 15]; vlambda = 1; vinit = 10;
62 %! vopt = odeset ('NormControl', 'on', 'RelTol', 1e-4, 'AbsTol', 1e-4);
63 %! vsol = ode23d (@fexp, vtslot, vinit, vlambda, vinit, vopt, vlambda);
64 %! plot (vsol.x, vsol.y);
65
66 %!demo
67 %! # Solves the example 3 from the publication 'DELSOL - a numerical
68 %! # code for the solution of systems of delay-differential equations'
69 %! # from the authors David Wille and Christopher Baker.
70 %!
71 %! function [vyd] = fdelsol (vt, vy, vz, varargin)
72 %!   %# vy is a column vector of size (3,1)
73 %!   %# vz is the history of size (3,2)
74 %!   vyd = [vz(1,1); vz(1,1) + vz(2,2); vy(2,1)];
75 %! endfunction
76 %!
77 %! vopt = odeset ('NormControl', 'on', 'MaxStep', 0.1, 'InitialStep', 0.01);
78 %! vsol = ode45d (@fdelsol, [0, 5], [1, 1, 1], [1, 0.2], ones(3,2), vopt);
79 %! plot (vsol.x, vsol.y);
80
81 %!demo
82 %! # Solves the examples 2.3.1 and 2.3.2 from the book 'Dynamics of
83 %! # Controlled Mechanical Systems with Delayed Feedback' from the
84 %! # authors Haiyan Hu and Zaihua Wang.
85 %!
86 %! function [vyd] = fhuwang1 (vt, vy, vz, varargin)
87 %!   %# vy is of size (1,1), vz is of size (1,1)
88 %!   vyd = (vz(1,1) - varargin{1})^(1/3);
89 %! endfunction
90 %!
91 %! function [vyd] = fhuwang2 (vt, vy, vz, varargin)
92 %!   %# vy is of size (1,1), vz is of size (1,1)
93 %!   vyd = (vy - vz)^(1/3);
94 %! endfunction
95 %!
96 %! vtslot = [0, 10]; vK = 1; vinit = 1; vhist = 0;
97 %! vopt = odeset ('NormControl', 'on', 'RelTol', 1e-6, 'InitialStep', 0.1);
98 %!
99 %! vsol = ode54d (@fhuwang1, vtslot, vK, vinit, vhist, vopt, vK);
100 %! plot (vsol.x, vsol.y, 'ko-', 'markersize', 1); hold;
101 %!
102 %! vsol = ode54d (@fhuwang2, vtslot, vK, vinit, vhist, vopt, vK);
103 %! plot (vsol.x, vsol.y, 'bx-', 'markersize', 1);
104
105 %!demo
106 %! # Solves the infectious disease model from the book 'Solving Ordinary
107 %! # Differential Equations 1' from the authors Ernst Hairer and Gerhard
108 %! # Wanner.
109 %!
110 %! function [vyd] = finfect (vx, vy, vz, varargin)
111 %!   %# vy is of size (3,1), vz is of size (3,2)
112 %!   vyd = [ - vy(1) * vz(2,1) + vz(2,2);
113 %!            vy(1) * vz(2,1) - vy(2);
114 %!            vy(2) - vz(2,2) ];
115 %! endfunction
116 %!
117 %! function [vval, vtrm, vdir] = fevent (vx, vy, vz, varargin)
118 %!   %# vy is of size (3,1), vz is of size (3,2)
119 %!   vfec = finfect (vx, vy, vz);
120 %!   vval = vfec(2:3);  %# Have a look at component two + three
121 %!   vtrm = zeros(1,2); %# Don't stop if an event is found
122 %!   vdir = -ones(1,2); %# Check only for falling direction
123 %! endfunction
124 %!
125 %! vopt = odeset ('InitialStep', 1e-3, 'Events', @fevent);
126 %! vsol = ode78d (@finfect, [0, 40], [5, 0.1, 1], [1, 10], ...
127 %!                [5, 5; 0.1, 0.1; 1, 1], vopt);
128 %! plot (vsol.x, vsol.y, 'k-', vsol.xe, vsol.ye, 'ro');
129
130 %# Local Variables: ***
131 %# mode: octave ***
132 %# End: ***