Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- def p(x):
- return np.cos(x)
- def q(x):
- return np.cosh(x)
- def f(x):
- return np.cos(x)
- def methProg(p, q, f, a, b, c1, c2, c, d1, d2, d, n):
- h = (b - a) / n
- u = np.zeros(n + 1)
- v = np.zeros(n + 1)
- x = np.zeros(n + 1)
- y = np.zeros(n + 1)
- alp = np.zeros(n + 1)
- bet = np.zeros(n + 1)
- gam = np.zeros(n + 1)
- phi = np.zeros(n + 1)
- x[0] = a
- i = 1
- u[0] = c * h / (c1 * h - c2)
- v[0] = -c2 / (c1 * h - c2)
- while True:
- x[i] = x[i - 1] + h
- alp[i] = 1 - p(x[i]) * h / 2
- bet[i] = h ** 2 * q(x[i]) - 2
- gam[i] = 1 + p(x[i]) * h / 2
- phi[i] = (h ** 2) * f(x[i])
- v[i] = -gam[i] / (bet[i] + alp[i] * v[i - 1])
- u[i] = (phi[i] - alp[i] * u[i - 1]) / (bet[i] + alp[i] * v[i])
- if i == n - 1:
- break
- i += 1
- x[n] = b
- alp[n] = -d2
- bet[n] = h * d1 + d2
- phi[n] = h * d
- v[n] = 0
- u[n] = (phi[n] - alp[n] * u[n - 1]) / bet[n]
- y[n] = u[n]
- i = n - 1
- while True:
- y[i] = u[i] + y[i + 1] * v[i]
- if i == 0:
- break
- i -= 1
- return {'x': x, 'y': y}
- a = -1
- b = 1
- c1 = 0.1
- c2 = 0.4
- c = 0
- d1 = 0.9
- d2 = 0.92
- d = 0
- n = 500
- res = methProg(p, q, f, a, b, c1, c2, c, d1, d2, d, n)
- plt.plot(res['x'], res['y'])
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement