/tags/cvs_final/octave-forge/main/odepkg/inst/odepkg_equations_vanderpol.m

# · MATLAB · 112 lines · 25 code · 14 blank · 73 comment · 6 complexity · cad2faa942c0cb78701feb51a321fda3 MD5 · raw file

  1. %# Copyright (C) 2006, Thomas Treichl <treichl@users.sourceforge.net>
  2. %# OdePkg - Package for solving ordinary differential equations with octave
  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, write to the Free Software
  16. %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. %# -*- texinfo -*-
  18. %# @deftypefn {Function} odepkg_equations_vanderpol ()
  19. %# Displays the help text of the function and terminates with an error.
  20. %#
  21. %# @deftypefnx {Function} {@var{ydot} =} odepkg_equations_vanderpol (@var{t, y})
  22. %# Returns two derivatives of the ordinary differential equations (ODEs) from the "Van der Pol" implementation, cf. @url{http://en.wikipedia.org/wiki/Van_der_Pol_oscillator} for further details. The output argument @var{ydot} is a column vector and contains the derivatives, @var{y} also is a column vector that contains the integration results from the previous integration step and @var{t} is a scalar value with actual time stamp. There is a error handling implemented in this function, ie. if an unvalid input argument is found then this function terminates with an error.
  23. %#
  24. %# Run
  25. %# @example
  26. %# demo odepkg_equations_vanderpol
  27. %# @end example
  28. %# to see an example.
  29. %# @end deftypefn
  30. %#
  31. %# @seealso{odepkg}
  32. %# Maintainer: Thomas Treichl
  33. %# Created: 20060809
  34. %# ChangeLog:
  35. function ydot = odepkg_equations_vanderpol (tvar, yvar, varargin)
  36. %# odepkg_equations_vanderpol is a demo function. Therefore some
  37. %# error handling is done. If you would write your own function you
  38. %# would not add any error handling to achieve highest performance.
  39. if (nargin == 0)
  40. help ('odepkg_equations_vanderpol');
  41. vmsg = sprintf ('Number of input arguments must be greater than zero');
  42. error (vmsg);
  43. elseif (nargin < 2 || nargin > 3)
  44. vmsg = sprintf ('Number of input arguments must be greater 1 and lower 4');
  45. error (vmsg);
  46. elseif (isnumeric (tvar) == false || isnumeric (yvar) == false)
  47. vmsg = sprintf ('First and second input argument must be valid numeric values');
  48. error (vmsg);
  49. elseif (isvector (yvar) == false || length (yvar) ~= 2)
  50. vmsg = sprintf ('Second input argument must be a vector of length 2');
  51. error (vmsg);
  52. end
  53. if (length (varargin) == 1) %# Check if parameter mu is given
  54. if (isscalar (varargin{1}) == true)
  55. mu = varargin{1};
  56. else
  57. vmsg = sprintf ('Third input argument must be a valid scalar');
  58. error (vmsg);
  59. end
  60. else, mu = 1; end
  61. %# yvar and ydot must be column vectors
  62. ydot = [yvar(2); ...
  63. mu * (1 - yvar(1)^2) * yvar(2) - yvar(1)];
  64. %#! A stiff ode euqation test preocedure is
  65. %#! A = odeset ('RelTol', 1e-1, 'AbsTol', 1, 'InitialStep', 1e-2, ...
  66. %#! 'NormControl', 'on', 'Stats', 'on', 'OutputFcn', @odeprint);
  67. %#! [x, y] = ode78 (@odepkg_equations_vanderpol, [0 300], [2 0], A, 100);
  68. %!test
  69. %! warning ("off", "OdePkg:InvalidOption");
  70. %! [vt, vy] = ode78 (@odepkg_equations_vanderpol, [0 1], [2 0]);
  71. %!demo
  72. %!
  73. %! [vt, vy] = ode78 (@odepkg_equations_vanderpol, [0 20], [2 0]);
  74. %!
  75. %! axis ([0, 20]);
  76. %! plot (vt, vy(:,1), '-or;x1(t);', vt, vy(:,2), '-ob;x2(t);');
  77. %!
  78. %! % ---------------------------------------------------------------------------
  79. %! % The figure window shows the two states of the "Van Der Pol" implementation
  80. %! % example. The math equation for this differential equation is
  81. %! %
  82. %! % d^2y/dt^2 - mu*(1-y^2)*dy/dt + y = 0.
  83. %! %
  84. %! % If not manually parametrized then mu = 1.
  85. %!demo
  86. %!
  87. %! A = odeset ('NormControl', 'on', 'InitialStep', 1e-3);
  88. %! [vt, vy] = ode78 (@odepkg_equations_vanderpol, [0 40], [2 0], A, 20);
  89. %!
  90. %! axis ([0, 40]);
  91. %! plot (vt, vy(:,1), '-or;x1(t);', vt, vy(:,2), '-ob;x2(t);');
  92. %!
  93. %! % ---------------------------------------------------------------------------
  94. %! % The figure window shows the two integrated states of the "Van Der Pol"
  95. %! % implementation. The parameter mu = 20 was set manually for this demo.
  96. %# Local Variables: ***
  97. %# mode: octave ***
  98. %# End: ***