-
Notifications
You must be signed in to change notification settings - Fork 0
/
Laplace.m
49 lines (43 loc) · 1.52 KB
/
Laplace.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
% Solution of Laplace's Equation using Finite Difference Method
% See Documentation to clearly understand problem
% Parameters
v1 = 10.0; % Potential on the bottom edge
v2 = 100.0; % Potential on the right edge
v3 = 40.0; % Potential on the top edge
v4 = 0.0; % Potential on the left edge
ni = 100; % Number of iterations
nx = 16; % Number of X grid points
ny = 11; % Number of Y grid points
% Initialize the potential grid with zeroes
phi = zeros(nx, ny);
% Set fixed potentials on the grid boundaries
phi(2:nx-1, 1) = v1; % Bottom edge
phi(2:nx-1, ny) = v3; % Top edge
phi(1, 2:ny-1) = v4; % Left edge
phi(nx, 2:ny-1) = v2; % Right edge
% Set corner potentials as the average of adjacent edges
phi(1, 1) = 0.5 * (v1 + v4); % Bottom-left corner
phi(nx, 1) = 0.5 * (v1 + v2); % Bottom-right corner
phi(1, ny) = 0.5 * (v3 + v4); % Top-left corner
phi(nx, ny) = 0.5 * (v2 + v3); % Top-right corner
% Perform the finite difference method iterations
for k = 1:ni
for i = 2:nx-1
for j = 2:ny-1
phi(i, j) = 0.25 * (phi(i+1, j) + phi(i-1, j) + phi(i, j+1) + phi(i, j-1));
end
end
end
% Visualization of the potential field
figure;
[X, Y] = meshgrid(1:ny, 1:nx);
surf(X, Y, phi, 'EdgeColor', 'none');
title('Solution of Laplace''s Equation', 'FontSize', 16, 'Color', 'b');
xlabel('X', 'FontSize', 14, 'Color', 'r');
ylabel('Y', 'FontSize', 14, 'Color', 'r');
zlabel('\phi (Potential)', 'FontSize', 14, 'Color', 'r');
colorbar;
colormap(jet);
view(45, 45);
grid on;
axis tight; % Adjust the axis limits to fit the data