|
| 1 | +% ====================== Test 1 ===================== |
| 2 | +% 2D Laplace BVP: Dirichlet, Dirichlet |
| 3 | +% u_xx + u_yy = 0, 0 < x,y < pi, |
| 4 | +% BC: u(x,0) = e^x, u(x,pi) = - e^x, u(0,y) = cos(y), u(pi,y) = e^pi cos(y) |
| 5 | +% exact solution: u(x,y) = e^x cos(y) |
| 6 | +% =================================================== |
| 7 | +% example that uses addBC2D |
| 8 | +% |
| 9 | +close all; clc; |
| 10 | + |
| 11 | +addpath('../../src/matlab'); |
| 12 | + |
| 13 | +k = 2; |
| 14 | +bvp = 1; |
| 15 | +m = 99; % it should be odd |
| 16 | +n = m+2; % it should be odd |
| 17 | +dx = pi/m; |
| 18 | +dy = pi/n; |
| 19 | +% centers and vertices |
| 20 | +xc = [0 dx/2:dx:pi-dx/2 pi]'; |
| 21 | +yc = [0 dy/2:dy:pi-dy/2 pi]'; |
| 22 | +[Y,X] = meshgrid(yc,xc); |
| 23 | +% t = 'u_xx + u_yy = 0, (x,y) in [0,pi]x[0,pi], u(x,0) = e^x, u(x,pi) = - e^x, u(0,y) = cos(y), u(pi,y) = e^pi cos(y), with exact solution u(x,y) = e^x cos(y)'; |
| 24 | +ue = exp(X).*cos(Y); % exact solution |
| 25 | +dc = [1;1;1;1]; |
| 26 | +nc = [0;0;0;0]; |
| 27 | +bcl = squeeze(ue(1,:))'; % left bc (y increases) |
| 28 | +bcr = squeeze(ue(end,:))'; % right bc (y increases) |
| 29 | +bcb = squeeze(ue(:,1)); % bottom bc (x increases) |
| 30 | +bct = squeeze(ue(:,end)); % top bc (x increases) |
| 31 | +bcl = bcl(2:end-1,1); |
| 32 | +bcr = bcr(2:end-1,1); |
| 33 | +v = {bcl;bcr;bcb;bct}; |
| 34 | +A = - lap2D(k,m,dx,n,dy); |
| 35 | +b = zeros(m+2,n+2); |
| 36 | +b = reshape(b,[],1); |
| 37 | +[A0,b0] = addBC2D(A,b,k,m,dx,n,dy,dc,nc,v); |
| 38 | +ua = A0\b0; % approximate solution |
| 39 | +ua = reshape(ua,m+2,n+2); |
| 40 | + |
| 41 | +figure(bvp) |
| 42 | +surf(X,Y,ua); |
| 43 | +title('Approximate Solution: 2D Poisson with Periodic BC'); |
| 44 | +shading interp; |
| 45 | +figure(bvp+10) |
| 46 | +surf(X,Y,ue); |
| 47 | +title('Exact Solution: 2D Poisson with Periodic BC'); |
| 48 | +shading interp; |
| 49 | + |
| 50 | +fprintf('Maximum error: %.4f\n', max(max(abs(ue-ua)))) |
| 51 | +fprintf('Relative error: %.4f%%\n', 100*max(max(abs(ue-ua)))/(max(max(ue)) - min(min(ue)))) |
0 commit comments