Advertisement
Sephinroth

RM

Dec 22nd, 2020
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from Progonka import *
  4.  
  5. def main():
  6.     begin = 0
  7.     end = 2
  8.     h = 0.01
  9.     y_0 = 0
  10.     y_T = 0
  11.     x = []
  12.     for i in np.arange(begin, end+h, h):
  13.         x.append(h*i)
  14.     n = len(x)    
  15.     matrix = np.zeros((n, n))
  16.     x, y = Razn_method(x, matrix, n, h, y_0, y_T)
  17.     y_ans = [Y(i) for i in x]
  18.     plt.plot(x, y)
  19.     plt.plot(x, y_ans)
  20.     plt.suptitle("График решения краевой задачи")
  21.     plt.grid()
  22.     for i in range(n+1):
  23.         print("{0} - {1}".format(y[i],y_ans[i]))
  24.    
  25. def Razn_method(x, matrix, n, h, y_0, y_T):
  26.  
  27.     for i in range(1, n):            
  28.         if i > 0: matrix[i][i-1] = (2*p(x[i])*h)/(2*h**2)
  29.         matrix[i][i] = (q(x[i])*h**2-2)/(h**2)
  30.         if i < n-2: matrix[i][i+1] = (2-p(x[i])*h)/(2*h**2)
  31.     d = [f(i) for i in x]
  32.     y = Tridiagonal_solve(matrix, d, n-1)
  33.     y_alt = np.linalg.solve(matrix, d)
  34.     print(y)
  35.     print(y_alt)
  36.     y.insert(y_0, 0)
  37.     y.append(y_T)
  38.     return x, y
  39.    
  40. def Y(x): return x*x*(x-2)
  41. def p(x): return -x*x
  42. def q(x): return -x**3
  43. def f(x):
  44.     return -x**6 - 2*x**5 + 3*x**4 - 4*x**3+6*x-4
  45.    
  46. if __name__ == "__main__"    :
  47.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement