[Python] Gauss-Legendre integration in Python →→→→→进入此内容的聊天室

来自 , 2021-03-09, 写在 Python, 查看 149 次.
URL http://www.code666.cn/view/b05bf587
  1. ''' I = gaussQuad2(f,xc,yc,m).
  2.    Gauss-Legendre integration of f(x,y) over a
  3.    quadrilateral using integration order m.
  4.    {xc},{yc} are the corner coordinates of the quadrilateral.
  5. '''
  6. from gaussNodes import *
  7. from numpy import zeros,dot
  8.  
  9. def gaussQuad2(f,x,y,m):
  10.  
  11.     def jac(x,y,s,t):
  12.         J = zeros((2,2))
  13.         J[0,0] = -(1.0 - t)*x[0] + (1.0 - t)*x[1]  \
  14.                 + (1.0 + t)*x[2] - (1.0 + t)*x[3]
  15.         J[0,1] = -(1.0 - t)*y[0] + (1.0 - t)*y[1]  \
  16.                 + (1.0 + t)*y[2] - (1.0 + t)*y[3]
  17.         J[1,0] = -(1.0 - s)*x[0] - (1.0 + s)*x[1]  \
  18.                 + (1.0 + s)*x[2] + (1.0 - s)*x[3]
  19.         J[1,1] = -(1.0 - s)*y[0] - (1.0 + s)*y[1]  \
  20.                 + (1.0 + s)*y[2] + (1.0 - s)*y[3]
  21.         return (J[0,0]*J[1,1] - J[0,1]*J[1,0])/16.0
  22.  
  23.     def map(x,y,s,t):
  24.         N = zeros(4)
  25.         N[0] = (1.0 - s)*(1.0 - t)/4.0
  26.         N[1] = (1.0 + s)*(1.0 - t)/4.0
  27.         N[2] = (1.0 + s)*(1.0 + t)/4.0
  28.         N[3] = (1.0 - s)*(1.0 + t)/4.0
  29.         xCoord = dot(N,x)
  30.         yCoord = dot(N,y)
  31.         return xCoord,yCoord
  32.  
  33.     s,A = gaussNodes(m)
  34.     sum = 0.0
  35.     for i in range(m):
  36.         for j in range(m):
  37.             xCoord,yCoord = map(x,y,s[i],s[j])
  38.             sum = sum + A[i]*A[j]*jac(x,y,s[i],s[j])  \
  39.                        *f(xCoord,yCoord)
  40.     return sum
  41. #//python/7425

回复 "Gauss-Legendre integration in Python"

这儿你可以回复上面这条便签

captcha