solve discrete poisson equation using sor in matlab

To solve the discrete Poisson equation using the Successive Over-Relaxation (SOR) method in MATLAB, you can follow these steps:

  1. Define the problem domain and discretize it into a grid.
  2. Set up the boundary conditions.
  3. Initialize the solution grid with initial values.
  4. Start an iterative loop until convergence criteria are met: a. Update the solution using the SOR formula. b. Check for convergence.
  5. Display or return the final solution.

Here is an example MATLAB code to solve the discrete Poisson equation using the SOR method:

main.m
% Parameters
N = 100;                % Number of grid points
L = 1.0;                % Length of the domain
dx = L / (N - 1);       % Grid spacing
omega = 1.8;            % Relaxation parameter
tolerance = 1e-6;       % Convergence tolerance
maxIterations = 1000;   % Maximum number of iterations

% Define the grid
x = linspace(0, L, N)';
y = linspace(0, L, N)';
[X, Y] = meshgrid(x, y);

% Set up the initial solution grid
phi = zeros(N, N);

% Set up the boundary conditions
phi(:, 1) = 1.0;    % Left boundary
phi(:, end) = 0.0;  % Right boundary
phi(1, :) = 0.0;    % Bottom boundary
phi(end, :) = 0.0;  % Top boundary

% Solve the Poisson equation using the SOR method
iterations = 0;
delta = tolerance + 1;
while delta > tolerance && iterations < maxIterations
    previousPhi = phi;
    for i = 2:N-1
        for j = 2:N-1
            phi(i, j) = (1 - omega) * phi(i, j) + (omega / (4 + dx^2)) * (phi(i+1, j) + phi(i-1, j) + phi(i, j+1) + phi(i, j-1));
        end
    end
    delta = max(abs(phi(:) - previousPhi(:)));  % Calculate the maximum change
    iterations = iterations + 1;
end

% Display the solution
figure;
imagesc(x, y, phi');
axis equal tight;
colorbar;
xlabel('x');
ylabel('y');
title('Solution of the Poisson Equation using SOR');
1257 chars
45 lines

In this code, the Poisson equation is discretized on a square domain with equal grid spacing along both x and y directions. The SOR method is used to update the solution grid until the convergence criterion is met.

Note that the omega parameter controls the relaxation rate, and its value can be adjusted to optimize convergence. Also, the tolerance parameter sets the maximum acceptable change in the solution between iterations for convergence.

You can modify the code as per your specific problem and requirements.

related categories

gistlibby LogSnag