why does it get stuck in loop if?

3 visualizaciones (últimos 30 días)
Piyush Kelkar
Piyush Kelkar el 30 de Sept. de 2020
Comentada: Eshwaree Shingwekar el 1 de Oct. de 2020
clear all
close all
L=40;
d1=6;
d2=6;
H1=4;
H2=3;
d=3;
k1=0.004;
k2=0.002;
avg=0.5*(k1+k2);
Nx =L+2;
Ny = d1+d2+1 ;
%sheet pile will be at Nx=0.5*L+1
% initialize coefficient matrix and constant vector
A = zeros(Nx*Ny);
X = zeros(Nx*Ny,1);
% initial head distribution
H(1:Nx*Ny,1) = 0;
% coefficient matrix and constant vector
% Corners
%bottom left
i=1;
A(i,Nx+i) = 1;
A(i,2) = 1;
A(i,1) = -4;
%bottom right
i = Nx;
A(i,i+Nx) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
%top left
i = (Ny-1)*Nx + 1;
A(i,i+1) = 1;
A(i,i) = -4;
A(i,i-Nx) = 1;
X(i) = -(H1);
%top right
i = Nx*Ny;
A(i,i-1) = 1;
A(i,i) = -4;
A(i,i-Nx) = 1;
X(i) = -(H2);
% Boundary nodes
% bottom
for m = 2:(Nx-1)
%n = 1
i = m;
A(i,i+Nx) = 2;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
%top
for m = 2:((Nx)*0.5)
i = (Ny-1)*Nx + m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
X(i)=-H1;
end
for m = ((0.5*Nx)+1):(Nx-1)
i = (Ny-1)*Nx + m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
X(i)=-H2;
end
%left
for m=2:(Ny-1)
%m = 1
i = (m-1)*Nx + 1;
A(i,i+Nx) = 1;
A(i,i+1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%right
for m=2:(Ny-1)
%m = Nx
i = (m-1)*Nx + Nx;
A(i,i+Nx) = 1;
A(i,i-1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%intersection
for m=((d2*Nx) +1):((d2+1)*Nx)
i=m;
A(i,i+Nx) = k1;
A(i,i-Nx) = k1;
A(i,i+1) = avg;
A(i,i-1) = avg;
A(i,i) = -2*(avg+k1);
end
%inner nodes below intersection
for n = 2:d2
for m = 2:(Nx-1)
i = (n-1)*Nx + m;
A(i,i+Nx) = 1;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
end
%inner nodes above intersection
for n = (d2+2):(Ny-1)
for m = 2:(Nx-1)
while((Ny-d)<n<=Ny)
%left of sheet pile
if m==(0.5*Nx)
i=(n-1)*Nx+m;
A(i,i+Nx) = 1;
A(i,i-1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%right of sheet pile
if m==(Nx*0.5)+1
i=(n-1)*Nx+m;
A(i,i+Nx) = 1;
A(i,i+1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%bottom of sheet pile
if n== Ny-d
if m==(0.5*Nx)
i=(n-1)*Nx+m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
A(i,i+Nx)=0.5;
A(i,i+Nx+1)=0.5;
end
end
i = (n-1)*Nx + m;
A(i,i+Nx) = 1;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
end
end
% Direct Method
H =(inv(A) * X);
for n=1:Ny
for m=1:Nx
i = (n-1)*Nx + m;
while(n< (Ny-d))
if m==(0.5*Nx)
H(i+1)=H(i);
end
end
Direct(n,m) = H(i);
end
end
[SL: formatted as code and Smart Indented it in the Editor to make the start and end of for, if, while statements align. Piyush Kelkar, in future questions please use the first button in the Code section on the toolbar to format your code as code when you enter it. Thanks.]

Respuestas (1)

Steven Lord
Steven Lord el 30 de Sept. de 2020
In the part of your code that starts with the comment %inner nodes above intersection you have a while statement:
while((Ny-d)<n<=Ny)
This doesn't do what you think it does. It is the equivalent of:
while ((Ny-d) < n) <= Ny
(Ny-d) < n is either false (treated as 0) or true (treated as 1). So this is one of:
while 0 <= Ny
end
% or
while 1 <= Ny
end
To do what you want:
while ((Ny-d) < n) & (n <= Ny)
end
But note that none of Ny, d, or n change inside the while statement. So if MATLAB enters that statement because the condition is satisfied, it can never leave that statement because the condition will always be satisfied whenever it checks. You have this same problem with the while in the code started with the % Direct Method comment.
  2 comentarios
Eshwaree Shingwekar
Eshwaree Shingwekar el 1 de Oct. de 2020
clear all
close all
L=40;
d1=6;
d2=6;
H1=4;
H2=3;
d=3;
k1=0.004;
k2=0.002;
avg=0.5*(k1+k2);
Nx =L+2;
Ny = d1+d2+1 ;
%sheet pile will be at Nx=0.5*L+1
% initialize coefficient matrix and constant vector
A = zeros(Nx*Ny);
X = zeros(Nx*Ny,1);
% initial head distribution
H(1:Nx*Ny,1) = 0;
% coefficient matrix and constant vector
% Corners
%bottom left
i=1;
A(i,Nx+i) = 1;
A(i,2) = 1;
A(i,1) = -4;
%bottom right
i = Nx;
A(i,i+Nx) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
%top left
i = (Ny-1)*Nx + 1;
A(i,i+1) = 1;
A(i,i) = -4;
A(i,i-Nx) = 1;
X(i) = -(H1);
%top right
i = Nx*Ny;
A(i,i-1) = 1;
A(i,i) = -4;
A(i,i-Nx) = 1;
X(i) = -(H2);
% Boundary nodes
% bottom
for m = 2:(Nx-1)
%n = 1
i = m;
A(i,i+Nx) = 2;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
%top
for m = 2:((Nx)*0.5)
i = (Ny-1)*Nx + m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
X(i)=-H1;
end
for m = ((0.5*Nx)+1):(Nx-1)
i = (Ny-1)*Nx + m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
X(i)=-H2;
end
%left
for m=2:(Ny-1)
%m = 1
i = (m-1)*Nx + 1;
A(i,i+Nx) = 1;
A(i,i+1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%right
for m=2:(Ny-1)
%m = Nx
i = (m-1)*Nx + Nx;
A(i,i+Nx) = 1;
A(i,i-1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%intersection
for m=((d2*Nx) +1):((d2+1)*Nx)
i=m;
A(i,i+Nx) = k1;
A(i,i-Nx) = k1;
A(i,i+1) = avg;
A(i,i-1) = avg;
A(i,i) = -2*(avg+k1);
end
%inner nodes below intersection
for n = 2:d2
for m = 2:(Nx-1)
i = (n-1)*Nx + m;
A(i,i+Nx) = 1;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
end
%inner nodes above intersection
for n = (d2+2):(Ny-1)
for m = 2:(Nx-1)
%bottom of sheet pile
if n==( Ny-d)
if m==(0.5*Nx)
i=(n-1)*Nx+m;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
A(i,i+Nx)=0.5;
A(i,i+Nx+1)=0.5;
end
end
while ((Ny-d) < n & n <= Ny)
%left of sheet pile
if m==(0.5*Nx)
i=(n-1)*Nx+m;
A(i,i+Nx) = 1;
A(i,i-1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
%right of sheet pile
if m==(Nx*0.5)+1
i=(n-1)*Nx+m;
A(i,i+Nx) = 1;
A(i,i+1) = 2;
A(i,i-Nx) = 1;
A(i,i) = -4;
end
n=n+1;
m=m+1;
end
end
i = (n-1)*Nx + m;
A(i,i+Nx) = 1;
A(i,i-Nx) = 1;
A(i,i+1) = 1;
A(i,i-1) = 1;
A(i,i) = -4;
end
% Direct Method
H =(inv(A) * X);
for n=1:Ny
for m=1:Nx
i = (n-1)*Nx + m;
while(n< (Ny-d))
if m==(0.5*Nx)
H(i+1)=H(i);
end
end
Direct(n,m) = H(i);
end
end
Eshwaree Shingwekar
Eshwaree Shingwekar el 1 de Oct. de 2020
how does the size of matrix A change when it was defined previously?

Iniciar sesión para comentar.

Categorías

Más información sobre Call C++ from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by