/tags/R2007-03-28/octave-forge/main/odepkg/inst/odepkg_equations_secondorderlag.m

# · MATLAB · 116 lines · 29 code · 13 blank · 74 comment · 6 complexity · ac80ea5aaf198621702387ce687d7da2 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} {@var{ydot} =} odepkg_equations_secondorderlag (@var{t, y, [u, K, T1, T2]})
  19. %# Returns two derivatives of the ordinary differential equations (ODEs) from the second order lag implementation (control theory), cf. @url{http://en.wikipedia.org/wiki/Category:Control_theory} 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 no error handling implemented in this function to achieve the highest performance available.
  20. %#
  21. %# Run
  22. %# @example
  23. %# demo odepkg_equations_secondorderlag
  24. %# @end example
  25. %# to see an example.
  26. %# @end deftypefn
  27. %#
  28. %# @seealso{odepkg}
  29. %# Maintainer: Thomas Treichl
  30. %# Created: 20060809
  31. %# ChangeLog:
  32. function ydot = odepkg_equations_secondorderlag (tvar, yvar, varargin)
  33. %# odepkg_equations_vanderpol is a demo function. Therefore some
  34. %# error handling is done. If you would write your own function you
  35. %# would not add any error handling to achieve highest performance.
  36. if (nargin == 0)
  37. help ('odepkg_equations_secondorderlag');
  38. vmsg = sprintf ('Number of input arguments must be greater than zero');
  39. error (vmsg);
  40. elseif (nargin <= 1)
  41. vmsg = sprintf ('Number of input arguments must be greater or equal than 2');
  42. error (vmsg);
  43. elseif (isnumeric (tvar) == false || isnumeric (yvar) == false)
  44. vmsg = sprintf ('First and second input argument must be valid numeric values');
  45. error (vmsg);
  46. elseif (isvector (yvar) == false || length (yvar) ~= 2)
  47. vmsg = sprintf ('Second input argument must be a vector of length 2');
  48. error (vmsg);
  49. end
  50. %# yvar and ydot must be column vectors
  51. %# Check if varargin arguments are given
  52. if (length (varargin) > 0)
  53. if (length (varargin) ~= 4)
  54. vmsg = sprintf ('If parametrizing arguments are given then number of these arguments must match 4');
  55. error (vmsg);
  56. end
  57. vu = varargin{1}; %# vu is the input signal of the control system
  58. vK = varargin{2}; %# vK is the amplification of the control system
  59. vT1 = varargin{3}; %# vT1 is the smaller time constant that is indominant
  60. vT2 = varargin{4}; %# vT2 is the greater time constant that is dominant
  61. else
  62. vu = 10; vK = 1; vT1 = 0.01; vT2 = 0.1;
  63. end
  64. ydot(1,1) = yvar(2,1);
  65. ydot(2,1) = 1/vT1 * (- yvar(1,1) - vT2*yvar(2,1) + vK*vu);
  66. %!test [vt, vy] = ode45 (@odepkg_equations_secondorderlag, [0 1], [0 0]);
  67. %!test [vt, vy] = ode45 (@odepkg_equations_secondorderlag, [0 1], [3 2]);
  68. %!demo
  69. %!
  70. %! A = odeset ('RelTol', 1e-3);
  71. %! [vt, vy] = ode45 (@odepkg_equations_secondorderlag, [0 1.4], [0 0], A);
  72. %!
  73. %! axis ([0 1.4]);
  74. %! plot(vt, 10 * ones (length (vy(:,1)),1), "-or;in (t);", ...
  75. %! vt, vy(:,1), "-ob;out (t);");
  76. %!
  77. %! % ---------------------------------------------------------------------------
  78. %! % The figure window shows the input signal and the output signal of the
  79. %! % "Second Order Lag" implementation. The mathematical describtion for this
  80. %! % differential equation is
  81. %! %
  82. %! % y + T2 * dy/dt + T1 * d^2y/dt^2 = K * u.
  83. %! %
  84. %! % If not manually parametrized then u = 10, K = 1, T1 = 0.01s and T2 = 0.1s^2
  85. %! % and then accumulated value (t->inf) is y = K * u = 10.
  86. %!demo
  87. %!
  88. %! A = odeset ('RelTol', 2e-2, 'NormControl', 'on');
  89. %! [vt, vy] = ode45 (@odepkg_equations_secondorderlag, ...
  90. %! [0 12], [0 0], A, 5, 1, 0.01, 0.01);
  91. %!
  92. %! axis ([0 12]);
  93. %! plot(vt, 5 * ones (length (vy(:,1)),1), "-or;in (t);", ...
  94. %! vt, vy(:,1), "-ob;out (t);");
  95. %!
  96. %! % ---------------------------------------------------------------------------
  97. %! % The figure window shows the input signal and the output signal of a
  98. %! % parametrized "Second Order Lag" implementation. The parameters for this
  99. %! % example are u = 5, K = 1, T1 = 0.01, T2 = 0.01. The accumulated value
  100. %! % (t->inf) is y = K * u = 5.
  101. %# Local Variables: ***
  102. %# mode: octave ***
  103. %# End: ***