u2[i] = ((V_ro(U[0, m[i]]) - U[1, m[i]]) / tau) return np.array([u1, u2]) def f_u(U, m): u1 = U[0, m] * U[1, m] u2 = 1 / 2 * (U[1, m] ** 2) + (c_0 ** 2) * np.log(U[0, m]) return np.array([u1, u2]) a = 9 b = 16 error = np.zeros(b - a) k_vec = np.zeros(b - a) ref = rw.read_data("L_F_ref") for i in range(a, b): k = 2 ** (-i) N = int(10 / k) u = np.zeros([2, len(x) + 1]) u_next = np.zeros([2, len(x) + 1]) initial_velocity = V_ro(rho_up) u[0, :] = rho_up u[1, :] = initial_velocity u_next[0, :] = rho_up u_next[1, :] = initial_velocity for n in range(N): f = f_u(u, range(1, len(x) + 1)) s_next = s(u, range(1, len(x) + 1), n)
f = f_u(u, range(0, len(x) + 1)) # length: L/h s_next = s(u, range(0, len(x) - 1), n) # length: L/h #print("length of x:",len(x)) #print("length of u:",len(u[0])) for m in range(1, len(x) - 1): u_next[0, m] = ( (u[0, m - 1] + u[0, m + 1]) / 2) - (k / (2 * h)) * (f[0, m + 1] - f[0, m - 1]) + (k * s_next[0, m]) u_next[1, m] = ((u[1, m - 1] + u[1, m + 1]) / 2) - (k / (2 * h)) * ( f[1, m + 1] - f[1, m - 1]) + (k * s_next[1, m]) + ( (k / (h**2)) * (u[1, m + 1] - 2 * u[1, m] + u[1, m - 1])) u_next[0, 0] = rho_up u_next[1, 0] = initial_velocity u_next[:, len(x)] = u_next[:, len(x) - 1] u = u_next #print(n) if (n % (N / 10) == 0): plt.plot(x, u[0][:-1]) plt.show() if __name__ == "__main__": print("hei") rw.write_data(u, "u_lax_friedrich_x3.txt") a = rw.read_data("u_lax_friedrich_x3.txt") print(a) #plt.plot(x,u[0][:-1]) #plt.show()
import numpy as np import matplotlib.pyplot as plt import readwrite as rw # This script makes a convergence plot for Lax-Wendroff in space # The different step lengths we are iterating for h_values = [2**i for i in range(3, 9)] ref_sol = rw.read_data("Reference solution, Lax-Wendroff, x-convergence.txt" ) # Reference solution # Constants: k = 10**-4 sigma = 300 tau = 0.5 V_0 = 2000 rho_hat = 0.14 E = 100 c_0 = 900 mu = 10000 f_up = 32.5 f_rmp = 2 rho_up = 0.02 N = 10**4 L = 2**13 ref_sol_points = L / ( len(ref_sol[0]) - 1 ) # Relationship between length of the road and length of reference solution number_of_discretizations = len(h_values) errors1 = np.zeros(number_of_discretizations) # Error in 1-norm
u2[i] = ((V_ro(U[0, m[i]]) - U[1, m[i]]) / tau) return np.array([u1, u2]) def f_u(U, m): u1 = U[0, m] * U[1, m] u2 = 1 / 2 * (U[1, m]**2) + (c_0**2) * np.log(U[0, m]) return np.array([u1, u2]) a = 9 b = 12 error = np.zeros(b - a) k_vec = np.zeros(b - a) ref = rw.read_data("ber1.txt") for i in range(a, b): #16 k = 2**(-i) N = int(10 / k) u = np.zeros([2, len(x) + 1]) #2 x M u_next = np.zeros([2, len(x) + 1]) initial_velocity = V_ro(rho_up) u[0, :] = rho_up u[1, :] = initial_velocity u_next[0, :] = rho_up u_next[1, :] = initial_velocity for n in range(N): f = f_u(u, range(1, len(x) + 1)) s_next = s(u, range(1, len(x) + 1), n)
rho_hat = 0.14 E = 100 c_0 = 900 mu = 100 f_up = 32.5 f_rmp = 2 rho_up = 0.02 N = 10**4 # The different step lengths we are iterating for h_values = [2**i for i in range(2, 9)] number_of_discretizations = len(h_values) errors2 = np.zeros(number_of_discretizations) # Error in 2-norm ref_sol = rw.read_data("Reference solution, Lax-Friedrich, x-convergence.txt" ) # Reference solution ref_sol_points = L / ( len(ref_sol[0]) - 1 ) # Relationship between length of the road and length of reference solution def q(t): return f_rmp def phi(x): return ((2 * np.pi * (sigma**2))**(-1 / 2)) * np.exp(-(x**2) / (2 * (sigma**2))) def V_ro(ro):
c_0 = 54 mu = 600 f_up = 1948 f_rmp = 121 rho_up = 20 N = 1000 """ # Lager konvergensplot for Lax-Wendroff i x-retning #h_values = [2**i for i in range(4, 8)] h_values = [2**i for i in range(3, 9)] #h_values = [10, 20, 50, 100, 200, 500] number_of_discretizations = len(h_values) errors1 = np.zeros(number_of_discretizations) # Error in 1-norm errors2 = np.zeros(number_of_discretizations) # Error in 2-norm ref_sol = rw.read_data("u_lax_wendroff_x2.txt") # Reference solution L = 2**13 ref_sol_points = L / (len(ref_sol[0]) - 1) k = 10**-4 sigma = 300 tau = 0.5 V_0 = 2000 rho_hat = 0.14 E = 100 c_0 = 900 mu = 10000 f_up = 32.5 f_rmp = 2 rho_up = 0.02 N = 10**4
from sklearn.neural_network import MLPClassifier import matplotlib.pyplot as plt from readwrite import read_data, write_data from sklearn.metrics import plot_confusion_matrix train_images, train_labels, test_images, validation_images, validation_labels = read_data( ) classifier = MLPClassifier(hidden_layer_sizes=(100, ), activation='relu', solver='adam', alpha=0.0001, batch_size='auto', learning_rate='constant', power_t=0.5, max_iter=10, shuffle=True, random_state=None, tol=0.0001, momentum=0.9, early_stopping=False, validation_fraction=0.1, n_iter_no_change=10) classifier.fit(train_images, train_labels) plot_confusion_matrix(classifier, validation_images, validation_labels) plt.show() plt.savefig('mlp.png') prediction = classifier.predict(test_images) write_data(prediction)
import readwrite as rw # Lager konvergensplot for Lax-Friedrichs i x-retning k = 10**-4 L = 2**13 #h_values = [50, 100, 200, 500] #h_values = [50, 100] h_values = [2**i for i in range(2, 9)] number_of_discretizations = len(h_values) errors1 = np.zeros(number_of_discretizations) # Error in 1-norm errors2 = np.zeros(number_of_discretizations) # Error in 2-norm ref_sol = rw.read_data("u_lax_friedrich_x2.txt") # Reference solution ref_sol_points = L / (len(ref_sol[0]) - 1) sigma = 300 tau = 0.5 V_0 = 2000 rho_hat = 0.14 E = 100 c_0 = 900 mu = 100 f_up = 32.5 f_rmp = 2 rho_up = 0.02 N = 10**4 def q(t):
f_next_p1 = f_u(u,m+1) #m+1 u_half[0,m] = ((u[0,m] + u[0,m+1])/2) -(k/(2*h))*(f_next_p1[0]-f_next[0])+((k/2)*(s_next[0]+s_next_p1[0])) u_half[1,m] = ((u[1,m] + u[1,m+1])/2) -(k/(2*h))*(f_next_p1[1]-f_next[1])+((k/2)*(s_next[1]+s_next_p1[1])) f_half_p1 = f_u(u_half,m) f_half_m1 = f_u(u_half,m-1) s_half_p1 = s(u_half, m, n) s_half_m1 = s(u_half, m-1, n) u_next[0,m]= u[0,m] -(k/(2*h))*(f_half_p1[0]-f_half_m1[0])+((k/2)*(s_half_p1[0]+s_half_m1[0])) u_next[1,m] = u[1,m] -(k/(2*h))*(f_half_p1[1]-f_half_m1[1])+((k/2)*(s_half_p1[1]+s_half_m1[1])) #print(((u[0, m - 1] + u[0, m + 1]) / 2), (k / (2 * h)) * (f_next_p1[0] - f_next_m1[0]), (k * s_next[0])) #print(((u[1,m-1]+ u[1,m+1])/2),(k/(2*h))*(f_next_p1[1]-f_next_m1[1]),(k*s_next[1])) u_next[0,0] = u_next[0,1] -(u_next[0,2]-u_next[0,1]) u_next[1,0] = u_next[1,1] - (u_next[1,2]-u_next[1,1]) u_next[0, len(x)] = u_next[0, len(x)-1]+(u_next[0,len(x)-1]-u_next[0, len(x)-2]) u_next[1, len(x)] = u_next[1, len(x) - 1] + (u_next[1, len(x) - 1] - u_next[1, len(x) - 2]) u = u_next #if (n % (N / 10) == 0): # plt.plot(x, u[0][:-1]) # plt.show() if __name__ == "__main__": rw.write_data(u, "u_lax_wendroff_x2.txt") a = rw.read_data("u_lax_wendroff_x2.txt") print(a) plt.plot(x,ref_sol[0]) plt.show()
u1 = q(n * k) * phi((m * h) - (L / 2)) u2 = ((V_ro(U[0, m]) - U[1, m]) / tau) return np.array([u1, u2]) def f_u(U, m): u1 = U[0, m] * U[1, m] u2 = 1 / 2 * (U[1, m]**2) + (c_0**2) * np.log(U[0, m]) return np.array([u1, u2]) a = 5 b = 12 error = np.zeros(b - a) k_vec = np.zeros(b - a) ref = rw.read_data("L_W_100_0.00001_200000") for i in range(a, b): # 16 k = 2**(-i) N = int(2 / k) u = np.zeros([2, len(x) + 1]) # 2 x M u_next = np.zeros([2, len(x) + 1]) u_half = np.zeros([2, len(x) + 1]) initial_velocity = V_ro(rho_up) u_half[0, :] = rho_up u_half[1, :] = initial_velocity u[0, :] = rho_up u[1, :] = initial_velocity u_next[0, :] = rho_up u_next[1, :] = initial_velocity
u2[i] = ((V_ro(U[0, m[i]]) - U[1, m[i]]) / tau) return np.array([u1, u2]) def f_u(U, m): u1 = U[0, m] * U[1, m] u2 = 1 / 2 * (U[1, m]**2) + (c_0**2) * np.log(U[0, m]) return np.array([u1, u2]) a = 9 b = 12 error = np.zeros(b - a) k_vec = np.zeros(b - a) ref = rw.read_data("ber1.txt") index = 0 for i in range(a, b): #16 k = 2**(-i) N = int(10 / k) u = np.zeros([2, len(x) + 1]) #2 x M u_next = np.zeros([2, len(x) + 1]) initial_velocity = V_ro(rho_up) u[0, :] = rho_up u[1, :] = initial_velocity u_next[0, :] = rho_up u_next[1, :] = initial_velocity for n in range(N): f = f_u(u, range(1, len(x) + 1)) s_next = s(u, range(1, len(x) + 1), n)
u2 = ((V_ro(U[0, m]) - U[1, m]) / tau) return np.array([u1, u2]) def f_u(U, m): u1 = U[0, m] * U[1, m] u2 = 1 / 2 * (U[1, m]**2) + (c_0**2) * np.log(U[0, m]) return np.array([u1, u2]) #Declarations for convergence analysis a = 6 b = 14 error = np.zeros(b - a) #To save difference in norm k_vec = np.zeros(b - a) #To save k-values ref = rw.read_data("L_W_ref_boundaries") #Load reference solution #Scale reference solution ref[0] = ref[0] / 1000 ref[1] = ref[1] / 0.06 for i in range(a, b): #Loop executing Lax-Wendroff with varying values of k k = 2**(-i) N = int(2 / k) u = np.zeros([2, len(x) + 1]) u_next = np.zeros([2, len(x) + 1]) u_half = np.zeros([2, len(x) + 1]) initial_velocity = V_ro(rho_up) u_half[0, :] = rho_up u_half[1, :] = initial_velocity