From f7b6e917acb30e2bb14901e195396bd44c1db41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Mon, 12 Dec 2011 19:05:25 +0100 Subject: [PATCH] Added two routines for evaluating multivariate integrals with gaussian quadratures. --- matlab/cartesian_product_of_sets.m | 56 ++++++++++++++++++++++ matlab/gauss_hermite_weights_and_nodes.m | 59 ++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 matlab/cartesian_product_of_sets.m create mode 100644 matlab/gauss_hermite_weights_and_nodes.m diff --git a/matlab/cartesian_product_of_sets.m b/matlab/cartesian_product_of_sets.m new file mode 100644 index 000000000..1368f8e28 --- /dev/null +++ b/matlab/cartesian_product_of_sets.m @@ -0,0 +1,56 @@ +function cprod = cartesian_product_of_sets(varargin) +% Computes the cartesian product. + +%@info: +%! @deftypefn {Function File} {@var{cprod} =} cartesian_product_of_sets (@var{a},@var{b}, ...) +%! @anchor{cartesian_product_of_sets} +%! @sp 1 +%! Computes A_1 * A_2 * .... * A_n with a generic set A_i = {e_1,e_2,e_3,...} where e_i is a string +%! or a number. It is assumed that each element e_i is unique in set A_i. +%! @sp 2 +%! @strong{Inputs} +%! @sp 1 +%! @table @ @var +%! @item a +%! n*1 vector of doubles. +%! @item a +%! m*1 vector of doubles. +%! @end table +%! @sp 1 +%! @strong{Outputs} +%! @sp 1 +%! @table @ @var +%! @item cprod +%! nm*2 matrix of doubles, the nodes cartesian product of sets @var{a} and @var{b}. +%! @end table +%! @sp 2 +%! @strong{This function is called by:} +%! @sp 2 +%! @strong{This function calls:} +%! @sp 2 +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 Dynare Team +% stephane DOT adjemian AT univ DASH lemans DOT fr +% +% 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 . + +[ F{1:nargin} ] = ndgrid( varargin{:} ); + +for i=1:nargin + cprod(:,i) = F{i}(:); +end \ No newline at end of file diff --git a/matlab/gauss_hermite_weights_and_nodes.m b/matlab/gauss_hermite_weights_and_nodes.m new file mode 100644 index 000000000..d035f520c --- /dev/null +++ b/matlab/gauss_hermite_weights_and_nodes.m @@ -0,0 +1,59 @@ +function [nodes,weights] = gauss_hermite_weights_and_nodes(n) +% Computes the weights and nodes for an Hermite Gaussian quadrature rule. + +%@info: +%! @deftypefn {Function File} {@var{nodes}, @var{weights} =} gauss_hermite_weights_and_nodes (@var{n}) +%! @anchor{gauss_hermite_weights_and_nodes} +%! @sp 1 +%! Computes the weights and nodes for an Hermite Gaussian quadrature rule. designed to approximate integrals +%! on the infinite interval (-\infty,\infty) of an unweighted smooth function. +%! @sp 2 +%! @strong{Inputs} +%! @sp 1 +%! @table @ @var +%! @item n +%! Positive integer scalar, number of nodes (order of approximation). +%! @end table +%! @sp 1 +%! @strong{Outputs} +%! @sp 1 +%! @table @ @var +%! @item nodes +%! n*1 vector of doubles, the nodes (roots of an order n Hermite polynomial) +%! @item weights +%! n*1 vector of doubles, the associated weights. +%! @end table +%! @sp 2 +%! @strong{This function is called by:} +%! @sp 2 +%! @strong{This function calls:} +%! @sp 2 +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 Dynare Team +% stephane DOT adjemian AT univ DASH lemans DOT fr +% +% 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 . + +b = sqrt([1:n-1]/2); +JacobiMatrix = diag(b,1)+diag(b,-1); +[JacobiEigenVectors,JacobiEigenValues] = eig(JacobiMatrix); +[nodes,idx] = sort(diag(JacobiEigenValues)); +JacobiEigenVector = JacobiEigenVectors(1,:); +JacobiEigenVector = transpose(JacobiEigenVector(idx)); +weights = JacobiEigenVector.^2; +nodes = sqrt(2)*nodes; \ No newline at end of file