Skip to content

Commit 61dc2d3

Browse files
committed
new examples and name typos
Corrected function names and added four examples: two are old ones written with addBC instead of robinBC, one 1D wave equation using addBC and one 3D with Neumann and periodic BC
1 parent a74cb9f commit 61dc2d3

13 files changed

Lines changed: 255 additions & 15 deletions

examples/matlab/elliptic1DaddBC.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
% Solves the 1D Poisson's equation with Robin boundary conditions
2+
3+
clc
4+
close all
5+
6+
addpath('../../src/matlab')
7+
8+
west = 0; % Domain's limits
9+
east = 1;
10+
11+
k = 6; % Operator's order of accuracy
12+
m = 2*k+1; % Minimum number of cells to attain the desired accuracy
13+
dx = (east-west)/m; % Step length
14+
grid = [west west+dx/2 : dx : east-dx/2 east]; % grid
15+
16+
% Robin boundary conditions
17+
dc = [1;1];
18+
nc = [1;1];
19+
v = [0;2*exp(1)];
20+
21+
L = lap(k, m, dx); % 1D Mimetic laplacian operator
22+
U = exp(grid)'; % RHS
23+
[L0,U0] = addBC1D(L,U,k,m,dx,dc,nc,v); % add BC to system
24+
U0 = L0\U0; % Solve a linear system of equations
25+
26+
% Plot result
27+
plot(grid, U0, 'o')
28+
hold on
29+
plot(grid, exp(grid))
30+
legend('Approximated', 'Analytical', 'Location', 'NorthWest')
31+
title('Poisson''s equation with Robin BC')
32+
xlabel('x')
33+
ylabel('u(x)')

examples/matlab/elliptic2DXDirichletYDirichlet.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
addpath('../../src/matlab');
1212

1313
k = 2;
14-
1514
bvp = 1;
1615
m = 99; % it should be odd
1716
n = m+2; % it should be odd

examples/matlab/elliptic3DXDirichletYDirichletZDirichlet.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
addpath('../../src/matlab');
1212

1313
k = 2;
14-
1514
bvp = 1;
1615
m = 29; % should be odd to be able to plot the middle slice
1716
n = m+2; % should be odd to be able to plot the middle slice
@@ -50,6 +49,7 @@
5049
[A0,b0] = addBC3D(A,b,k,m,dx,n,dy,o,dz,dc,nc,v);
5150
ua = A0\b0; % approximate solution
5251
ua = reshape(ua,m+2,n+2,o+2);
52+
ua = ua - ua((m+1)/2,(n+3)/2,(o+3)/2);
5353

5454
% plot slices as surfaces
5555
figure(bvp)

examples/matlab/elliptic3DXDirichletYPeriodicZPeriodic.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
addpath('../../src/matlab');
1414

1515
k = 2;
16-
1716
bvp = 2;
1817
m = 49; % should be odd to be able to plot the middle slice
1918
n = m+2; % should be odd to be able to plot the middle slice
@@ -43,7 +42,6 @@
4342
[A0,b0] = addBC3D(A,b,k,m,dx,n,dy,o,dz,dc,nc,v);
4443
ua = A0\b0; % approximate solution
4544
ua = reshape(ua,m+2,n+2,o+2);
46-
% addBC3DSurfacePlots(X,Y,Z,m,n,o,ua,ue,bvp);
4745

4846

4947
% plot slices as surfaces
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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)))))

examples/matlab/elliptic3DaddBC.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
% 3D Staggering example using a 3D Mimetic laplacian
2+
3+
clc
4+
close all
5+
6+
addpath('../../src/matlab')
7+
8+
k = 2; % Order of accuracy
9+
m = 5; % -> 7
10+
n = 6; % -> 8
11+
o = 7; % -> 9
12+
13+
% boundary type
14+
dc = [1;1;1;1;1;1];
15+
nc = [0;0;0;0;0;0];
16+
bcl = zeros(n*o,1);
17+
bcr = zeros(n*o,1);
18+
bcb = zeros((m+2)*o,1);
19+
bct = zeros((m+2)*o,1);
20+
bcf = 100*ones((n+2)*(m+2),1); % Dirichlet boundary condition
21+
bcz = zeros((n+2)*(m+2),1);
22+
v = {bcl;bcr;bcb;bct;bcf;bcz};
23+
24+
L = lap3D(k, m, 1, n, 1, o, 1); % 3D Mimetic laplacian operator
25+
RHS = zeros(m+2, n+2, o+2);
26+
RHS = reshape(RHS, [], 1);
27+
28+
[L0, RHS0] = addBC3D(L,RHS,k,m,1,n,1,o,1,dc,nc,v); % add BC to linear system
29+
SOL = L0\RHS0;
30+
SOL = reshape(SOL, m+2, n+2, o+2);
31+
32+
p = 2; % Page to be displayed
33+
34+
page = SOL(:, :, p);
35+
36+
imagesc(page)
37+
title([num2str(p) ' page'])
38+
xlabel('m')
39+
ylabel('n')
40+
set(gca, 'YDir', 'Normal')
41+
colorbar

examples/matlab/sturmLiouvilleBessel.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
nc = [0;0];
2323
v = [0;besselj(3,1)];
2424
G = grad(k,m,dx);
25-
I = interpolFacesToStaggered1D(k,m);
25+
I = interpolFacesToCentersG1D(k,m);
2626
A = sparse(diag(xc.^2)*lap(k,m,dx) + diag(xc)*I*G + diag(xc.^2 - 9)*speye(m+2,m+2)); % nu = 3
2727
b = zeros(size(A,2),1);
2828
[A0,b0] = addBC1D(A,b,k,m,dx,dc,nc,v);

examples/matlab/sturmLiouvilleChebyshev.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
nc = [0;0];
2222
v = [1;1];
2323
G = grad(k,m,dx);
24-
I = interpolFacesToStaggered1D(k,m);
24+
I = interpolFacesToCentersG1D(k,m);
2525
A = sparse(diag(1-xc.^2)*lap(k,m,dx) - diag(xc)*I*G) + 4*speye(m+2,m+2); % n = 2
2626
b = zeros(size(A,2),1);
2727
[A0,b0] = addBC1D(A,b,k,m,dx,dc,nc,v);

examples/matlab/sturmLiouvilleHermite.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
% v = [hermiteH(4,-1);hermiteH(4,1)];
2525
v = [-20; -20];
2626
G = grad(k,m,dx);
27-
I = interpolFacesToStaggered1D(k,m);
27+
I = interpolFacesToCentersG1D(k,m);
2828
A = lap(k,m,dx) - 2*sparse(diag(xc)*I*G) + 8*speye(m+2,m+2); % m = 4
2929
b = zeros(size(A,2),1);
3030
[A0,b0] = addBC1D(A,b,k,m,dx,dc,nc,v);

examples/matlab/sturmLiouvilleLaguerre.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
% v = [laguerreL(4,0);laguerreL(4,2)];
3131
v = [1; 0.333333333333333];
3232
G = grad(k,m,dx);
33-
I = interpolFacesToStaggered1D(k,m);
33+
I = interpolFacesToCentersG1D(k,m);
3434
A = sparse(diag(xc)*lap(k,m,dx) + diag(1-xc)*I*G) + 4*speye(m+2,m+2); % n = 4
3535
b = zeros(size(A,2),1);
3636
[A0,b0] = addBC1D(A,b,k,m,dx,dc,nc,v);

0 commit comments

Comments
 (0)