forked from mikkelpm/het_agents_bayes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputeGaussLegendreQuadrature.m
36 lines (32 loc) · 1.04 KB
/
computeGaussLegendreQuadrature.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function [grid,weight] = computeGaussLegendreQuadrature(order)
% This function computes the notes and weights for the Gauss - Legendre quadrature
% of order "order."
%
% Inputs
% (1) order: order of the quadrature
%
% Outputs
% (1) grid: nodes to evaluate the function on
% (2) weights: weight in the approximation
%
% Jung Sakong, February 10, 2016
% Compute polynomial recursively
temp_poly = zeros(order+1,order+1);
temp_poly(1,1) = 1;
temp_poly(2,2) = 1;
for ii = 3:(order+1)
temp_poly(ii,:) = (2*ii-3)/(ii-1)*[0,temp_poly(ii-1,1:end-1)]...
- (ii-2)/(ii-1)*temp_poly(ii-2,:);
end
the_poly = fliplr(temp_poly(end,:)); % higher order coefficients first
% Solve for roots of the polynomial
grid = roots(the_poly);
% Compute weights
temp_powers = zeros(order,order);
for ii = 1:order
temp_powers(:,ii) = (order+1-ii)*grid.^(order-ii);
end
% poly_prime = repmat(the_poly(1:end-1),[order 1]) ...
% .* temp_powers;
poly_prime = the_poly(1:end-1).* temp_powers;
weight = 2 ./ ((1-grid.^2).*(sum(poly_prime,2)).^2);