# SIMPLE algorithm – Implementation

Today we show in short, how to implement the whole SIMPLE (Semi Implicit Method for Pressure Linked Equations) algorithm to solve a stationary, incompressible fluid flow problem for finite volume or finite element discretization. We do not explain the whole algorithm here, but a very detailed description is given in pdf tutorial for this CFD code. You can get free access and download the whole documentation and tutorial files from this homepage.

```function u = simpleAlgorithm(SM, F, x0, nVnodes)

% Let us first split the whole SM matrix into A, B and C matrices
A = SM(1:2*nVnodes,1:2*nVnodes);
B = SM(1:2*nVnodes,2*nVnodes+1:end);
C = SM(2*nVnodes+1:end,1:2*nVnodes);

% Now, retrieve RHS vector of the momentum equations and initial guess
% of the pressure field
Fu = F(1:2*nVnodes);
p_star = x0(2*nVnodes+1:end);

% Prepare also other matrices that will be needed in the algorithm and
% do not change from one iteration to another in case of a Stokes
% problem
D = diag(A);
invD = diag(1./D);
CinvDB = C*invD*B;

% Define underrelaxation parameter
alpha_p = 0.02;

% Start iteration loop
for iter = 1:100
u_star = A\(Fu-B*p_star);
p_prime = CinvDB\(C*u_star);
p = p_star + alpha_p*p_prime;
p_star = p;
end

% Output final solution
u = [u_star; p];

% Close function
end```

You can solve the whole example in MATLAB using our free QuickerSim CFD Toolbox.