eharley0142

fdjac__.m

May 10th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.79 KB | None | 0 0
  1. % Copyright (C) 2008, 2009 Jaroslav Hajek
  2. %
  3. % This file is part of Octave.
  4. %
  5. % Octave is free software; you can redistribute it and/or modify it
  6. % under the terms of the GNU General Public License as published by
  7. % the Free Software Foundation; either version 3 of the License, or (at
  8. % your option) any later version.
  9. %
  10. % Octave is distributed in the hope that it will be useful, but
  11. % WITHOUT ANY WARRANTY; without even the implied warranty of
  12. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. % General Public License for more details.
  14. %
  15. % You should have received a copy of the GNU General Public License
  16. % along with Octave; see the file COPYING.  If not, see
  17. % <http://www.gnu.org/licenses/>.
  18.  
  19. % -*- texinfo -*-
  20. % @deftypefn{Function File} {} __fdjac__ (@var{fcn}, @var{x}, @var{fvec}, @var{err})
  21. % Undocumented internal function.
  22. % @end deftypefn
  23.  
  24. % function fjac = fdjac__ (fcn, x, fvec, cdif, err = 0)
  25. function fjac = fdjac__ (fcn, x, fvec, cdif, err)
  26.     err = 0;
  27.   if (cdif)
  28.     err = (max (eps, err)) ^ (1/3);
  29.     h = max (abs (x), 1)*err; % FIXME? (NOT MINE)
  30.     fjac = zeros (length (fvec), numel (x));
  31.     for i = 1:numel (x)
  32.       x1 = x;
  33.       x2 = x;
  34.       x1(i) = x1(i) + h(i);
  35.       x2(i) = x2(i) - h(i);
  36.  
  37. %      fjac(:,i) = (fcn (x1)(:) - fcn (x2)(:)) / (x1(i) - x2(i));
  38.       fcnx1 = fcn (x1);
  39.       fcnx2 = fcn (x2);
  40.       fjac(:,i) = (fcnx1(:) - fcnx2(:)) / (x1(i) - x2(i));
  41.  
  42.     end
  43.   else
  44.     err = sqrt (max (eps, err));
  45.     h = max (abs (x), 1)*err; % FIXME? (NOT MINE)
  46.     fjac = zeros (length (fvec), numel (x));
  47.     for i = 1:numel (x)
  48.       x1 = x;
  49.       x1(i) = x1(i) + h(i);
  50. %      fjac(:,i) = (fcn (x1)(:) - fvec) / (x1(i) - x(i));
  51.       fcnx1 = fcn (x1);
  52.       fjac(:,i) = (fcnx1(:) - fvec) / (x1(i) - x(i));
  53.     end
  54.   end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment