Problem with for loop within a loop of a loop
Show older comments
The following code calculates the excess pore water pressure u after consolidation, and generates a m x n matrix, where the thickness of the soil layer i divided into m equal parts and u is calculated for n time steps.
In this simplified exampel, beta is a constant, but I want to impliment specifik values of beta for different equal sized depths indicated by m.
So say I have a beta-vector containing 5 values and a soil layer divided into m=50 equal parts. beta(1) will run from m(1):m(10), beta(2) will run from m(11):m(20) and so forth.
Does anyone have a suggestion on how to impliment the beta-vector?
beta=0.2; n= 10; m= 5
u_1=[60 54 41 29 19 15]';
u(:,1)=u_1;
for j=(1:n)
for i=(2:m)
u(i,j+1)=u(i,j)+beta*(u(i-1,j)+u(i+1,j)-2*u(i,j));
for i=(m+1) % As we look at a half-closed layer
u(i,j+1)=u(i,j)+beta(end)*(2*u(i-1,j)-2*u(i,j))
end
end
end
(Ultimatly I will end up with lenght(beta)=30, m= 300 and n=>130 and even with beta as a constant it takes some time to run.)
Much appriciated!
8 Comments
Walter Roberson
on 7 Sep 2019
for i=(2:m)
u(i,j+1)=u(i,j)+beta*(u(i-1,j)+u(i+1,j)-2*u(i,j));
for i=(m+1) % As we look at a half-closed layer
u(i,j+1)=u(i,j)+beta(end)*(2*u(i-1,j)-2*u(i,j)) %HERE
end
end
On the line I marked with HERE, does i refer to for i=(m+1) or does it refer to the enclosing for i=(2:m) ?
Mette Marie Bondegaard Petersen
on 7 Sep 2019
Walter Roberson
on 7 Sep 2019
Perhaps
for i=(2:m)
u(i,j+1)=u(i,j)+beta*(u(i-1,j)+u(i+1,j)-2*u(i,j));
end
for i=(m+1) % As we look at a half-closed layer
u(i,j+1)=u(i,j)+beta(end)*(2*u(i-1,j)-2*u(i,j));
end
?
Mette Marie Bondegaard Petersen
on 7 Sep 2019
Walter Roberson
on 7 Sep 2019
Well as I see it, this gives me the same result as before.
Yes, but faster! ;-)
Mette Marie Bondegaard Petersen
on 7 Sep 2019
Guillaume
on 7 Sep 2019
Note that
for i = (m+1) %why the () ?
is simply
i = m+1;
Mette Marie Bondegaard Petersen
on 8 Sep 2019
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!