|
| 1 | +% ====================== Test 2 ===================== |
| 2 | +% 3D Poisson BVP: Robin (on left and right faces), Periodic, Periodic BC |
| 3 | +% -(u_xx + u_yy + u_zz) = - f(x,y,z), -1 < x < 1, 0 < y,z < 2 pi |
| 4 | +% - f(x,y,z) = 16(1-x^2)cos(4x) - 16x sin(4x) + 2(cos(4x)+sin(2y)+sin(4z)) + (1-x^2)(4 sin(2y) + 16 sin(4z)) |
| 5 | +% % f(x,y,z) = 16(1-x^2)cos(4x) - 16x sin(4x) + 2 cos(4x) + (1-x^2)(4 sin(2y) + 16 sin(4z)) |
| 6 | +% BC: u(-1,y,z) = 0, u(1,x,y) = 0, u(x,0,z) = u(x,2 pi,z), u_y(x,0,z) = u_y(x,2 pi,z), u(x,y,0) = u(x,y,2 pi), u_z(x,y,0) = u_z(x,y,2 pi) |
| 7 | +% exact solution: (cos(4x)+sin(2y)+sin(4z))*(1-x^2) |
| 8 | +% =================================================== |
| 9 | +% example that uses addBC3D |
| 10 | +% |
| 11 | +close all; clc; |
| 12 | + |
| 13 | +addpath('../../src/matlab'); |
| 14 | + |
| 15 | +k = 2; |
| 16 | +bvp = 2; |
| 17 | +m = 39; % should be odd to be able to plot the middle slice |
| 18 | +n = m+2; % should be odd to be able to plot the middle slice |
| 19 | +o = m+4; % should be odd to be able to plot the middle slice |
| 20 | +dx = 2/m; |
| 21 | +dy = 2*pi/n; |
| 22 | +dz = 2*pi/o; |
| 23 | +% centers and vertices |
| 24 | +xc = [-1 -1+dx/2:dx:1-dx/2 1]'; |
| 25 | +yc = [0 dy/2:dy:2*pi-dy/2 2*pi]'; |
| 26 | +zc = [0 dz/2:dz:2*pi-dz/2 2*pi]'; |
| 27 | +[Y,X,Z] = meshgrid(yc,xc,zc); |
| 28 | +t = 'u_xx + u_yy + u_zz = f(x,y,z), -1 < x < 1, 0 < y,z < 2 pi, u(-1,y,z) = 0, u(1,y,z) = 0, periodic BC on y,z, with exact solution u(x,y,z) = (cos(4x)+sin(2y)+sin(4z))*(1-x^2)'; |
| 29 | +ue = (cos(4*X)+sin(2*Y)+sin(4*Z)).*(1-X.^2); |
| 30 | +dc = [0;0;0;0;0;0]; |
| 31 | +nc = [1;1;0;0;0;0]; |
| 32 | +bcl = -2*(cos(4)+sin(2*Y)+sin(4*Z)); |
| 33 | +bcr = -2*(cos(4)+sin(2*Y)+sin(4*Z)); |
| 34 | +bcl = squeeze(bcl(1,:,:)); % left bc (y increase along rows, z increase along cols) |
| 35 | +bcr = squeeze(bcr(end,:,:)); % right bc (y increase along rows, z increase along cols) |
| 36 | +bcl = reshape(bcl(2:end-1,2:end-1),[],1); |
| 37 | +bcr = reshape(bcr(2:end-1,2:end-1),[],1); |
| 38 | +bcb = zeros((m+2)*o,1); |
| 39 | +bct = zeros((m+2)*o,1); |
| 40 | +bcf = zeros((n+2)*(m+2),1); |
| 41 | +bcz = zeros((n+2)*(m+2),1); |
| 42 | +v = {bcl;bcr;bcb;bct;bcf;bcz}; |
| 43 | +A = - lap3D(k,m,dx,n,dy,o,dz); |
| 44 | +b = 16*(1-X.^2).*cos(4*X) - 16*X.*sin(4*X) + 2*(cos(4*X)+sin(2*Y)+sin(4*Z)) + (1-X.^2).*(4*sin(2*Y) + 16*sin(4*Z)); |
| 45 | +b = reshape(b,[],1); |
| 46 | +[A0,b0] = addBC3D(A,b,k,m,dx,n,dy,o,dz,dc,nc,v); |
| 47 | +ua = A0\b0; % approximate solution |
| 48 | +ua = reshape(ua,m+2,n+2,o+2); |
| 49 | +ua = ua - ua(1) + ue(1); |
| 50 | +% ua = ua - ua((m+1)/2,(n+3)/2,(o+3)/2); |
| 51 | + |
| 52 | + |
| 53 | +% plot slices as surfaces |
| 54 | +figure(bvp) |
| 55 | +surf(squeeze(Y((m+3)/2,:,:)),squeeze(Z((m+3)/2,:,:)),squeeze(ua((m+3)/2,:,:))); |
| 56 | +title('Approximate Solution: 3D Poisson with Periodic BC (Middle YZ slice)'); |
| 57 | +xlabel('Y'); |
| 58 | +ylabel('Z'); |
| 59 | +shading interp; |
| 60 | +figure(bvp+10) |
| 61 | +surf(squeeze(Y((m+3)/2,:,:)),squeeze(Z((m+3)/2,:,:)),squeeze(ue((m+3)/2,:,:))); |
| 62 | +title('Exact Solution: 3D Poisson with Periodic BC (Middle YZ slice)'); |
| 63 | +xlabel('Y'); |
| 64 | +ylabel('Z'); |
| 65 | +shading interp; |
| 66 | +figure(bvp+20) |
| 67 | +surf(squeeze(X(:,(n+3)/2,:)),squeeze(Z(:,(n+3)/2,:)),squeeze(ua(:,(n+3)/2,:))); |
| 68 | +title('Approximate Solution: 3D Poisson with Periodic BC (Middle XZ slice)'); |
| 69 | +xlabel('X'); |
| 70 | +ylabel('Z'); |
| 71 | +shading interp; |
| 72 | +figure(bvp+30) |
| 73 | +surf(squeeze(X(:,(n+3)/2,:)),squeeze(Z(:,(n+3)/2,:)),squeeze(ue(:,(n+3)/2,:))); |
| 74 | +title('Exact Solution: 3D Poisson with Periodic BC (Middle XZ slice)'); |
| 75 | +xlabel('X'); |
| 76 | +ylabel('Z'); |
| 77 | +shading interp; |
| 78 | +figure(bvp+40) |
| 79 | +surf(squeeze(X(:,:,(o+3)/2)),squeeze(Y(:,:,(o+3)/2)),squeeze(ua(:,:,(o+3)/2))); |
| 80 | +title('Approximate Solution: 3D Poisson with Periodic BC (Middle XY slice)'); |
| 81 | +xlabel('X'); |
| 82 | +ylabel('Y'); |
| 83 | +shading interp; |
| 84 | +figure(bvp+50) |
| 85 | +surf(squeeze(X(:,:,(o+3)/2)),squeeze(Y(:,:,(o+3)/2)),squeeze(ue(:,:,(o+3)/2))); |
| 86 | +title('Exact Solution: 3D Poisson with Periodic BC (Middle XY slice)'); |
| 87 | +xlabel('X'); |
| 88 | +ylabel('Y'); |
| 89 | +shading interp; |
| 90 | + |
| 91 | +fprintf('Maximum error: %.4f\n', max(max(max(abs(ue-ua))))) |
| 92 | +fprintf('Relative error: %.4f%%\n', 100*max(max(max(abs(ue-ua))))/(max(max(max(ue))) - min(min(min(ue))))) |
0 commit comments