def solve(n): # we have n+1 grid points h = (xmax-xmin)/n x = linspace(xmin,xmax,n+1) a = afun(x) # Right hand side b = 2.0 * h**2 * qfun(x) # First and last equations are bc b[0] = 0.0 b[-1] = 0.0; A,B,C = zeros(n+1), zeros(n+1), zeros(n+1) A[0],C[0] = 1.0, 0.0 for i in range(1,n): B[i] = -(a[i-1]+a[i]) A[i] = (a[i-1]+2*a[i]+a[i+1]) C[i] = -(a[i]+a[i+1]) B[-1],A[-1] = 0.0, 1.0 u = tdma(A,B,C,b) xe = linspace(xmin,xmax,200) ue = uexact(xe) plt.plot(xe,ue,'-',x,u,'o--') plt.title('FD using '+str(n+1)+' points') plt.xlabel('x'); plt.ylabel('u') plt.legend(('Exact','FD')) print "Close plot window to continue" plt.show() return h, abs(u-uexact(x)).max()
def solve(n): # we have n+1 grid points h = (xmax - xmin) / n x = linspace(xmin, xmax, n + 1) a = afun(x) # Right hand side b = 2.0 * h**2 * qfun(x) # First and last equations are bc b[0] = 0.0 b[-1] = 0.0 A, B, C = zeros(n + 1), zeros(n + 1), zeros(n + 1) A[0], C[0] = 1.0, 0.0 for i in range(1, n): B[i] = -(a[i - 1] + a[i]) A[i] = (a[i - 1] + 2 * a[i] + a[i + 1]) C[i] = -(a[i] + a[i + 1]) B[-1], A[-1] = 0.0, 1.0 u = tdma(A, B, C, b) xe = linspace(xmin, xmax, 200) ue = uexact(xe) plt.plot(xe, ue, '-', x, u, 'o--') plt.title('FD using ' + str(n + 1) + ' points') plt.xlabel('x') plt.ylabel('u') plt.legend(('Exact', 'FD')) print("Close plot window to continue") plt.show() return h, abs(u - uexact(x)).max()
def solve(Pe, xf): N = len(xf) - 1 # number of cells xc = 0.5 * (xf[0:-1] + xf[1:]) # cell centers hc = xf[1:] - xf[0:-1] # cell lenghts hf = empty_like(xf) # h_{j+1/2} hf[0] = hc[0] hf[1:-1] = 0.5 * (hc[0:-1] + hc[1:]) hf[-1] = hc[-1] # Mesh Peclet number P = hf * Pe # Three diagonals A, B, C = zeros(N), zeros(N), zeros(N) A[0], C[0] = 0.5 + 2.0 / P[0] + 1.0 / P[1], 0.5 - 1.0 / P[1] for i in range(1, N - 1): B[i], A[i], C[i] = -(0.5 + 1.0 / P[i]), ( 1.0 / P[i] + 1.0 / P[i + 1]), 0.5 - 1.0 / P[i + 1] B[-1], A[-1] = -(0.5 + 1.0 / P[-2]), -0.5 + 2.0 / P[-1] + 1.0 / P[-2] a, b = 0.0, 1.0 # Dirichlet condition # Right hand side F = zeros(N) F[0] = (1.0 + 2.0 / P[0]) * a F[-1] = (-1.0 + 2.0 / P[-1]) * b # Solve u = tdma(A, B, C, F) return xc, u
def error(n): xmin, xmax = 0.0, 2.0*pi h = (xmax - xmin)/(n - 1) x = linspace(xmin,xmax,n) u = zeros(n) # BC for first and last points u[0] = uexact(x[0]) u[-1] = uexact(x[-1]) b = h**2 * f(x[1:-1]) b[0] += u[0] b[-1] += u[-1] u[1:-1] = tdma(2*ones(n-2),-ones(n-2),-ones(n-2),b) return h, abs(uexact(x)-u).max()
for i in range(len(x)): if x[i] < 0.5: ue[i] = a + (cos(pi*x[i])-1)/pi else: ue[i] = a - 1.0/pi + 2.0*cos(pi*x[i])/pi return ue h = (xmax-xmin)/n x = linspace(xmin+0.5*h, xmax-0.5*h, n) q = qfun(x) q[0] += a/h A,B,C = zeros(n),zeros(n),zeros(n) A[0], C[0] = 0.5/h, 0.5/h for i in range(1,n-1): B[i], C[i] = -0.5/h, 0.5/h B[-1], A[-1] = -0.5/h, 0.5/h u = tdma(A,B,C,q) xe = linspace(xmin,xmax,100) ue = uexact(xe) plt.plot(x,u,'o--',xe,ue,'-') plt.xlabel('x') plt.ylabel('u') plt.legend(('Numerical','Exact')) plt.title('Central scheme') plt.show()
args = parser.parse_args() N, Pe = args.N, args.Pe a, b = 0.0, 1.0 h = (b - a) / N P = h * Pe A, B, C = zeros(N), zeros(N), zeros(N) A[0], C[0] = 1.0 + 6.0 / P, 1.0 - 2.0 / P for i in range(1, N - 1): B[i], A[i], C[i] = -(1.0 + 2.0 / P), 4.0 / P, 1.0 - 2.0 / P B[-1], A[-1] = -(1.0 + 2.0 / P), -1.0 + 6.0 / P F = zeros(N) F[0] = (2.0 + 4.0 / P) * a F[-1] = (-2.0 + 4.0 / P) * b u = tdma(A, B, C, F) x = linspace(a + 0.5 * h, b - 0.5 * h, N) xe = linspace(a, b, 100) ue = a + (b - a) * (exp((xe - 1) * Pe) - exp(-Pe)) / (1 - exp(-Pe)) plt.plot(x, u, 'o-', xe, ue) plt.xlabel('x') plt.ylabel('u') plt.legend(('Numerical', 'Exact'), loc='upper left') T = 'N=' + str(N) + ', Pe=' + str(Pe) + ', P=' + str(P) plt.title(T) plt.show()
for i in range(len(x)): if x[i] < 0.5: ue[i] = a + (cos(pi * x[i]) - 1) / pi else: ue[i] = a - 1.0 / pi + 2.0 * cos(pi * x[i]) / pi return ue h = (xmax - xmin) / n x = linspace(xmin + 0.5 * h, xmax - 0.5 * h, n) q = qfun(x) q[0] += a / h A, B, C = zeros(n), zeros(n), zeros(n) A[0], C[0] = 0.5 / h, 0.5 / h for i in range(1, n - 1): B[i], C[i] = -0.5 / h, 0.5 / h B[-1], A[-1] = -0.5 / h, 0.5 / h u = tdma(A, B, C, q) xe = linspace(xmin, xmax, 100) ue = uexact(xe) plt.plot(x, u, 'o--', xe, ue, '-') plt.xlabel('x') plt.ylabel('u') plt.legend(('Numerical', 'Exact')) plt.title('Central scheme') plt.show()
xmin, xmax = 0.0, 2.0 * pi # Grid of n points n = 20 h = (xmax - xmin) / (n - 1) x = linspace(xmin, xmax, n) # array for solution u = zeros(n) # BC for first and last points u[0] = uexact(x[0]) u[-1] = uexact(x[-1]) b = h**2 * f(x[1:-1]) b[0] += u[0] b[-1] += u[-1] u[1:-1] = tdma(2 * ones(n - 2), -ones(n - 2), -ones(n - 2), b) print("Max error = ", abs(uexact(x) - u).max()) # Exact solution on fine mesh for plotting xe = linspace(xmin, xmax, 100) ue = uexact(xe) # Plot exact and numerical solution plt.plot(xe, ue, x, u, 'o') plt.legend(('Exact solution', 'Numerical solution')) plt.xlabel('x') plt.ylabel('u') plt.show()
def f(x): return sin(x) # exact solution def uexact(x): return sin(x) xmin, xmax = 0.0, 2.0*pi n = 20 h = (xmax - xmin)/(n - 1) x = linspace(xmin,xmax,n) u = zeros(n) # BC for first and last points u[0] = uexact(x[0]) u[-1] = uexact(x[-1]) b = h**2 * f(x[1:-1]) b[0] += u[0] b[-1] += u[-1] u[1:-1] = tdma(2*ones(n-2),-ones(n-2),-ones(n-2),b) print "Max error = ", abs(uexact(x)-u).max() xe = linspace(xmin, xmax, 100); ue = uexact(xe) plt.plot(xe,ue,x,u,'o-') plt.legend(('Exact solution','Numerical solution')) plt.xlabel('x') plt.ylabel('u') plt.show()
h = (b - a) / N P = h * Pe # Construct diagonals of matrix A, B, C = zeros(N), zeros(N), zeros(N) A[0], C[0] = 1.0 + 2.0 / P, -1.0 / P for i in range(1, N - 1): B[i], A[i], C[i] = -(1.0 + 1.0 / P), 1.0 + 2.0 / P, -1.0 / P B[-1], A[-1] = -(1.0 + 1.0 / P), 1.0 + 3.0 / P # construct rhs F = zeros(N) F[0] = (1.0 + 1.0 / P) * a F[-1] = (2.0 / P) * b # solve u = tdma(A, B, C, F) # plot the solution x = linspace(a + 0.5 * h, b - 0.5 * h, N) xe = linspace(a, b, 100) ue = a + (b - a) * (exp((xe - 1) * Pe) - exp(-Pe)) / (1 - exp(-Pe)) plt.plot(x, u, "o-", xe, ue) plt.xlabel("x") plt.ylabel("u") plt.legend(("Numerical", "Exact"), loc="upper left") T = "N=" + str(N) + ", Pe=" + str(Pe) + ", P=" + str(P) plt.title(T) plt.show()