Pentadiagonal matrix to solve for Pressure variable
Show older comments
Hello everyone,
I want to solve my pressure equation implicitly by pentadiagonal matrix method. Here is the following equation.

Eq. (1) is the linear system of equaion with 5 pressure unknowns. For 3 x 4 = 12 cells, considering
and
as 1, the matrix can be determined as shown below, representing Pentadiagonal matrix.

Thus, in order to represent this pentagonal matrix in MATLAB, for-loop should be used.
% for internal cells excluding boundaries
for i=2:Ny-1
for j=2:Nx-1
U(i,j) = 1;
V(i,j) = -4;
W(i,j) = 1;
X(i,j) = 1;
Y(i,j) = 1;
end
end
% for left boundary
//
% for right boundary
//
% for top boundary
//
% for right boundary
//
In case of tri-diagonal matrix (1D problem), the algorithm is based on forward elimination and backward substitution to calculate the unknown variable, as shown below. Link: https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
%TriDiagonal_Matrix_Algorithm(N%, A#(), B#(), C#(), D#(), X#())
for i = 2:N
W = A(i) / B(i - 1)
B(i) = B(i) - W * C(i - 1)
D(i) = D(i) - W * D(i - 1)
end
for i = N-1:-1:1
X(i) = (D(i) - C(i) * X(i + 1)) / B(i)
end
for i = 1:N
X(N) = D(N) / B(N)
end
But, I'm not sure about the algorithm of pentadiagonal matrix. I have searched through forum links and documents, but I couldn't able to get the clarity of thoughts. Can someone guide me to find a way and kindly judge my above implementation.
Thank you
Answers (1)
John D'Errico
on 6 Apr 2023
Edited: John D'Errico
on 6 Apr 2023
WHY? Don't write code to do what was already written well by world class experts in linear algebra. You are using MATLAB. So use sparse matrices to represent a sparse matrix. A banded matrix is indeed sparse, and very nicely so.
help spdiags
Then use backslash for the solve.
B = repmat([1 1 -4 1 1],10,1);
M = spdiags(B,[-4 -1 0 1 4],10,10);
spy(M)
full(M)
Leave the matrix as a sparse matrix of course.
S = rand(10,1); % I don't have your data.
P = M\S;
7 Comments
Kumaresh Kumaresh
on 7 Apr 2023
Torsten
on 7 Apr 2023
However, if I need to analyse my problem with increase in grid numbers, it should be hard in creating the sparse matrix every time.
Is this hard ?
B = repmat([1 1 -4 1 1],10,1);
M = spdiags(B,[-4 -1 0 1 4],10,10);
S = rand(10,1); % I don't have your data.
P = M\S;
In case your systems are too big for sparse direct methods like above, use an iterative scheme. It should be convergent since your matrix is weakly diagonal-dominant.
John D'Errico
on 7 Apr 2023
You have a viable solution to solve the problem. It will be very quick to solve such a problem, even for huge matrices. A quick test on my computer, for matrices of size 1e6 has the solve take 1/4 of a second, which includes building the matrix itself.
Why spend a fair amount of time to find the algorithm, then write and debug the code, to then need to maintain it? Good programming policy says not to waste programmer time on something you have that works, and works well. I'm not even at all sure your code would be faster than the simple sparse matrix solution. Lengthy loops can be slow.
Kumaresh Kumaresh
on 10 Apr 2023
Edited: Kumaresh Kumaresh
on 11 Apr 2023
Your matrix A from above is wrong in many ways.
I suggest you try to solve
d^2u/dx^2 + d^2u/dy^2 = f
on a rectangle with Dirichlet boundary conditions for u to test and correct it.
John D'Errico
on 10 Apr 2023
First, you apparently want to solve your problem efficiently. There is no reason why you would be asking to use a pentadiagonal solver, if it was not that.
But then you say you want to use a loop to construct your matrices, because you want to "understand" your problem better. Understanding how to use the right software to solve your problem means that you should use the proper tools.
If you REALLY, REALLY want to fully understand every aspect about your problem, you should write the code to multiply, add, subtract and divide numbers, from scratch. You should write EVERYTHING from scratch. Why are you using MATLAB anyway? Don't even use C. Go directly to assembly code. Perform those adds and multiplies using registers. Hey, I wrote code on machines where I needed to toggle in the instructions using dip switches. I learned nothing more than I hated writing code that way.
My point is, this is silly. You gain no more understanding of a problem by doing things the hard way. And you will gain no speed. Anyway, you already know how to construct the matrix you have decided to construct, doing it the inefficient way here with loops. So there is no understanding to be gained from that.
We are trying to teach you how to solve your problem, using MATLAB. If you want to understand something, then learn to use MATLAB.
Kumaresh Kumaresh
on 11 Apr 2023
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!