Advertisement
EWTD

AdditionalLab2

Sep 22nd, 2023
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import math
  2. import numpy as np
  3.  
  4. def phi(x):
  5.     return math.exp(x)
  6.  
  7. def f(x):
  8.     return ((x+2)*math.exp(x) + math.exp(2*x+4) - 1)/(x+2)
  9.  
  10. def K(x, y):
  11.     return math.exp(x*y)
  12.  
  13. start = 0
  14. end = 2
  15. n = 10
  16. h = (end - start)/n
  17. xi = [start + h*(i + 1/2) for i in range(n)]
  18. buffer = []
  19. real_sol = np.matrix([[np.float64(phi(xi[i])) for i in range(n)]]).transpose()
  20. _ = real_sol
  21. coeffs = np.matrix([[np.float64(f(xi[i-1])) for i in range(1, n+1)]]).transpose()
  22. for i in range(1, n+1):
  23.     app = []
  24.     for j in range(1, n+1):
  25.         if i == j:
  26.             app.append(np.float64(1 - h*K(xi[i-1], xi[j-1])))
  27.         else:
  28.             app.append(np.float64(-h*K(xi[i-1], xi[j-1])))
  29.     buffer.append(app)
  30. System = np.matrix(buffer)
  31. solution = np.linalg.solve(System, coeffs)
  32. print("------------PHI(X)--------")
  33. print(real_sol)
  34. print("------------~PHI(X)--------")
  35. print(solution)
  36. print("--------------------")
  37. print(max([math.fabs(solution[i][0] - real_sol[i][0]) for i in range(n)]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement