Repeat Lines 14 to 25 a specified number of times, only print final matrix

I'm really new to matlab. How would I go about getting this to repeat from line 14 to 25 a certain number of times (iterate) for instance 131 times. I don't want it to print the result at each iteration just the resulting matrix after 131 iterations.
clear;
HOLD=zeros(23,23);
HNEW=zeros(23,23);
R=zeros(23,23);
%Initial Value of matrix
for i=1:23
for j=1:23
HOLD(i,j)=10
HNEW(i,j)=10
end
end
R(22,2)=-0.2;
%implement implicit method
for i=2:22
for j=2:22
H1=0.25*(HOLD(i,j+1)+HOLD(i,j-1)+HOLD(i-1,j)+HOLD(i-1,j-1))
H2=0.25*(HNEW(i,j+1)+HNEW(i,j-1)+HNEW(i-1,j)+HNEW(i-1,j-1))
HNEW(i,j)=(1/((100*100*0.002/4/300/0.1)+1))*(H2+100*100*0.002/4/300/0.1*HOLD(i,j)+100*100*R(i,j)/4/300)
end
end
for i=2:22
for j=2:22
HOLD(i,j)=HNEW(i,j)
end
end
%No Flow Boundarys
for j=1:23
HOLD(1,:)=HOLD(3,:)
HOLD(23,:)=HOLD(21,:)
end
for i=1:23
HOLD(:,1)=HOLD(:,3)
HOLD(:,23)=HOLD(:,21)
end
%Need to repeat from line 14 a specified number of times.

 Accepted Answer

for RepeatNumber = 1 : 131
.... code ...
end

1 Comment

Thank you. I fixed an error as well. It runs for a very long time though and prints the matrix at every iteration. Is there a way to speed it up and/or only get it to show the final result.
clear;
HOLD=zeros(23,23);
HNEW=zeros(23,23);
R=zeros(23,23);
%Initial Value of matrix
for i=1:23
for j=1:23
HOLD(i,j)=10
HNEW(i,j)=10
end
end
R(22,2)=-0.2;
for RepeatNumber = 1 : 3
%implement implicit method
for i=2:22
for j=2:22
H2(i,j)=0.25*(HNEW(i,j+1)+HNEW(i,j-1)+HNEW(i-1,j)+HNEW(i-1,j-1))
HNEW(i,j)=(1/((100*100*0.002/4/300/0.1)+1))*(H2(i,j)+100*100*0.002/4/300/0.1*HOLD(i,j)+100*100*R(i,j)/4/300)
end
end
for i=2:22
for j=2:22
HOLD(i,j)=HNEW(i,j)
end
end
%No Flow Boundarys
for j=1:23
HOLD(1,:)=HOLD(3,:)
HOLD(23,:)=HOLD(21,:)
end
for i=1:23
HOLD(:,1)=HOLD(:,3)
HOLD(:,23)=HOLD(:,21)
end
%Need to repeat from line 14 a specified number of times.
end

Sign in to comment.

More Answers (1)

I'm okay with the iteration thing now, but it takes forever (minutes for three iterations and I need hundreds). It also prints the matrix after every single iteration rather than only after the last one. Is there any way to change that?
clear;
HOLD=zeros(23,23);
HNEW=zeros(23,23);
R=zeros(23,23);
%Initial Value of matrix
for i=1:23
for j=1:23
HOLD(i,j)=10
HNEW(i,j)=10
end
end
R(22,2)=-0.2;
for RepeatNumber = 1 : 3
%implement implicit method
for i=2:22
for j=2:22
H2(i,j)=0.25*(HNEW(i,j+1)+HNEW(i,j-1)+HNEW(i-1,j)+HNEW(i-1,j-1))
HNEW(i,j)=(1/((100*100*0.002/4/300/0.1)+1))*(H2(i,j)+100*100*0.002/4/300/0.1*HOLD(i,j)+100*100*R(i,j)/4/300)
end
end
for i=2:22
for j=2:22
HOLD(i,j)=HNEW(i,j)
end
end
%No Flow Boundarys
for j=1:23
HOLD(1,:)=HOLD(3,:)
HOLD(23,:)=HOLD(21,:)
end
for i=1:23
HOLD(:,1)=HOLD(:,3)
HOLD(:,23)=HOLD(:,21)
end
%Need to repeat from line 14 a specified number of times.
end

1 Comment

semi-colon on the end of a line prevents the result of the assignment from being displayed. For example instead of
HOLD(i,j)=HNEW(i,j)
use
HOLD(i,j)=HNEW(i,j);

Sign in to comment.

Categories

Find more on Language Fundamentals 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!