import numpy as np import sympy as sp import math #define 时间步长空间步长 time_1 = 0.25 space_1 = 0.25 ht1 = int(1 / time_1) hs1 = int(1 / space_1) ht = ht1 + 1 hs = hs1 + 1 #定义出边界条件对应的函数并且把他的值放到数组里面去 x = sp.symbols("x") y = sp.symbols("y") t = sp.symbols("t") def u_text(x,y,t): return 20 + 80 * (y - np.exp(-0.5*math.pi*math.pi*t)*np.sin(math.pi/2*y)*np.sin(math.pi/2*x)) def u_t0(x,y,t): return 0 def u_x0(x,y,t): return 20 + 80 * y def u_x1(x,y,t): return 20 + 80 * (y - np.exp(-0.5*math.pi*math.pi*t)*np.sin(math.pi/2*y)) def u_y0(x,y,t): return 20 def u_y1(x,y,t): return 20 + 80 * (1 - np.exp(-0.5*math.pi*math.pi*t)*np.sin(math.pi/2*x)) u = np.zeros((ht, hs, hs)) u_cen = np.zeros((ht1, hs, hs)) u_1 = np.zeros((ht, hs, hs))#测试数组 #测试数组值 for i in range(ht): for h in range(hs): for k in range(hs): u_1[i][h][k] = u_text(h*space_1,k*space_1,i*time_1) print(u_1) #边值条件放进数组中 for i in range(ht): for j in range(hs): u[i][hs-1][j] = u_x1(j*space_1, j*space_1, i*time_1) u[i][j][hs-1] = u_y1(j*space_1, j*space_1, i*time_1) u[i][0][j] = u_x0(0, j*space_1, i*time_1) u[:, :, 0] = 20 #print(u) #ADI格式求解 #先对中间值的边界条件确定 aerf_x = time_1 / (2 * space_1 * space_1) aerf_y = time_1 / (2 * space_1 * space_1) for i in range(ht1): for j in range(hs): for k in range(hs-2): if j == 0 or j == hs1: k = k + 1 u_cen[i][j][k]=u[i][j][k]/2+u[i+1][j][k]/2-aerf_y*(u[i+1][j][k+1] -2*u[i+1][j][k]+u[i+1][j][k-1]-u[i][j][k+1]+2*u[i][j][k]-u[i][j][k-1])/4 #print(u_cen) #追赶法求解矩阵 left = np.zeros(ht-1) m1 = np.zeros(ht-1) m2 = -(2*aerf_x + 1) m3 = aerf_x m1[0] = m3 for t in range(ht1): for j in range(hs1-1): j = j+1 m2 = -(2 * aerf_x + 1) for i in range(hs1-1): i = i+1 left[i] = (2*aerf_y-1)*u[t][i][j]-aerf_y*(u[t][i][j+1]+u[t][i][j-1]) + left[i-1]*(-aerf_x/m2) if i >= 2: m2 = m3 + m3*(-m3/m2) m1[i] = m1[i-1]*(-m3/m2) for k in range(hs1-1): k1 = hs1-1-k u_cen[t][k1][j] = (left[k1] - aerf_x * u_cen[t][k1 + 1][j]) / m2-u_cen[t][0][j]*m1[k1]/m2 m2 = -(2 * aerf_x + 1) for i in range(hs1-1): i = i+1 left[i] = (2*aerf_y-1)*u_cen[t][i][j]-aerf_y*(u_cen[t][i][j+1]+u_cen[t][i][j-1]) + left[i-1]*(-aerf_x/m2) if i >= 2: m2 = m2 + m3*(-m3/m2) m1[i] = m1[i-1]*(-m3/m2) for k in range(hs1-1): k1 = hs1-1-k u[t+1][k1][j] = (left[k1] - aerf_x * u[t+1][k1 + 1][j]) / m2-u[t+1][0][j]*m1[k1]/m2 #print(u_cen) print(u)这个代码后面数组输出为什么和前面不同
时间: 2023-06-12 14:04:43 浏览: 132
这这是这是Python这是Python中这是Python中导这是Python中导入这是Python中导入三这是Python中导入三个这是Python中导入三个库这是Python中导入三个库:这是Python中导入三个库:numpy这是Python中导入三个库:numpy、这是Python中导入三个库:numpy、sy这是Python中导入三个库:numpy、symp这是Python中导入三个库:numpy、sympy这是Python中导入三个库:numpy、sympy和这是Python中导入三个库:numpy、sympy和math这是Python中导入三个库:numpy、sympy和math。
阅读全文