why do I receive two numbers when I use Find command?

조회 수: 1 (최근 30일)
mehmet salihi
mehmet salihi 2021년 12월 28일
댓글: mehmet salihi 2021년 12월 28일
could you please figure out the problem why do i get twi indecies for tg
which the first index t( 45633 ) is not giving the correct index but i still receive it.
clear
close all, clc
ca = 1014 ;
ci = 2050 ;
cw = 4218 ;
eps = 27.03 ;
LWC = 0.001 ;
Haw = 500 ;
His = 100 ;
ki = 2.18 ;
kw = 0.571 ;
Le = 13.4 ;
LF = 3.344 * 1e5 ;
LE = 2.26 * 1e6 ;
p0 = 6.3*1e4 ;
v = 90 ;
beta = 0.55 ;
rho_g = 917 ;
rho_r = 880 ;
rho_w = 1000 ;
X = 11 ;
r = 0.9 ;
R = 287.05 ;
Tf = 273.15 ;
cp = 1012 ;
Ta= 263.15;
Ts=Ta;
Qa=r*Haw*((v)^2/(2*cp));
Qk=LWC*beta*((v^3)/2);
sigma = 5.67*1e-8;
t=0:0.001:320;
for i=1:length(t)
Bg = (ki*(Tf-Ts)) / (LWC*beta*v*LF + (Qa+Qk)-((Haw+LWC*beta*v*cw+X*eps)*(Tf-Ta)));
tg = (rho_r /(LWC*beta*v))*Bg ; % time when first glaze appears
% B(i)=((Haw+X*eps+LWC*beta*v*cw-Qa-Qk)/(rho_g*LF))*t(i);
end
tg_index=find(abs(t-tg)<1e-3)
tg_index = 1×2
45633 45634
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2021년 12월 28일
@mehmet salihi - but both of those indices satisfy the condition that you are using in your call to find. For example,
>> abs(t(45633)-tg)
ans =
5.9622e-04
and
>> abs(t(45634)-tg)
ans =
4.0378e-04
where the ans to each is less than 1e-3.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 12월 28일
편집: Adam Danz 2021년 12월 28일
> why do i get two indices for tg?
Let's look at your data. Your code:
ca = 1014 ;
ci = 2050 ;
cw = 4218 ;
eps = 27.03 ;
LWC = 0.001 ;
Haw = 500 ;
His = 100 ;
ki = 2.18 ;
kw = 0.571 ;
Le = 13.4 ;
LF = 3.344 * 1e5 ;
LE = 2.26 * 1e6 ;
p0 = 6.3*1e4 ;
v = 90 ;
beta = 0.55 ;
rho_g = 917 ;
rho_r = 880 ;
rho_w = 1000 ;
X = 11 ;
r = 0.9 ;
R = 287.05 ;
Tf = 273.15 ;
cp = 1012 ;
Ta= 263.15;
Ts=Ta;
Qa=r*Haw*((v)^2/(2*cp));
Qk=LWC*beta*((v^3)/2);
sigma = 5.67*1e-8;
t=0:0.001:320;
for i=1:length(t)
Bg = (ki*(Tf-Ts)) / (LWC*beta*v*LF + (Qa+Qk)-((Haw+LWC*beta*v*cw+X*eps)*(Tf-Ta)));
tg = (rho_r /(LWC*beta*v))*Bg ; % time when first glaze appears
% B(i)=((Haw+X*eps+LWC*beta*v*cw-Qa-Qk)/(rho_g*LF))*t(i);
end
tg_index=find(abs(t-tg)<1e-3)
tg_index = 1×2
45633 45634
tg_index contains the indices of all values abs(t-tg) that are less than 0.001. Let's plot those values,
plot(abs(t-tg), '-o')
yline(1e-3) % draw a line at your specified threshold
ylim([0,0.002]) % zoom into the bottom to see the threshold
% add red lines at the tg_index values for confirmation
xline(tg_index, 'r--')
Here we can see that there are two values less than your threshold and they are at the expected indices returned by find().
> the first index t( 45633 ) is not giving the correct index but i still receive it.
The investigation above shows that both indices correctly find values less than 0.001. Were you trying to find the minimum value? If so, [~, tg_index] = min(abs(t-tg)).
  댓글 수: 1
mehmet salihi
mehmet salihi 2021년 12월 28일
I am really in love with you explanation.
thank you very much for your help

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2021년 12월 28일
Evidently those two elements of t are within 0.001 of tg. If you want just the first element of t that is within 0.001 of tg you can say
tg_index=find(abs(t-tg)<1e-3,1);
but it seems more likely that you want the element of t that is closest to tg, in which case you can say
[~,tg_index]=min(abs(t-tg));

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by