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