# Reducing iteration computation time

1 view (last 30 days)
Ignatius Tommy Pratama on 14 Oct 2017
Edited: dpb on 14 Oct 2017
Hi, I am trying to do finite difference calculation with MATLAB to estimate the total head within the flow region in a groundwater flow analysis. I have already done the code and it run well. Nonetheless, the computation time during the iteration procedure took a very long time for a very fine grid (nsize). Up to now, my guess is because of the size of the M matrix. The bigger the matrix is, the longer the calculation time. For nsize equals to 0.25, the dimension of M matrix is up to 97 x 217, and it required more than 30 mins to finish the computation of one model. If you can help with my problem, I really appreciate it. Thank you. The following code is the code for the iteration:
%%Step 3: Matrix iteration construction
M_mod = M;
while err > tol
%Update iteration counter
k = k+1;
%Loop through computational nodes
for i = 2:Ny;
for j = 1:Nx; %Retained zone
if j == 1;
if i == Ny;
M_mod(i,j) = 0.25*(2*M(i-1,j)+2*M(i,j+1));
else
M_mod(i,j) = 0.25*(M(i+1,j)+2*M(i,j+1)+M(i-1,j));
end
elseif j == (2*D/nsize); %Retained zone near sheeting pile
if i <= ((He-dj+Hp)/nsize);
M_mod(i,j) = 0.25*(M(i+1,j)+M(i-1,j)+2*M(i,j-1));
elseif and(i > ((He-dj+Hp)/nsize),i <= Ny-1);
M_mod(i,j) = 0.25*(M(i+1,j)+M(i-1,j)+M(i,j-1)+M(i,j+1));
elseif i == Ny
M_mod(i,j) = 0.25*(M(i,j+1)+2*M(i-1,j)+M(i,j-1));
end
elseif j == 2*D/nsize+1; %Nodes along the interface cut-off
if and(i >= 2, i <=((He-dj+Hp)/nsize));
M_mod(i,j) = M(i,j);
elseif i == ((He-dj+Hp)/nsize+1);
M_mod(i,j) = 0.25*(M(i,j+1)+2*M(i+1,j)+M(i,j-1));
elseif i == Ny;
M_mod(i,j) = 0.25*(M(i,j+1)+2*M(i-1,j)+M(i,j-1));
else
M_mod(i,j) = 0.25*(M(i+1,j)+M(i,j+1)+M(i-1,j)+M(i,j-1));
end
elseif and(and(j >= (2*D/nsize+2), j <= Nx),i <= ((He-dj+di)/nsize)+1); %Nodes in the excavation area
M_mod(i,j) = M(i,j);
elseif j == Nx; %Excavated zone
if i == Ny;
M_mod(i,j) = 0.25*(2*M(i-1,j)+2*M(i,j-1));
else
M_mod(i,j) = 0.25*(M(i+1,j)+2*M(i,j-1)+M(i-1,j));
end
elseif j == (2*D/nsize+2); %Excavated zone near sheeting pile
if i == Ny;
M_mod(i,j) = 0.25*(M(i,j+1)+2*M(i-1,j)+M(i,j-1));
elseif and(i > ((He-dj+di)/nsize+1),i <= ((He-dj+Hp)/nsize));
M_mod(i,j) = 0.25*(M(i+1,j)+M(i-1,j)+2*M(i,j+1));
elseif and(i > ((He-dj+Hp)/nsize),i <= Ny-1);
M_mod(i,j) = 0.25*(M(i+1,j)+M(i-1,j)+M(i,j+1)+M(i,j-1));
end
elseif i == Ny; %Global
M_mod(i,j) = 0.25*(M(i,j+1)+2*M(i-1,j)+M(i,j-1));
else
M_mod(i,j) = 0.25*(M(i+1,j)+M(i,j+1)+M(i-1,j)+M(i,j-1));
end
end
end
%Calculate error
err = sqrt(sum(sum((M_mod-M).^2)));
%Update M_mod
M = M_mod;
end

### Categories

Find more on Solver Outputs and Iterative Display 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!
{"ft_lang_label":"__label__en","ft_lang_prob":0.6558836,"math_prob":0.9998116,"size":2853,"snap":"2022-40-2023-06","text_gpt3_token_len":1090,"char_repetition_ratio":0.21024922,"word_repetition_ratio":0.0726817,"special_character_ratio":0.41254818,"punctuation_ratio":0.18270402,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99988973,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-05T05:02:21Z\",\"WARC-Record-ID\":\"<urn:uuid:09d367f5-d9fa-444b-ab30-025fea0e2849>\",\"Content-Length\":\"120266\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7ec90655-dd88-48d2-b16e-1adb44ecdd46>\",\"WARC-Concurrent-To\":\"<urn:uuid:34563e58-dc81-4c81-8b49-c8bd0a30b8f7>\",\"WARC-IP-Address\":\"104.68.243.15\",\"WARC-Target-URI\":\"https://es.mathworks.com/matlabcentral/answers/361289-reducing-iteration-computation-time?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:MD4OVI4ERAC3CDZULX5HSSNZ7PILUADY\",\"WARC-Block-Digest\":\"sha1:SU75BYSCL37RID2ATGKLT44EVSSLQAUQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500215.91_warc_CC-MAIN-20230205032040-20230205062040-00277.warc.gz\"}"}