Skip to content

Commit 4f20b73

Browse files
committed
First commit, adding files
0 parents  commit 4f20b73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+5513
-0
lines changed

FiniteDifferenceMethods/.gitkeep

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
% Define the parameters
2+
L = 1; % Length of the rod
3+
T = 1; % Total time
4+
N = 100; % Number of spatial grid points
5+
alpha = 0.02; % Thermal diffusivity constant
6+
7+
for k=1:5
8+
% Refine spacial resolution
9+
if k > 1
10+
N = N*2;
11+
end
12+
13+
dx = L/N;
14+
dt = 0.5*dx*dx/alpha;
15+
16+
M = int32(T/dt);
17+
18+
x = linspace(0,L,N+1);
19+
20+
% Initialize the temperature matrix
21+
u = zeros(N+1,M+1);
22+
23+
% Set the initial condition
24+
u(:,1) = sin(pi*x);
25+
26+
time = 0.0;
27+
toatlError(:) = 0;
28+
29+
for j = 1:M
30+
for i = 2:N
31+
u(i,j+1) = u(i,j) + alpha*dt/dx^2 *(u(i+1,j) - 2*u(i,j) + u(i-1,j));
32+
end
33+
34+
time = time + dt;
35+
exact = sin(pi*x)*exp(-alpha*(pi)^2*time);
36+
37+
Error = exact' - u(:,j);
38+
totalError(j) = sum(Error);
39+
40+
end
41+
% Error
42+
E(k) = norm(totalError,inf);
43+
DX(k) = dx;
44+
end
45+
46+
%%
47+
figure
48+
plot(DX,E,'o-')
49+
xlabel("Delta x")
50+
ylabel("Error")
51+
142 KB
Binary file not shown.

