Advertisement
svinoviteran

diff

May 24th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def p(x):
  5.     return np.cos(x)
  6. def q(x):
  7.     return np.cosh(x)
  8. def f(x):
  9.     return np.cos(x)
  10.  
  11.  
  12. def methProg(p, q, f, a, b, c1, c2, c, d1, d2, d, n):
  13.     h = (b - a) / n
  14.     u = np.zeros(n + 1)
  15.     v = np.zeros(n + 1)
  16.     x = np.zeros(n + 1)
  17.     y = np.zeros(n + 1)
  18.     alp = np.zeros(n + 1)
  19.     bet = np.zeros(n + 1)
  20.     gam = np.zeros(n + 1)
  21.     phi = np.zeros(n + 1)
  22.  
  23.     x[0] = a
  24.     i = 1
  25.     u[0] = c * h / (c1 * h - c2)
  26.     v[0] = -c2 / (c1 * h - c2)
  27.  
  28.     while True:
  29.         x[i] = x[i - 1] + h
  30.         alp[i] = 1 - p(x[i]) * h / 2
  31.         bet[i] = h ** 2 * q(x[i]) - 2
  32.         gam[i] = 1 + p(x[i]) * h / 2
  33.         phi[i] = (h ** 2) * f(x[i])
  34.         v[i] = -gam[i] / (bet[i] + alp[i] * v[i - 1])
  35.         u[i] = (phi[i] - alp[i] * u[i - 1]) / (bet[i] + alp[i] * v[i])
  36.         if i == n - 1:
  37.             break
  38.         i += 1
  39.     x[n] = b
  40.     alp[n] = -d2
  41.     bet[n] = h * d1 + d2
  42.     phi[n] = h * d
  43.     v[n] = 0
  44.     u[n] = (phi[n] - alp[n] * u[n - 1]) / bet[n]
  45.     y[n] = u[n]
  46.     i = n - 1
  47.     while True:
  48.         y[i] = u[i] + y[i + 1] * v[i]
  49.         if i == 0:
  50.             break
  51.         i -= 1
  52.     return {'x': x, 'y': y}
  53. a = -1
  54. b = 1
  55. c1 = 0.1
  56. c2 = 0.4
  57. c = 0
  58. d1 = 0.9
  59. d2 = 0.92
  60. d = 0
  61. n = 500
  62.  
  63.  
  64. res = methProg(p, q, f, a, b, c1, c2, c, d1, d2, d, n)
  65. plt.plot(res['x'], res['y'])
  66. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement