def delta_x(x): mat = Matrix.from_list(jacobi(x)) B = Vector.from_list([-f1(x), -f2(x)]) LU, P = lu.LU_decomposition(mat) new_B = lu.get_new_B(B, P) delta = lu.LU_solve(LU, new_B) return delta.get_data()
def get_c(x, f, h): n = len(f) a = [0] + [h[i - 1] for i in range(3, n)] b = [2 * (h[i - 1] + h[i]) for i in range(2, n)] c = [h[i] for i in range(2, n - 1)] + [0] d = [ 3 * ((f[i] - f[i - 1]) / h[i] - ((f[i - 1] - f[i - 2]) / h[i - 1])) for i in range(2, n) ] x = tma(TridiagonalMatrix.from_lists(a, b, c), Vector.from_list(d)) res = [0, 0] + x.get_data() return res
def mls(n, x, y): N = len(x) mat = [[sum([x_j**(i + j) for x_j in x]) for i in range(n + 1)] for j in range(n + 1)] mat[0][0] = N + 1 b = [sum([x_j**i * y_j for x_j, y_j in zip(x, y)]) for i in range(n + 1)] mat = Matrix.from_list(mat) B = Vector.from_list(b) LU, P = lu.LU_decomposition(mat) new_B = lu.get_new_B(B, P) coefs = lu.LU_solve(LU, new_B) return coefs.get_data()
def finite_difference_method(f, p, q, a, b, alpha, beta, delta, gamma, y0, y1, h): n = int((b - a) / h) x = [i for i in np.arange(a, b + h, h)] A = [0] + [1 - p(x[i]) * h / 2 for i in range(0, n - 1)] + [-gamma] B = [alpha * h - beta] + [q(x[i]) * h**2 - 2 for i in range(0, n - 1)] + [delta * h + gamma] C = [beta] + [1 + p(x[i]) * h / 2 for i in range(0, n - 1)] + [0] D = [y0 * h] + [f(x[i]) * h**2 for i in range(0, n - 1)] + [y1 * h] y = tma(TridiagonalMatrix.from_lists(A, B, C), Vector.from_list(D)) return x, y.get_data()
def seidel_method(alpha, beta, eps): sz = len(alpha) np_alpha = np.array(alpha.get_data()) np_beta = np.array(beta.get_data()) B = np.tril(np_alpha, -1) C = np_alpha - B tmp1 = inv(np.eye(sz, sz) - B) @ C tmp2 = inv(np.eye(sz, sz) - B) @ np_beta x = tmp2 while True: x_i = tmp2 + tmp1 @ x if finish_iter_process(x_i, x, norm(tmp1), norm(C), eps, zeidel=True): break x = x_i return Vector.from_list(x_i.tolist())