FiniteDifferenceMethods/exercise/.gitkeep

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
% Define the parameters
2+
L = 2*pi; % Length
3+
T = 3; % Total time
4+
N = 100; % Number of spatial grid points
5+
a = 1; % Wave speed
6+
7+
% Spatial discretization
8+
dx = L/N;
9+
10+
% Stability condition
11+
dt = abs(dx/a)*0.5;
12+
13+
% Number of iterations
14+
M = int32(T/dt);
15+
16+
% Spatial grid
17+
x = linspace(0,L,N+1);
18+
19+
% Initialize the matrix
20+
u = zeros(N+1,1);
21+
22+
% Set the initial condition
23+
u(:,1) = sin(x);
24+
%%
25+
26+
u_old = u;
27+
u_older = u;
28+
29+
% Apply the finite difference method
30+
for i = 1:M
31+
% Write your finite difference scheme here
32+
% Don't forget about the boundary conditions
33+
% And carrying three time levels of the solution
34+
35+
36+
37+
38+
39+
40+
end
41+
42+
figure
43+
plot(x,u)
44+
xlabel("Position")
45+
ylabel("U")
46+
ylim([-1 1])
47+
xlim([0 2*pi])
48+
set(gcf,'Position',[400 400 800 380])
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
% Points in the domain
2+
nx = 50;
3+
ny = 50;
4+
5+
% Initializing solution
6+
U_init = zeros(ny,nx);
7+
8+
% Boundary conditions
9+
U_init(:,1) = 0;
10+
U_init(:,nx) = 0;
11+
U_init(1,:) = 0;
12+
U_init(ny,:) = 0;
13+
14+
% Corners
15+
U_init(ny,nx) = 0.5*(U_init(ny-1,nx) + U_init(ny,nx-1));
16+
U_init(ny,1) = 0.5*(U_init(ny-1,1) + U_init(ny,2));
17+
U_init(1,nx) = 0.5*(U_init(2,nx) + U_init(1,nx-1));
18+
U_init(1,1) = 0.5*(U_init(1,2) + U_init(2,1));
19+
20+
U_original = U_init;
21+
U_new = U_init;
22+
23+
% Optimization parameter
24+
omega = 2/(1 + (pi/nx));
25+
26+
% Initialize error and iteration count
27+
maxError = 1;
28+
iterations = 0;
29+
30+
% Running 3 cases,1 for each method
31+
numCases = 0;
32+
33+
% Start big loop
34+
while numCases < 3
35+
while maxError > 10^-3
36+
37+
for j=2:ny-1
38+
for i=2:nx-1
39+
% Jacobi method
40+
if numCases == 0
41+
U_new(j,i) = 0.25*(U_init(j+1,i) + U_init(j-1,i) + U_init(j,i+1) + U_init(j,i-1));
42+
end
43+
44+
% Gauss-Seidel
45+
if numCases == 1
46+
U_new(j,i) = 0.25*(U_new(j+1,i) + U_new(j-1,i) + U_new(j,i+1) + U_new(j,i-1));
47+
end
48+
49+
% SOR
50+
if numCases == 2
51+
U_new(j,i) = (1-omega)*U_init(j,i) + (0.25*omega)*(U_new(j+1,i) + U_new(j-1,i) + U_new(j,i+1) + U_new(j,i-1));
52+
end
53+
end
54+
end
55+
% Calculate the error
56+
57+
% Stopping criteria 1
58+
error = U_init - U_new;
59+
maxError = max(max(abs(error)));
60+
61+
% Stopping criteria 2
62+
% error = 0;
63+
% for j=2:ny-1
64+
% for i=2:nx-1
65+
% error = error + 0.25*(U_new(j+1,i) + U_new(j-1,i) + U_new(j,i+1) + U_new(j,i-1)) - U_new(j,i);
66+
% end
67+
% end
68+
% maxError = abs(error);
69+
70+
% Set U for next iteration
71+
U_init = U_new;
72+
73+
% Keep track of each iteration
74+
iterations = iterations + 1;
75+
end
76+
caseIterations(numCases+1) = iterations;
77+
numCases = numCases + 1;
78+
79+
U_solution = U_new;
80+
81+
% Reset conditions
82+
U_init = U_original;
83+
U_new = U_init;
84+
maxError = 1;
85+
iterations = 0;
86+
end
87+
88+
% Plot the solution
89+
figure
90+
contourf(U_solution)
91+
title("Laplace equation solution")
92+
xlabel("x")
93+
ylabel("y")
94+
colorbar
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
% Domain setup
2+
L = 1;
3+
H = 1;
4+
5+
% Points in the domain
6+
nx = 22;
7+
ny = 22;
8+
9+
% Grid resolution
10+
dx = L/nx;
11+
dy = H/ny;
12+
13+
% Initializing solution
14+
U_init = zeros(ny,nx);
15+
16+
% Boundary conditions
17+
U_init(:,1) = 10;
18+
U_init(:,nx) = 5;
19+
U_init(1,:) = 4;
20+
U_init(ny,:) = 1;
21+
22+
U_sol = U_init;
23+
24+
% Build the coefficient matrix
25+
% Use MATLAB function spdiag() and feed in matrix coefficients
26+
n = nx-2;
27+
e = ones(n,1);
28+
T = ;
29+
30+
% Kronecker Delta product
31+
% Use MATLAb function kron, and take the prudct of I(x)T + T(x)I
32+
A = ;
33+
34+
% Build the right hand side
35+
% This matrix will contain your boundary conditions and is equal to
36+
% the negative value of your stencil.
37+
for j=2:n+1
38+
for i=2:n+1
39+
rhs(j-1,i-1) = ;
40+
end
41+
end
42+
43+
% Reshape the rhs to a 1D vector
44+
% Use MATLAB reshape() to [n*n, 1]
45+
46+
47+
% Solve the equation x = A \ rhs
48+
49+
50+
% Reshape the matrix back to a n by n grid
51+
52+
53+
% Add back in the boundary conditions
54+
for j=2:n+1
55+
for i=2:n+1
56+
57+
end
58+
end
59+
60+
%% Plot the solution
61+
figure
62+
contourf(U_sol)
63+
title("Laplace equation solution")
64+
xlabel("x")
65+
ylabel("y")
66+
colorbar
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
% Domain setup
2+
L = 2;
3+
H = 1;
4+
5+
% Points in the domain
6+
nx = 44;
7+
ny = 22;
8+
9+
% Grid resolution
10+
dx = L/nx;
11+
dy = H/ny;
12+
13+
% Initializing solution
14+
U_init = zeros (ny,nx);
15+
16+
% Boundary conditions
17+
U_init(:,1) = 10;
18+
U_init(:,nx) = 5;
19+
U_init(1,:) = 4;
20+
U_init(ny,:) = 1;
21+
22+
U_sol = U_init;
23+
24+
% Build the coefficient matrix
25+
n_x = nx-2;
26+
n_y = ny-2;
27+
28+
ex = ones(n_x,1);
29+
ey = ones(n_y,1);
30+
31+
% Need Tx and Ty individually now
32+
Tx = ;
33+
Ty = ;
34+
35+
% Kronecker Delta product
36+
% Product between I_x(x)Ty + Tx(x)I_y
37+
A = ;
38+
39+
% Build the right hand side
40+
for j=2:n_y+1
41+
for i=2:n_x+1
42+
rhs(j-1,i-1) = ;
43+
end
44+
end
45+
46+
% Reshape the rhs to a 1D vector
47+
48+
49+
% Solve the equation x = A \ rhs
50+
x = A\rhs;
51+
52+
% Reshape the matrix back to a n by n grid
53+
54+
55+
% Add back in the boundary conditions
56+
for j=2:n_y+1
57+
for i=2:n_x+1
58+
59+
end
60+
end
61+
62+
%% Plot the solution
63+
figure
64+
contourf(U_sol)
65+
title("Laplace equation solution")
66+
xlabel("x")
67+
ylabel("y")
68+
colorbar
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
% Define the parameters
2+
L = 1; % Length of the rod
3+
T = 1; % Total time
4+
N = 100; % Number of spatial grid points
5+
alpha = 0.02; % Thermal diffusivity constant
6+
7+
for k=1:5
8+
% Refine spatial resolution
9+
if k > 1
10+
N = N*2;
11+
end
12+
13+
dx = L/N;
14+
dt = 0.5*dx*dx/alpha;
15+
16+
M = int32(T/dt);
17+
18+
x = linspace(0,L,N+1);
19+
20+
% Initialize the temperature matrix
21+
u = zeros(N+1,M+1);
22+
23+
% Set the initial condition
24+
u(:,1) = sin(pi*x);
25+
26+
time = 0.0;
27+
28+
for j = 1:M
29+
for i = 2:N
30+
u(i,j+1) = u(i,j) + alpha*dt/dx^2 * (u(i+1,j) - 2*u(i,j) + u(i-1,j));
31+
end
32+
33+
time = time + dt;
34+
exact = sin(pi*x)*exp(-alpha*(pi)^2*time);
35+
36+
Error = exact' - u(:,j);
37+
totalError(j) = sum(Error);
38+
39+
end
40+
41+
% Error
42+
E(k) = norm(totalError,inf);
43+
end
44+
45+
%%
46+
figure
47+
plot(E,'o-')
48+
xlabel("Delta x")
49+
ylabel("Error")

FiniteDifferenceMethods/exercise/solution/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)