Hướng dẫn gauss-legendre quadrature c++ - phép cầu phương gauss-legendre trong C++

Tôi đang cố gắng giải quyết các tích phân kép thông qua phương thức số quadratre của Gauss, Legendre trong Python mà không sử dụng bất kỳ thư viện nào có các phương thức số. Nhưng tôi không thể làm cho thuật toán hoạt động khi tôi có chức năng là giới hạn của tích hợp. Đây là những gì tôi đã thử:

def integrate(a: float, b: float, n: int, f_xy: callable, upper_func: callable, lower_func: callable) -> float:

    if n < 1:
        raise("n < 1 is invalid.")

    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2
    sum = 0

    for i in range(n):

        x_i = e1_x*t[i]+e2_x

        if type(upper_func) == int:
            d = upper_func
        else:
            d = upper_func(x_i)

        if type(lower_func) == int:
            c = lower_func
        else:
            c = lower_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        som = 0
        for j in range(n):
            y_i = e1_y*t[j]+e2_y
            som += w[j]*f_xy(x_i, y_i)

        sum += w[i]*som

    result = (1/4)*(b-a)*(d-c)*sum

    return result

trong đó hàm

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
2 trả về các trọng số và nút.

[Edit]

Tôi quản lý để làm điều đó. Nó chỉ cần một vài điều chỉnh.

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result

Dưới đây là 8 kho lưu trữ công cộng phù hợp với chủ đề này ...

  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • C++
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature
  • Mã số
  • Vấn đề
  • Kéo yêu cầu

Đền Gauss-Legendre

  • Cập nhật ngày 16 tháng 3 năm 2016
  • Các mô -đun Fortran hiện đại để tạo ra các quy tắc Quadrature

Cập nhật ngày 3 tháng 12 năm 2018

FORTRANgauss-legendre topic page so that developers can more easily learn about it. gauss-legendre topic page so that developers can more easily learn about it.

Thư viện toán học thu thập các thuật toán quadratre phổ biến được triển khai trong MATLAB để xấp xỉ nhanh và chính xác của các tích phân xác định.

Cập nhật ngày 30 tháng 7 năm 2021

Matlabgauss-legendre topic, visit your repo's landing page and select "manage topics." gauss-legendre topic, visit your repo's landing page and select "manage topics."

Đây là những dự án tôi đã thực hiện cho một khóa học phân tích số với bạn bè của tôi.

scipy.special.roots_legendre (n, mu = false) [nguồn]#roots_legendre(n, mu=False)[source]#roots_legendre(n, mu=False)[source]#

Đền Gauss-Legendre.

Tính toán các điểm và trọng số mẫu cho Quad Gauss-Legendre [GL]. Các điểm mẫu là gốc của đa thức Legendre Legendre \ (P_N (x) \). Các điểm và trọng số mẫu này tích hợp chính xác các đa thức của mức độ \ (2n - 1 \) hoặc ít hơn trong khoảng \ ([ - 1, 1] \) với hàm trọng lượng \ (w (x) = 1 \). Xem 2.2.10 trong [AS] để biết thêm chi tiết.\(P_n(x)\). These sample points and weights correctly integrate polynomials of degree \(2n - 1\) or less over the interval \([-1, 1]\) with weight function \(w(x) = 1\). See 2.2.10 in [AS] for more details.\(P_n(x)\). These sample points and weights correctly integrate polynomials of degree \(2n - 1\) or less over the interval \([-1, 1]\) with weight function \(w(x) = 1\). See 2.2.10 in [AS] for more details.

Tham sốnintnint

thứ tự bậc hai

Mubool, tùy chọnbool, optionalbool, optional

Nếu đúng, hãy trả lại tổng của các trọng số, tùy chọn.

Returnsxndarrayxndarrayxndarray

Điểm lấy mẫu

wndarrayndarrayndarray

Trọng lượng

Mufloatfloatfloat

Tổng trọng lượng

Người giới thiệu

BẰNG

Milton Abramowitz và Irene A. Stegun, biên tập. Cẩm nang về các chức năng toán học với các công thức, đồ thị và bảng toán học. New York: Dover, 1972.

GL(1,2)(1,2)(1,2)

Gauss-Legendre Quadrature, Wikipedia, https://en.wikipedia.org/wiki/gauss%E2%80%93legendre_quadrature

Ví dụ

>>> from scipy.special import roots_legendre, eval_legendre
>>> roots, weights = roots_legendre(9)
def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
3 giữ rễ và
def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
4 giữ các trọng số cho phương trình Gauss-Legendre.
>>> roots
array([-0.96816024, -0.83603111, -0.61337143, -0.32425342,  0.        ,
        0.32425342,  0.61337143,  0.83603111,  0.96816024])
>>> weights
array([0.08127439, 0.18064816, 0.2606107 , 0.31234708, 0.33023936,
       0.31234708, 0.2606107 , 0.18064816, 0.08127439])

Xác minh rằng chúng tôi có gốc bằng cách đánh giá đa thức về Legendre Legendre ở mức độ 9 tại

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
3. Tất cả các giá trị xấp xỉ bằng 0:
>>> eval_legendre(9, roots)
array([-8.88178420e-16, -2.22044605e-16,  1.11022302e-16,  1.11022302e-16,
        0.00000000e+00, -5.55111512e-17, -1.94289029e-16,  1.38777878e-16,
       -8.32667268e-17])

Ở đây, chúng tôi sẽ chỉ ra làm thế nào các giá trị trên có thể được sử dụng để ước tính tích phân từ 1 đến 2 của F (t) = t + 1/t với cầu phương Gauss-Legendre [GL]. Đầu tiên xác định chức năng và giới hạn tích hợp.

>>> def f(t):
...    return t + 1/t
...
>>> a = 1
>>> b = 2

Chúng tôi sẽ sử dụng

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
6 để biểu thị tích phân xác định của f từ t = a đến t = b. Các điểm mẫu trong
def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
3 là từ khoảng [-1, 1], vì vậy chúng tôi sẽ viết lại tích phân với sự thay đổi đơn giản của biến:
def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
2

với nghịch đảo:

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
3

Then:

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
4

Chúng ta có thể xấp xỉ tích phân sau với các giá trị được trả về bởi

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
8.

Ánh xạ các gốc được tính toán ở trên từ [-1, 1] đến [a, b].

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
6

Xấp xỉ tích phân là tổng trọng số của các giá trị hàm.

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
0

So sánh với kết quả chính xác, đó là 3/2 + log (2):

def double_integrate(a: float, b: float, n: int, f_xy: Union[Callable, float],
                     up_func: Union[Callable, float], low_func: Union[Callable, float]) -> float:
     
    w, t = get_wt(n)

    e1_x = (b-a)/2
    e2_x = (a+b)/2

    sum = 0

    for i in range(n):

        som = 0

        x_i = e1_x*t[i]+e2_x

        if not isfunction(up_func):
            d = up_func
        else:
            d = up_func(x_i)

        if not isfunction(low_func):
            c = low_func
        else:
            c = low_func(x_i)

        e1_y = (d - c)/2
        e2_y = (c + d)/2

        for j in range(n):
            y_i = e1_y*t[j]+e2_y

            if isfunction(f_xy):
                som += w[j]*f_xy(x_i, y_i)
            else:
                som += w[j]*f_xy

        sum += w[i]*e1_y*som

    result = e1_x*sum

    return result
1