R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > # qgaustb.r October 2007 > # > # find abscissas and weights for gauss quadrature > # > # define function > qgaus <-function(n,type=3,alph=0) { + # default=Hermite, alph parameter in Laguerre + nm1 <- 1:(n-1) # vector one smaller + # main branch + if( type == 1 ) { # Legendre + alpha <- nm1/sqrt(4*nm1*nm1-1) + beta <- rep(0,n) } + if( type == 2 ) { # shifted Legendre + alpha <- (nm1/2)/sqrt(4*nm1*nm1-1) + beta <- rep(1/2,n) } + if( type == 3 ) { # Hermite + alpha <- sqrt(nm1/2) + beta <- rep(0,n) } + if( type == 4 ) { # modified Hermite + alpha <- sqrt(nm1) + beta <- rep(0,n) } + if( type == 5 ) { # Laguerre, alph = parameter + alpha <- sqrt(nm1*(nm1+alph)) + beta <- 2*(1:n)+alph-1 } + # make matrix + A <- rbind(cbind(rep(0,n-1),diag(alpha,n-1)),rep(0,n)) + A <- A + t(A) + diag(beta,n) + e <- eigen(A,symmetric=T) # eigenvalues and vectors + abscissas <- e$values + weights <- (e$vectors[1,])*(e$vectors[1,]) + qgaus <- list(abscissas,weights) + } # end of function qgaus > # call it to check it out > q1 <- qgaus(5,1) # Legendre > q1 [[1]] [1] 0.9061798 0.5384693 0.0000000 -0.5384693 -0.9061798 [[2]] [1] 0.1184634 0.2393143 0.2844444 0.2393143 0.1184634 > q2 <- qgaus(5,2) # shifted Legendre > q2 [[1]] [1] 0.95308992 0.76923466 0.50000000 0.23076534 0.04691008 [[2]] [1] 0.1184634 0.2393143 0.2844444 0.2393143 0.1184634 > sum(q2[[2]]) [1] 1 > q3 <- qgaus(5,3) # Hermite > q3 [[1]] [1] 2.020183e+00 9.585725e-01 -4.440892e-16 -9.585725e-01 -2.020183e+00 [[2]] [1] 0.01125741 0.22207592 0.53333333 0.22207592 0.01125741 > q4 <- qgaus(5,4) # modified Hermite > q4 [[1]] [1] 2.856970e+00 1.355626e+00 -4.440892e-16 -1.355626e+00 -2.856970e+00 [[2]] [1] 0.01125741 0.22207592 0.53333333 0.22207592 0.01125741 > sum(q4[[2]]) [1] 1 > q5 <-qgaus(5,5,2) # Laguerre with parameter 2 > q5 [[1]] [1] 15.828474 9.682910 5.620294 2.837213 1.031109 [[2]] [1] 0.0001313564 0.0142821168 0.1917748618 0.5333529666 0.2604586984 > # done > rm(list=ls()) > q()