Example #1
0
def gauss_laguerre(f, a, b, n):
    if a != 0 and b != float('int'):
        raise ValueError('Gauss-Laguerre Quadrature integrates over [a,0).')

    w = lambda x: math.exp(x)  # inverse weighting function
    [roots,weights] = sp.l_roots(n,0)
    return sum([weights[i]*w(roots[i])*f(roots[i]) for i in range(n)])
def GaussianQuadrature():

    ########## PART A ##########
    # Array to carry integrands
    n = 30
    intArr = np.zeros(n,)

    # Loop over all numbers of nodes 
    intFact = FD_factor(20.)	# kT = 20MeV
    for i in range(1,n+1):
	# Find roots and approximate integral
	[xi,wi] = sp.l_roots(i)
	intArr[i-1] = intFact*np.sum(wi*fermi_dirac_mod(xi))	

    # Save integral array to file
    np.savetxt('Q2_numdensity.dat',intArr,fmt='%10.10e')
    ############################

    ########## PART B ##########
    # nInt-1 intervals of width dE = 5MeV 
    # in range [0,150] MeV
    nInt = 30
    dE = 5.
    dndE = np.zeros(nInt,)

    # Find roots
    [xi,wi] = sp.p_roots(n)

    # Transformations for xi,wi in each interval
    xTemp = np.linspace(0.,150.,num=nInt+1)
    bMa = 2.5					# b-a/2
    bPa = 0.5*(xTemp[1:] + xTemp[:-1])		# b+a/2
 
    # Loop over all intervals
    for i in range(0,nInt):
	# Transform variables
	w = wi*bMa
	x = bMa*xi + bPa[i]
	# Evaluate intgral and differential
        dndE[i] = (intFact/dE)*np.sum(w*fermi_dirac(x))
   
    # Save spectral information to file
    outArr1 = np.zeros((nInt,2.),float)
    outArr1[:,0] = bPa
    outArr1[:,1] = dndE
    np.savetxt('Q2_energyspectrum.dat',outArr1,fmt='%10.10e')
	
    # Verify method and save to file
    outArr2 = [intArr[-1],np.sum(dndE*dE)]
    np.savetxt('Q2_verifymethod.dat',outArr2,fmt='%10.10e')
    ############################

    return
Example #3
0
def gaussLaguerre(g, n):
    f = lambda x: eval(g)    # define function to evaluate

    # get abscissas (xi), and weights (wi) from l_roots(n) function below
    # scipy.special.l_roots(n) returns abscissas & weights of Laguerre polynomials
    abscissa, weight = l_roots(n)
    intSum = 0

    # Gauss-Laguerre method is valid  over interval [0,infty]
    for i in range(n):
        # evaluate integral
        intSum = intSum + weight[i]*f(abscissa[i])*np.exp(abscissa[i])

    return intSum
Example #4
0
def quest2a():
    "Routine for answering question 2a"
    # setting kT = 20 MeV
    nodes = range(1,11)
    
    F = 8*pi*np.power(20e6 * const.EV / (const.C*const.H),3)

    integral = np.array([])

    for k in nodes:
        [roots,weights] = spy.l_roots(k,0)  #pulls out 
        integral = np.append(integral,sum(weights*funca(roots)))
    
    print("Integral Node Convergence: {0}".format(integral))
    print "NumberDensity: {0}".format(F*integral[-1])
    return F*integral[-1]
Example #5
0
def twoa(n):
	kT=3.2*10**(-5)#cgs
	hc=3.16*10**(-17)#cgs
#use Python's built-in Gaussian-Laguerre roots and weights
	[la_r,la_w]=sp.l_roots(n)
	f=lambda x:x**2*np.exp(x)/(1.+np.exp(x))#f(x)
	Q=np.zeros(len(la_r))	
#calculate each Qi
	for i in range(len(Q)):
		Q[i]=la_w[i]*f(la_r[i])
#and the sum
	Qtot=np.sum(Q)
