def SOR(matrix, B, omega, n, eps): x_error = create_vector(n) x = create_vector(n) temp2 = create_vector(n) iteration = create_matrix(n) temp = create_matrix(n) for i in range(n): for j in range(n): if j > i: iteration[i][j] = 0 elif j == i: iteration[i][j] = (1.0 / omega) * matrix[i][j] else: iteration[i][j] = (-1) * matrix[i][j] iteration = reverse_matrix(iteration, n) for i in range(n): for j in range(n): temp[i][j] = 0 for k in range(n): temp[i][j] += iteration[i][k] * matrix[k][j] for i in range(n): temp2[i] = 0 for j in range(n): temp2[i] += iteration[i][j] * B[j] for i in range(n): for j in range(n): if i == j: iteration[i][j] = 1 - temp[i][j] else: iteration[i][j] = (-1) * temp[i][j] B[i] = temp2[i] for i in range(n): x_error[i] = 1e+300 x[i] = 1 epsilon = eps steps = 0 while maximum_norm(x_error) > epsilon and steps < MAX_STEPS: steps += 1 for i in range(n): for j in range(n): temp3 = 0 for k in range(n): temp3 += iteration[j][k] * x[k] temp3 += B[j] x_error[j] = x[j] - temp3 x[j] = temp3 # print("steps:" + str(steps)) return x, steps
def SOR(matrix, B, omega, n, eps): x_error = create_vector(n) x = create_vector(n) temp2 = create_vector(n) iteration = create_matrix(n) temp = create_matrix(n) for i in range(n): for j in range(n): if j > i: iteration[i][j] = 0 elif j == i: iteration[i][j] = (1.0/omega) * matrix[i][j] else: iteration[i][j] = (-1) * matrix[i][j] iteration = reverse_matrix(iteration, n) for i in range(n): for j in range(n): temp[i][j] = 0 for k in range(n): temp[i][j] += iteration[i][k] * matrix[k][j] for i in range(n): temp2[i] = 0 for j in range(n): temp2[i] += iteration[i][j] * B[j] for i in range(n): for j in range(n): if i == j: iteration[i][j] = 1 - temp[i][j] else: iteration[i][j] = (-1) * temp[i][j] B[i] = temp2[i] for i in range(n): x_error[i] = 1e+300 x[i] = 1 epsilon = eps steps = 0 while maximum_norm(x_error) > epsilon and steps < MAX_STEPS: steps += 1 for i in range(n): for j in range(n): temp3 = 0 for k in range(n): temp3 += iteration[j][k] * x[k] temp3 += B[j] x_error[j] = x[j] - temp3 x[j] = temp3 # print("steps:" + str(steps)) return x, steps
def algebraic_approximation(n, m, points, values): matrix_a = create_matrix(n, m + 1) for i in range(n): for j in range(m + 1): matrix_a[i][j] = math.pow(points[i], j) matrix_b = create_matrix(m + 1, n) for i in range(n): for j in range(m + 1): matrix_b[j][i] = matrix_a[i][j] coefficients = create_matrix(m + 1, m + 1) coefficients = multiply_matrixes(matrix_a, matrix_b, coefficients, n, m) vals = create_vector(m + 1) vals = multiply_vector(matrix_b, vals, n, m + 1, values) result = gaussian_elimination(coefficients, vals, m + 1) return result
def algebraic_approximation(n, m, points, values): matrix_a = create_matrix(n, m+1) for i in range(n): for j in range(m+1): matrix_a[i][j] = math.pow(points[i], j) matrix_b = create_matrix(m+1, n) for i in range(n): for j in range(m+1): matrix_b[j][i] = matrix_a[i][j] coefficients = create_matrix(m+1, m+1) coefficients = multiply_matrixes(matrix_a, matrix_b, coefficients, n, m) vals = create_vector(m+1) vals = multiply_vector(matrix_b, vals, n, m+1, values) result = gaussian_elimination(coefficients, vals, m+1) return result
def fill_diffs(n, points, values): diffs = create_matrix(n+1) for i in range(n): diffs[i][0] = points[i] diffs[i][1] = values[i] for i in range(2, n+1): for j in range(1+(i-2), n): diffs[j][i] = (diffs[j][i-1] - diffs[j-1][i-1]) / (points[j] - points[(j-1) - (i-2)]) return diffs
def fill_diffs(n, points, values): diffs = create_matrix(n + 1) for i in range(n): diffs[i][0] = points[i] diffs[i][1] = values[i] for i in range(2, n + 1): for j in range(1 + (i - 2), n): diffs[j][i] = (diffs[j][i - 1] - diffs[j - 1][i - 1]) / (points[j] - points[(j - 1) - (i - 2)]) return diffs
def fill_hermite_differences(n, points, values, derivative): differences = create_matrix(2*n+1) for i in range(0, 2*n, 2): differences[i][0] = points[int(i/2)] differences[i+1][0] = points[int(i/2)] differences[i][1] = values[int(i/2)] differences[i+1][1] = values[int(i/2)] for i in range(2, 2*n+1): for j in range(1+(i-2), 2*n): if i == 2 and j % 2 == 1: differences[j][i] = derivative(points[int(j/2)]) else: differences[j][i] = (differences[j][i-1] - differences[j-1][i-1]) / \ (differences[j][0] - differences[(j-1)-(i-2)][0]) return differences
def derivative_for_solution(solution, n): x = solution[0] y = solution[1] z = solution[2] derivative = create_matrix(n) derivative[0][0] = 2 * x derivative[0][1] = 2 * y derivative[0][2] = -2 * z derivative[1][0] = 1 derivative[1][1] = (-6) * y * y derivative[1][2] = 4 * z derivative[2][0] = 4 * x derivative[2][1] = 1 derivative[2][2] = -4 * z return derivative
def derivative_for_solution(solution, n): x = solution[0] y = solution[1] z = solution[2] derivative = create_matrix(n) derivative[0][0] = 2*x derivative[0][1] = 2*y derivative[0][2] = -2*z derivative[1][0] = 1 derivative[1][1] = (-6)*y*y derivative[1][2] = 4*z derivative[2][0] = 4*x derivative[2][1] = 1 derivative[2][2] = -4*z return derivative
def create_system_for_square(points, values, n, variant): matrix_a = create_matrix(3 * (n - 1)) vector_b = create_vector(3 * (n - 1)) distance = points[1] - points[0] for i in range(0, 3 * (n - 1), 3): if i != 3 * (n - 2): matrix_a[i][i] = 2 * distance matrix_a[i][i + 1] = 1 matrix_a[i][i + 4] = -1 else: calculate_variant(variant, matrix_a, i, distance) j = i + 1 matrix_a[j][j - 1] = distance * distance matrix_a[j][j] = distance vector_b[j] = values[int(j / 3) + 1] - values[int(j / 3)] k = j + 1 matrix_a[k][k] = 1 vector_b[k] = values[int(k / 3)] return matrix_a, vector_b
def create_system_for_square(points, values, n, variant): matrix_a = create_matrix(3*(n-1)) vector_b = create_vector(3*(n-1)) distance = points[1] - points[0] for i in range(0, 3*(n-1), 3): if i != 3 * (n-2): matrix_a[i][i] = 2 * distance matrix_a[i][i+1] = 1 matrix_a[i][i+4] = -1 else: calculate_variant(variant, matrix_a, i, distance) j = i + 1 matrix_a[j][j-1] = distance * distance matrix_a[j][j] = distance vector_b[j] = values[int(j / 3) + 1] - values[int(j / 3)] k = j + 1 matrix_a[k][k] = 1 vector_b[k] = values[int(k / 3)] return matrix_a, vector_b
def fdm_implicit(): x_start = 0 x_end = 2 * math.pi t_start = 0 t_end = 3.5 a = 2 for pair in [(20, 480)]: x_points = pair[0] + 1 t_points = pair[1] + 1 x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) # vectors created, x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) for i in range(x_points): x_coord[i] = x_start + i * x_step t_cord = create_vector(t_points) for i in range(t_points): t_cord[i] = t_start + i * t_step # grid created meshgrid = create_matrix(t_points, x_points) for i in range(x_points): meshgrid[0][i] = 2 * math.sin( 2 * x_coord[i]) * math.exp(-x_coord[i]) for i in range(t_points): meshgrid[i][0] = 0 meshgrid[i][x_points - 1] = 0 coefficient = (math.pow(a, 2) * t_step) / math.pow(x_step, 2) values = create_vector(x_points) matrix = create_tridiagonal(x_points) for j in range(1, t_points): for k in range(1, x_points - 1): matrix[k][0] = (-1) * coefficient matrix[k][1] = 1 + 2 * coefficient matrix[k][2] = (-1) * coefficient values[k] = meshgrid[j - 1][k] matrix[0][0] = 0 matrix[0][1] = 1 matrix[0][2] = 0 matrix[x_points - 1][0] = 0 matrix[x_points - 1][1] = 1 matrix[x_points - 1][2] = 0 values[0] = meshgrid[j][0] values[x_points - 1] = meshgrid[j][x_points - 1] thomas_algorithm(matrix, values, x_points) for i in range(x_points - 1): meshgrid[j][i] = values[i] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = x_coord y = t_cord X, Y = np.meshgrid(x, y) ax.plot_surface( X, Y, meshgrid, cstride=1, rstride=1, linewidth=0.0, cmap=cm.get_cmap("gist_rainbow"), ) ax.set_xlabel("os x") ax.set_ylabel("os t") ax.set_zlabel("rozwiazanie") file_name = '../plots/fdm/imp' + str(x_points) + "," + str( t_points) + '.png' plt.savefig(file_name) # plt.show() plt.close() max_val = 0 for i in range(x_points): for j in range(t_points): if math.fabs(meshgrid[j][i] > max_val): max_val = abs(meshgrid[j][i]) print( str(x_points) + ", " + str(t_points) + ", " + str(coefficient) + ", " + str(max_val))
def fdm(): x_start = 0 x_end = 2 * math.pi t_start = 0 t_end = 3.5 a = 2 for pair in [(20, 480)]: x_points = pair[0] + 1 t_points = pair[1] + 1 # vectors created, x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) for i in range(x_points): x_coord[i] = x_start + i * x_step t_cord = create_vector(t_points) for i in range(t_points): t_cord[i] = t_start + i * t_step # grid created meshgrid = create_matrix(t_points, x_points) for i in range(x_points): meshgrid[0][i] = 2 * math.sin( 2 * x_coord[i]) * math.exp(-x_coord[i]) for i in range(t_points): meshgrid[i][0] = 0 meshgrid[i][x_points - 1] = 0 # a*a+k/h*h coefficient = (math.pow(a, 2) * t_step) / math.pow(x_step, 2) for j in range(1, t_points): for i in range(1, x_points - 1): meshgrid[j][i] = coefficient * meshgrid[j-1][i-1] + \ (1-2*coefficient)*meshgrid[j-1][i] + coefficient * meshgrid[j-1][i+1] # plot drawing fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = x_coord y = t_cord X, Y = np.meshgrid(x, y) try: ax.plot_surface( X, Y, meshgrid, cstride=1, rstride=1, linewidth=0.0, cmap=cm.get_cmap("gist_rainbow"), ) ax.set_xlabel("os x") ax.set_ylabel("os t") ax.set_zlabel("rozwiazanie") file_name = '../plots/fdm/ex' + str(x_points) + "," + str( t_points) + '.png' plt.savefig(file_name) # plt.show() plt.close() except Exception as e: print(e.__class__) max_val = 0 for i in range(x_points): for j in range(t_points): if math.fabs(meshgrid[j][i] > max_val): max_val = abs(meshgrid[j][i]) print( str(x_points) + ", " + str(t_points) + ", " + str(coefficient) + ", " + str(max_val))
def fdm_implicit(): x_start = 0 x_end = 2*math.pi t_start = 0 t_end = 3.5 a = 2 for pair in [ (20, 480) ]: x_points = pair[0] + 1 t_points = pair[1] + 1 x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) # vectors created, x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) for i in range(x_points): x_coord[i] = x_start + i * x_step t_cord = create_vector(t_points) for i in range(t_points): t_cord[i] = t_start + i * t_step # grid created meshgrid = create_matrix(t_points, x_points) for i in range(x_points): meshgrid[0][i] = 2 * math.sin(2*x_coord[i]) * math.exp(-x_coord[i]) for i in range(t_points): meshgrid[i][0] = 0 meshgrid[i][x_points-1] = 0 coefficient = (math.pow(a, 2) * t_step) / math.pow(x_step, 2) values = create_vector(x_points) matrix = create_tridiagonal(x_points) for j in range(1, t_points): for k in range(1, x_points-1): matrix[k][0] = (-1) * coefficient matrix[k][1] = 1 + 2*coefficient matrix[k][2] = (-1)*coefficient values[k] = meshgrid[j-1][k] matrix[0][0] = 0 matrix[0][1] = 1 matrix[0][2] = 0 matrix[x_points-1][0] = 0 matrix[x_points-1][1] = 1 matrix[x_points-1][2] = 0 values[0] = meshgrid[j][0] values[x_points-1] = meshgrid[j][x_points-1] thomas_algorithm(matrix, values, x_points) for i in range(x_points-1): meshgrid[j][i] = values[i] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = x_coord y = t_cord X, Y = np.meshgrid(x, y) ax.plot_surface(X, Y, meshgrid, cstride=1, rstride=1, linewidth=0.0, cmap=cm.get_cmap("gist_rainbow"),) ax.set_xlabel("os x") ax.set_ylabel("os t") ax.set_zlabel("rozwiazanie") file_name = '../plots/fdm/imp' + str(x_points) + "," + str(t_points) + '.png' plt.savefig(file_name) # plt.show() plt.close() max_val = 0 for i in range(x_points): for j in range(t_points): if math.fabs(meshgrid[j][i] > max_val): max_val = abs(meshgrid[j][i]) print(str(x_points) + ", " + str(t_points) + ", " + str(coefficient) + ", " + str(max_val))
matrix[j][j] = M / j else: matrix[j][j] = 0 matrix[j][j] = K def fill_vector(vector, n): for j in range(n): if j % 2 == 0: vector[j] = -1 else: vector[j] = 1 if __name__ == "__main__": for i in [10, 30, 50, 100, 150, 250, 500, 750, 1000, 5000]: matrix_A = create_matrix(i) fill_matrix(matrix_A, i) vector_X = create_vector(i) fill_vector(vector_X, i) vector_B = calculate_constant_terms_vector(matrix_A, vector_X, i) x = jacobie_method(matrix_A, vector_B, 1e-15, euclidean_norm, i) euclidean = euclidean_norm(vector_difference(vector_X, x)) maximum = maximum_norm(vector_difference(vector_X, x)) print("Matrix size: " + str(i)) print(euclidean) print(maximum) radius = spectral_radius(matrix_A, i) print("Matrix size: " + str(i) + "\tspectral radius: " + str(radius))
else: matrix[j][j] = 0 matrix[j][j] = K def fill_vector(vector, n): for j in range(n): if j % 2 == 0: vector[j] = -1 else: vector[j] = 1 if __name__ == "__main__": for i in [10, 30, 50, 100, 150, 250, 500, 750, 1000, 5000]: matrix_A = create_matrix(i) fill_matrix(matrix_A, i) vector_X = create_vector(i) fill_vector(vector_X, i) vector_B = calculate_constant_terms_vector(matrix_A, vector_X, i) x = jacobie_method(matrix_A, vector_B, 1e-15, euclidean_norm, i) euclidean = euclidean_norm(vector_difference(vector_X, x)) maximum = maximum_norm(vector_difference(vector_X, x)) print("Matrix size: " + str(i)) print(euclidean) print(maximum) radius = spectral_radius(matrix_A, i) print("Matrix size: " + str(i) + "\tspectral radius: " + str(radius))
def fdm(): x_start = 0 x_end = 2*math.pi t_start = 0 t_end = 3.5 a = 2 for pair in [ (20, 480) ]: x_points = pair[0] + 1 t_points = pair[1] + 1 # vectors created, x_step = (x_end - x_start) / (x_points - 1) t_step = (t_end - t_start) / (t_points - 1) x_coord = create_vector(x_points) for i in range(x_points): x_coord[i] = x_start + i * x_step t_cord = create_vector(t_points) for i in range(t_points): t_cord[i] = t_start + i * t_step # grid created meshgrid = create_matrix(t_points, x_points) for i in range(x_points): meshgrid[0][i] = 2 * math.sin(2*x_coord[i]) * math.exp(-x_coord[i]) for i in range(t_points): meshgrid[i][0] = 0 meshgrid[i][x_points-1] = 0 # a*a+k/h*h coefficient = (math.pow(a, 2) * t_step) / math.pow(x_step, 2) for j in range(1, t_points): for i in range(1, x_points-1): meshgrid[j][i] = coefficient * meshgrid[j-1][i-1] + \ (1-2*coefficient)*meshgrid[j-1][i] + coefficient * meshgrid[j-1][i+1] # plot drawing fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = x_coord y = t_cord X, Y = np.meshgrid(x, y) try: ax.plot_surface(X, Y, meshgrid, cstride=1, rstride=1, linewidth=0.0, cmap=cm.get_cmap("gist_rainbow"),) ax.set_xlabel("os x") ax.set_ylabel("os t") ax.set_zlabel("rozwiazanie") file_name = '../plots/fdm/ex' + str(x_points) + "," + str(t_points) + '.png' plt.savefig(file_name) # plt.show() plt.close() except Exception as e: print(e.__class__) max_val = 0 for i in range(x_points): for j in range(t_points): if math.fabs(meshgrid[j][i] > max_val): max_val = abs(meshgrid[j][i]) print(str(x_points) + ", " + str(t_points) + ", " + str(coefficient) + ", " + str(max_val))