Advertisement
myloyo

№12 Квадратурный метод решения интегрального уравнения Фредгольма

Dec 12th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def f(x, v=8):
  4.     return v * (4 / 3 * x + 1 / 4 * x ** 2 + 1 / 5 * x ** 3)
  5.  
  6. def gauss(A, B):
  7.     n = len(A)
  8.     A = np.array(A, dtype=float)
  9.     B = np.array(B, dtype=float)
  10.     for i in range(n):
  11.         # Partial pivoting
  12.         max_row = max(range(i, n), key=lambda r: abs(A[r, i]))
  13.         if abs(A[max_row, i]) < 1e-6:
  14.             raise ValueError("Division by zero detected in Gaussian elimination.")
  15.         if max_row != i:
  16.             A[[i, max_row]] = A[[max_row, i]]
  17.             B[[i, max_row]] = B[[max_row, i]]
  18.  
  19.         # Eliminate column i
  20.         for j in range(i + 1, n):
  21.             factor = A[j, i] / A[i, i]
  22.             A[j, i:] -= factor * A[i, i:]
  23.             B[j] -= factor * B[i]
  24.  
  25.     # Back substitution
  26.     x = np.zeros_like(B)
  27.     for i in range(n - 1, -1, -1):
  28.         x[i] = (B[i] - np.dot(A[i, i + 1:], x[i + 1:])) / A[i, i]
  29.     return x
  30.  
  31. def main():
  32.     v = 8
  33.     n = 3
  34.  
  35.     l, r = 0, 1
  36.     h = (r - l) / (n - 1)
  37.     vx = np.linspace(l, r, n)
  38.  
  39.     A = np.zeros((n, n))
  40.     F = np.zeros(n)
  41.  
  42.     for i in range(n):
  43.         x_i = vx[i]
  44.         for j in range(n):
  45.             x_j = vx[j]
  46.             A[i, j] = h * (x_j * x_i + x_j ** 2 * x_i ** 2 + x_j ** 3 * x_i ** 3)
  47.             if i == j:
  48.                 A[i, j] += 1
  49.         F[i] = f(x_i, v)
  50.  
  51.     y = gauss(A, F)
  52.  
  53.     res1 = np.zeros((4, n))
  54.     res1[0] = vx
  55.  
  56.     for i in range(n):
  57.         x_i = vx[i]
  58.         res1[1, i] = f(x_i, v)
  59.         for j in range(n):
  60.             x_j = vx[j]
  61.             res1[1, i] -= h * (x_j * x_i + x_j ** 2 * x_i ** 2 + x_j ** 3 * x_i ** 3) * y[j]
  62.         res1[2, i] = v * x_i
  63.         res1[3, i] = res1[1, i] - res1[2, i]
  64.  
  65.     print("Решение интегрального уравнения Фредгольма квадратурным методом")
  66.     for row in res1:
  67.         print(" ".join(f"{value:14.5f}" for value in row))
  68.  
  69. if __name__ == "__main__":
  70.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement