Pentadiagonal matrix to solve for Pressure variable
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello everyone,
I want to solve my pressure equation implicitly by pentadiagonal matrix method. Here is the following equation.
---------------> (1)
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
댓글 수: 0
답변 (1개)
John D'Errico
2023년 4월 6일
편집: John D'Errico
2023년 4월 6일
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
John D'Errico
2023년 4월 10일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!