Why is Gauss elimination taking so much time in this algorithm?
2 views (last 30 days)
Show older comments
Following is a part of an algorithm i wrote for formulating block tri diagonal matrix to solve an elliptic pde via 5 point stencil formula and natural ordering:
M = 4;
N = M;
h = 1/M;
M1 = (M-1)*(N-1);
F = zeros(M1,1);
h1 = h^2;
x = zeros(N-1,1);
for i = 1:N-1
x(i) = h*i;
end
y = zeros(M-1,1);
for j = 1:M-1
y(j) = h*j;
end
for j = 1:N-1
for i = 1:M-1
k = i + (M-1)*(j-1);
F(k) = f(x(i),y(j));
end
end
% define block B%
B = zeros(N-1,N-1);
B(1,1) = -4/h1;
B(1,2) = 1/h1;
B(2,1) = 1/h1;
for k = 2:N-2
B(k,k) = -4/h1;
B(k+1,k) = 1/h1;
B(k,k+1) = 1/h1;
end
B(N-1,N-2) = 1/h1;
B(N-1,N-1) = -4/h1;
% Concatenate A
blocks = repmat({B}, N-1, 1);
res = blkdiag(blocks{:});
R = repmat(1/h1,1,M1-(M-1));
I1 = diag(R,M-1);
I2 = diag(R,-(M-1));
A = res+I1+I2;
tic
U = A\F;
toc
f(x,y) is defined in a function file as follows,
function W = f(x,y)
W = -2*pi^2*sin(pi*x)*sin(pi*y);
end
For large values of M gauss elimination takes a lot of time. For instance when i take M = 100 the G elimination step alone takes precisely 25.499652 seconds.
I cant figure out why gauss elimination is taking so much time. Am i missing some initializations? Any suggestions will be helpful.
Regards
0 Comments
Answers (0)
See Also
Categories
Find more on Eigenvalue Problems 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!