#then put the constants back in
	ne=(Qtot*8.*np.pi*(kT)**3)/(2.*np.pi*hc)**3
	print "The integral is %.11f"%Qtot
	print "ne is %g cm^-3"%ne
Example #6
0
def gauss_laguerre(f,n):
    [laguerre_roots, laguerre_weights] = sp.l_roots(n,0)
    return sum( f(laguerre_roots) * laguerre_weights )
Example #7
0
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 17 11:44:09 2014

@author: davidvartanyan
"""

from numpy import *
from scipy import special as sp

[laguerre_roots,laguerre_weights]=sp.l_roots(2,0)

#from Wolfram Alpha
k=20 #in MeV
h=1.97327*10**-11 #in Mev cm

def f(x):
    return x**2*exp(x)/(exp(x)+1)

print sum(laguerre_weights*f(laguerre_roots))
Example #8
0
import matplotlib.pyplot as pl
import plot_defaults

# Part A

# parameters
max_exponent = 8

number_density_coeff = 1.05495e35 # cm^(-3)

ns = [2**p for p in range(1,max_exponent)]

def f(x):
    return x * x * np.exp(x) / (np.exp(x) + 1)

[xs, ws] = sp.l_roots(ns[-1], 0)

Qs = np.array([np.sum(ws[:n] * f(xs)[:n]) for n in ns])
number_densities = number_density_coeff * Qs

print("\nPart A\n")
print("Number of nodes:\n{}".format(ns))
print("Number density [cm^(-3)]:\n{}".format(number_densities))
print("Change in number density [cm^(-3)]:\n{}".format(number_densities[1:]
                                             - number_densities[:-1]))

# Answer: 1.902*10^35 cm^(-3)

# Part B

# parameters
Example #9
0
from __future__ import division
from pylab import *
from scipy import special

# constants in cgs units
c    = 2.998e10     # speed of light
kB   = 1.38065e-16  # Boltzmann constant
hbar = 1.054572e-27 # reduced Planck constant

MeV_to_ergs = 1.602177e-6 # factor to convert units of energy from MeV to ergs
T = 20*MeV_to_ergs/kB     # temperature (kT = 20MeV)
integrand = lambda x: x**2/(exp(x)+1)

# (a) Use Gauss-Laguerre Quadrature to determine the total
#     number density of electrons
[laguerre_roots,laguerre_weights] = special.l_roots(20)
weight   = lambda x: exp(-x)
integral = sum(laguerre_weights*integrand(laguerre_roots)/weight(laguerre_roots))

n_electrons = 8*pi*(kB*T)**3/(2*pi*hbar*c)**3 * integral
print 'The number density of electrons is %.4e/cm^3' % n_electrons

# (b) Use Gauss-Legendre Quadrature to determine the spectral
#     distribution of the electrons
[legendre_roots,legendre_weights] = special.p_roots(20)

dE = 5; Emax = 200
E_bins = linspace(0,Emax,Emax/dE+1) # MeV
x_bins = E_bins*MeV_to_ergs/(kB*T)
n_bins = zeros(len(E_bins)-1) # number density of electrons in each energy bin
Example #10
0
def lag(n):
	[lagroots, lagweights] = sp.l_roots(n,0)
	integ = 0.0
	for j in range(0,n):
		integ +=lagweights[j]*g(lagroots[j])
	return integ
Example #11
0
def integrate_laguerre(f, args=(), n=6):
    x, c = l_roots(n)
    I = sum(np.exp(x) * f(x, *args) * c)

    return I
Example #12
0
def GaussLaguerre(func, n):
    [laguerre_roots, laguerre_weights] = sp.l_roots(n, 0.)
    return np.sum(laguerre_weights*func(laguerre_roots))
Example #13
0
File: p2a.py Project: xchma/ay190
def laguerre(n,f):
    roots,weights=sp.l_roots(n);
    return np.sum(weights*f(roots))
Example #14
0
def gla(n):
    [lag_roots, lag_weights] = sp.l_roots(n,0)
    func = map(f, lag_roots)
    prod = np.dot(lag_weights, func)
    return prod