Combining Functions of Gaussian elimination and Banded Matrix

5 次查看(过去 30 天)
Goal: to create a banded version of Gaussian elimination without pivoting.
%%Work so far: I have a code that generates a banded matrix. . .
function [Z]=bandmatrix(n,k1,k2)
Z=zeros(n,n);
for i=1:n
for j=1:n
Z(i,j)=1;
end
end
for i=1:n
for j=1:n
if (j<i-k1) Z(i,j)=0;
end
if(j>i+k2) Z(i,j)=0;
end
end
end
%% I have a code for GE without pivoting as. . .
function [x] = GE_WithoutPivoting1(A,b)
n = length(b);
for k=1:n-1
if abs(A(k,k)) < 1e-15
error('A has diagonal entries of zero')
end
for i=k+1:n
m = -A(i,k)/A(k,k); % multiplier for current row i
for j=k+1:n
A(i,j) = A(i,j) + m*A(k,j);
end
b(i) = b(i) + m*b(k);
end
end
x = GE_BackSubstitution(A,b);
QUESTION: to achieve my goal as in above, i.e., to properly implement a banded version of Gaussian elimination without pivoting. What I did was to call the GE elimination at end of the function [Z]=bandmatrix(n,k1,k2) i.e.,
function [Z]=bandmatrix(n,k1,k2)
....
....
x = GE_BackSubstitution(A,b); %This is already in my working directory
But my fear is that function [Z]=bandmatrix(n,k1,k2) might not be working and that it is function [x] = GE_WithoutPivoting1(A,b) that is working (since it is already saved on my working directory).
Please, using my two functions how do I implement a banded version of GE without pivoting. Any edition to my written codes will be welcome. Thank you

回答(1 个)

Sameer
Sameer 2025-3-12
To create a banded version of Gaussian elimination without pivoting, you need to ensure that your Gaussian elimination function works specifically with the banded matrix generated by your "bandmatrix" function.
  1. Generate the banded matrix using your "bandmatrix" function.
  2. Apply Gaussian elimination on this banded matrix without pivoting.
Here's how you can do it:
function [Z] = bandmatrix(n, k1, k2)
Z = zeros(n, n);
for i = 1:n
for j = 1:n
if (j >= i - k1) && (j <= i + k2)
Z(i, j) = rand();
end
end
end
for i = 1:n
if abs(Z(i, i)) < 1e-15
Z(i, i) = 1;
end
end
end
function [x] = GE_WithoutPivoting1(A, b)
n = length(b);
for k = 1:n-1
if abs(A(k, k)) < 1e-15
error('A has diagonal entries of zero')
end
for i = k+1:n
m = -A(i, k) / A(k, k);
for j = k+1:n
A(i, j) = A(i, j) + m * A(k, j);
end
b(i) = b(i) + m * b(k);
end
end
x = GE_BackSubstitution(A, b);
end
% Function for back substitution
function [x] = GE_BackSubstitution(A, b)
n = length(b);
x = zeros(n, 1);
for i = n:-1:1
x(i) = (b(i) - A(i, i+1:n) * x(i+1:n)) / A(i, i);
end
end
% integrate both banded matrix generation and GE
function [x] = banded_GE(n, k1, k2, b)
A = bandmatrix(n, k1, k2);
x = GE_WithoutPivoting1(A, b);
end
n = 5; % Size of the matrix
k1 = 1; % Lower bandwidth
k2 = 1; % Upper bandwidth
b = rand(n, 1);
x = banded_GE(n, k1, k2, b);
disp(x);
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Octave 的更多信息

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by