finite difference method remove for loop
Show older comments
Hello all
I have a complex for nested loop need to vectorization, the code I posted below is a piece of my code to calculate finite difference equation. Because my execution time takes too long so I need to improve it. Please give me any advice to reduce execution time. Thanks for any help from you, all
if true
% code
loopVal1 = 14;
loopVal2 = 12;
loopVal3 = 20;
for n = 1:loopVal1
for nx = 2:loopVal2- 1
for ny = 2:loopVal3- 1
n = (ny - 1)*loopVal2+ nx;
nm = n + (nc - 1)*loopVal2*loopVal3;
g_o(nm) =-coffi(n,nc).*(heatNext(1, n, nc)-heat(1, n, nc))./dt + 0.5.*((k(n, nc).*(heat(1, n + 1, nc) - 2.*heat(1, n, nc) + heat(1, n - 1, nc));
matrix(nm, nm - 1) = -coffi1(n,nc)./(2.*dx.^2);
matrix(nm, nm - loopVal2) = -coffi1(n,nc)./(2.*dy.^2);
matrix(nm, nm + loopVal2) = -coffi1(n,nc)./(2.*dy.^2);
matrix(nm, nm) = coffi(n,nc)./dt + heatNext(1, n - loopVal2, nc))/dy^2;
Matrix(nm, nm + 1) = -coffi1(n,nc)/(2*dx^2);
end
end
end
Answers (0)
Categories
Find more on Creating and Concatenating Matrices 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!