If any element of the array is less than a number, do some statements

If any element of the array is less than a number, do some statements
I want to use this above thing in while loop

 Akzeptierte Antwort

if any(YourArray(:) < TheValue)
do the statements
end

9 Kommentare

while (any(e1(:)) > e) | (any(e2(:)) > e)
do statements
end
This is going for an infinite loop.
clc
clear all
lh=input('Enter the value of L/H ');
n2=input('Enter the value of N2 ');
re=input('Enter the value of Re ');
dt=input('Enter the value of dt ');
dx=input('Enter the value of dx ');
dy=input('Enter the value of dy ');
hd=input('Enter the value of H/d ');
e=input('Enter the stopping criteria ');
m=((lh*hd)/dx)+1;
n=(hd/dy)+1;
n1=n-n2;
v0=zeros(m,n);
v=zeros(m,n);
s0=zeros(m,n);
s=zeros(m,n);
v_v=zeros(m,n);
v_u=zeros(m,n);
it=1;
count=0;
e1=ones(m,n);
e2=ones(m,n);
e3=10;
while (any(e1(:)) > e) | (any(e2(:)) > e)
count=count+1;
dt1=it*dt;
it=it+1;
c1=1/((1/dt1)+((2/re)*((1/(dx*dx))+(1/(dy*dy)))));
c2=1/(2*((1/(dx*dx))+(1/(dy*dy))));
for j=2:n1
s(1,j) = -1;
end
for j=n1+1:n
v(1,j) = 0;
s(1,j) = ((j-1)*dy)-(hd);
end
for i=2:m-1
s(i,1) = -1;
end
for j=2:n2
s(m,j) = ((j-1)*dy)-1;
v(m,j) = 0;
end
for j=n2+1:n
s(m,j) = 0;
end
for i=2:m-1
s(i,n) = 0;
end
for i=2:m-1
for j=2:n-1
v(i,j) = c1*((v0(i,j)/dt1)-(v0(i,j+1)/(dy*dy))-(v0(i,j-1)/(dy*dy))+(v0(i+1,j)/(re*dx*dx))+(v0(i-1,j)/(re*dx*dx)));
s(i,j) = c2*(((s0(i+1,j)+s0(i-1,j))/(dx*dx))+((s0(i,j+1)+s0(i,j-1))/(dy*dy))+v0(i,j));
end
end
for j=2:n1
v(1,j) = -2*((s0(2,j)-s0(1,j))/(dx*dx));
end
for i=2:m-1
v(i,1) = 2*((s0(i,1)-s0(i,2))/(dy*dy));
end
for j=n2+1:n
v(m,j) = -2*((s0(m-1,j)-s0(m,j))/(dx*dx));
end
for i=2:m-1
v(i,n) = 2*((s0(i,n)-s0(i,n-1))/(dy*dy));
end
for i=1:m
for j=1:n
e1(i,j)=abs(v(i,j)-v0(i,j));
e2(i,j)=abs(s(i,j)-s0(i,j));
v0(i,j)=v(i,j);
s0(i,j)=s(i,j);
end
end
end
fprintf ('\n Iterations = %d \n \n',count)
disp (v)
disp (s)
for j=1:n1
v_v(1,j) = 0;
v_u(1,j) = 0;
end
for j=n1:n
v_v(1,j) = 0;
v_u(1,j) = 1;
end
for i=2:m-1
v_v(i,1) = 0;
v_u(i,1) = 0;
end
for j=1:n2
v_v(m,j) = 0;
v_u(m,j) = 1;
end
for j=n2:n
v_v(m,j) = 0;
v_u(m,j) = 0;
end
for i=2:m-1
v_v(i,n) = 0;
v_u(i,n) = 0;
end
for i=2:m-1
for j=2:n-1
v_u(i,j) = (s(i,j+1)-s(i,j-1))/(2*dy);
v_v(i,j) = (s(i-1,j)-s(i+1,j))/(2*dx);
end
end
disp (v_u)
disp (v_v)
figure (1);
plot (v_u);
figure (2);
plot (v_v);
startx=[1:1:m];
starty=[1:1:n];
figure (3);
quiver(v_u,v_v);
streamline(v_u,v_v,startx,starty);
This is the whole code.
I want every value of e1 and e2 to be less than e.
lh=1, n2=5 re=1 dt=0.1 dx=0.5 dy=0.5 hd=10 e=0.000001
You put the brackets in the wrong place in your while statement. You have
(any(e1(:)) > e)
which breaks down like
temp = any(e1(:))
(temp > e)
Some of the e1 are non-zero, so any(e1(:)) is true, and logical true has value 1, so that 1 is being tested against 1e-6 and it is indeed greater.
You should have had
any(e1(:) > e)
which breaks down as
temp = e1(:) > e;
any(temp)
@Milan: By the way, the code gets nicer with vectorization. Replace:
for j=2:n1
s(1,j) = -1;
end
by
s(2:n1) = -1;
and the same in many other places.
@Walter: Thanks a lot. That worked.
There's one more problem. I'm using streamline in this code and I'm getting this figure. I'm getting the sreamlines only in that area and not in the upper left area.
Also, in some cases, I have to use lh=0.5 and 2. Then I'm getting this error:
Error using stream2 (line 49)
STARTX,STARTY must all be the same size.
Error in streamline (line 60)
verts = stream2(u,v,sx,sy,options);
Error in Copy_of_final (line 153)
streamline(v_u,v_v,startx,starty);
perhaps you want to use
[SX, SY] = meshgrid(startx, starty);
streamline(v_u, v_v, SX, SY);
This would also stop the error about startx and starty being different sizes.
However, you have confused x and y. Your m is number of rows, and you use startx = 1 : m, and your n is number of columns and you use starty = 1 : n. But in MATLAB, X corresponds to columns (the second subscript), not to rows, and Y corresponds to rows (the first subscript), not to columns. Anywhere in your calculation where you use the first subscript as X is in error.
One more doubt, I'm getting NaN for Re values 5 and 10
Sorry, I am having problems with my vision, and that pdf is too much for me to go through in detail and compare to your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by