Home > matlab > dynare_gradient.m

dynare_gradient

PURPOSE ^

Computes the gradient of a function from R^m in R^n.

SYNOPSIS ^

function [F,G] = dynare_gradient(fcn,x,epsilon,varargin)

DESCRIPTION ^

 Computes the gradient of a function from R^m in R^n.

 INPUTS:
  fcn      [string]  name of the matlab's function.
  x        [double]  m*1 vector (where the gradient is evaluated).
  epsilon  [double]  scalar or m*1 vector of steps. 

 OUTPUTS: 
  F        [double]  n*1 vector, evaluation of the function at x.
  G        [double]  n*m matrix, evaluation of the gradient at x.

 OUTPUTS
 
 Copyright (C) 2010-2011 Dynare Team

 This file is part of Dynare.

 Dynare is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Dynare is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [F,G] = dynare_gradient(fcn,x,epsilon,varargin)
0002 % Computes the gradient of a function from R^m in R^n.
0003 %
0004 % INPUTS:
0005 %  fcn      [string]  name of the matlab's function.
0006 %  x        [double]  m*1 vector (where the gradient is evaluated).
0007 %  epsilon  [double]  scalar or m*1 vector of steps.
0008 %
0009 % OUTPUTS:
0010 %  F        [double]  n*1 vector, evaluation of the function at x.
0011 %  G        [double]  n*m matrix, evaluation of the gradient at x.
0012 %
0013 % OUTPUTS
0014 %
0015 % Copyright (C) 2010-2011 Dynare Team
0016 %
0017 % This file is part of Dynare.
0018 %
0019 % Dynare is free software: you can redistribute it and/or modify
0020 % it under the terms of the GNU General Public License as published by
0021 % the Free Software Foundation, either version 3 of the License, or
0022 % (at your option) any later version.
0023 %
0024 % Dynare is distributed in the hope that it will be useful,
0025 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0026 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0027 % GNU General Public License for more details.
0028 %
0029 % You should have received a copy of the GNU General Public License
0030 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0031 
0032 % Evaluate the function at x.
0033 F = feval(fcn, x, varargin{:});
0034 
0035 % (G)Set dimensions.
0036 m = length(x);
0037 n = length(F);
0038 
0039 % Initialization of the gradient.
0040 G = NaN(length(F),length(x));
0041 
0042 if length(epsilon==1)
0043     H = epsilon*eye(m);
0044 else
0045     H = diag(epsilon);
0046 end
0047 
0048 % Compute the gradient.
0049 for i=1:m
0050     if size(x,1)>size(x,2)
0051         h = H(i,:);
0052     else
0053         h = H(:,i);
0054     end   
0055     [Fh,junk1,junk2,flag] = feval(fcn, x+transpose(h), varargin{:});
0056     if flag
0057         G(:,i) = (Fh-F)/epsilon;
0058     else
0059         [Fh,junk1,junk2,flag] = feval(fcn, x-transpose(h), varargin{:});
0060         if flag
0061             G(:,i) = (F-Fh)/epsilon;
0062         else
0063             error('-- Bad gradient --')
0064         end
0065     end
0066 end

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005