python⾼斯求和函数_选择积分⽅法—⾼斯积分
⽆限区间 (1)梯形法则,(2)⾟普森法则,(3)龙伯格积分法或(4)⾼斯积分法,有⼀些适⽤的指导原则。
通常,更⾼阶的⽅法对于平滑函数更好。 如果不是,那么使⽤更简单的⽅法会更好,因为数据的变化不会反映在采样点上。 梯形法则适⽤于在均匀间隔的采样点处积分来⾃实验的数据。 这对于表现不佳的函数是有好处的。 ⾟普森的规则依赖于被积函数的更⾼阶的近似,以便准确。 ⽽⾼斯积分是⾮常准确的,如果你需要均匀间隔的采样点,它不是令⼈满意的。
⾼斯积分
>>>>>>>>>>>>>>
#
# Functions to calculate integration points and weights for Gaussian
# quadrature
#
# x,w = gaussxw(N) returns integration points x and integration
# weights w such that sum_i w[i]*f(x[i]) is the Nth-order
# Gaussian approximation to the integral int_{-1}^1 f(x) dx
# x,w = gaussxwab(N,a,b) returns integration points and weights
# mapped to the interval [a,b], so that sum_i w[i]*f(x[i])
# is the Nth-order Gaussian approximation to the integral
# int_a^b f(x) dx
#
# This code finds the zeros of the nth Legendre polynomial using
# Newton's method, starting from the approximation given in Abramowitz
# and Stegun 22.16.6. The Legendre polynomial itself is evaluated
# using the recurrence relation given in Abramowitz and Stegun
# 22.7.10. The function has been checked against other sources for
# values of N up to 1000. It is compatible with version 2 and version
# 3 of Python.
#
# Written by Mark Newman , June 4, 2011
# You may use, share, or modify this file freely
#
>>>###%>>>>>>>>>>##
from numpy import ones,copy,cos,tan,pi,linspace
def gaussxw(N):
# Initial approximation to roots of the Legendre polynomial
a = linspace(3,4*N-1,N)/(4*N+2)
x = cos(pi*a+1/(8*N*N*tan(a)))
# Find roots using Newton's method epsilon = 1e-15
delta = 1.0
while delta>epsilon:
p0 = ones(N,float)
p1 = copy(x)
for k in range(1,N):
p0,p1 = p1,((2*k+1)*x*p1-k*p0)/(k+1) dp = (N+1)*(p0-x*p1)/(1-x*x)
dx = p1/dp
x -= dx
delta = max(abs(dx))
# Calculate the weights
w = 2*(N+1)*(N+1)/(N*N*(1-x*x)*dp*dp) return x,w
def gaussxwab(N,a,b):
x,w = gaussxw(N)
return 0.5*(b-a)*x+0.5*(b+a),0.5*(b-a)*w 计算实例
使⽤⾼斯积分的数值算法求解⾼斯积分
做积分变换
得到
from gaussxw import gaussxwab
from math import exp
def f(z):
return exp(-z**2/(1-z)**2)/(1-z)**2
N = 50
a = 0.0
b = 1.0
x,w = gaussxwab(N,a,b)
s = 0.0
for k in range(N):
s += w[k]*f(x[k])
print(s)
linspace函数python
the value of the actual integral is √π/2, and Gaussian quadrature is accurate to machine precision.
重写基本积分公式(6.3)通常很有⽤,这样我们就可以将加权函数W(x)与被积函数分开:
⾼斯求积⽅法, N个点和权重选择的近似误差消失如果g (x)是⼀个(2 N-1)度多项式。为了得到这个不可思议的优化,点
最终在[a, b]上有⼀个特定的分布。⼀般情况下,如果g(x)是光滑的,或者可以通过提出⼀些W(x)来使其光滑(表6.2.4),那么对于相同数量的点,⾼斯算法将⽐梯形规则和⾟普森规则产⽣更⾼的精度。有时被积函数可能不是光滑的,因为它在不同的区域有不同的⾏为。在这些情况下,分别对每个区域进⾏积分,然后将答案相加是有意义的。事实上,⼀些“智能”积分⼦例程决定使⽤多少个区间以及在每个区间中
使⽤什么规则。
表6.2.4所⽰规则均为⾼斯分布,⼀般形式为(6.32)。我们可以看到,在⼀种情况下权重函数是指数函数,在另⼀种情况下是⾼斯函数,在⼏种情况下是可积分奇点。与等间距规则不同,在区间的极值处从来不存在积分点,⽽点的值和权值随着点的个数N的变化⽽变化。虽然我们将离开⾼斯点的推导和权重数值⽅法的引⽤,我们注意,对于普通⾼斯(Gauss-Legendre)积分,然后点易变成零的勒让德多项式,权重0相关的微分,
。在数学函数库中,⽣成这些点和权重的⼦例程是标准的,可以在[A&S 72]之类的表中到,或者可以计算。我们提供的⾼斯⼦例程还将点缩放到指定区域。为了检查您的观点是否正确,您可能需要将它们与表6.1中的四点集进⾏⽐较。
映射积分点
GaussPoints.py
# GaussPoints.py: N point Gaussian quadrature pts & Wts generation
import numpy as np
def GaussPoints(Npts, a, b, x, w, eps):
m = 0; i = 0; j = 0; t = 0.; t1 = 0.; pp = 0.
p1 = 0.; p2 = 0.; p3 = 0.
m = int((Npts+1)/2)
for i in range(1, m+1):
t = np.cos(np.pi*(float(i)-0.25)/(float(Npts)+0.5))
t1 = 1
while((abs(t-t1)) >= eps):
p1 = 1. ; p2 = 0.
for j in range(1, Npts + 1):
p3 = p2; p2 = p1
p1 = ((2.*float(j)-1)*t*p2 - (float(j)-1.)*p3)/(float(j))
pp = Npts*(t*p1 - p2)/(t*t - 1.)
t1 = t
t = t1 - p1/pp
x[i-1] = -t
x[Npts-i] = t
w[i-1] = 2./( (1.-t*t)*pp*pp)
w[Npts-i] = w[i-1]
for j in range(0, Npts): # Scale [-1,+1] to [a,b]
x[j] = x[j]*(b-a)/2. + (b+a)/2.
w[j] = w[j]*(b-a)/2.
from numpy import *; from GaussPoints import GaussPoints Npts = 10; Ans = 0; a = 0.; b = 1.; eps = 3.E-14
w = zeros(2001, float); x = zeros(2001, float) # Arrays
def f(x): return exp(x) # Integrand
GaussPoints(Npts, a, b, x, w, eps) # eps: precison of pts
for i in range(0,Npts): Ans += f(x[i])*w[i] # Sum integrands print ('\n Npts =', Npts, ', Ans =', Ans)
print (' eps =',eps, ', Error =', Ans-(exp(1)-1) )

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论