why do I receive two numbers when I use Find command?
1 view (last 30 days)
Show older comments
mehmet salihi
on 28 Dec 2021
Commented: mehmet salihi
on 28 Dec 2021
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)
1 Comment
Geoff Hayes
on 28 Dec 2021
@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.
Accepted Answer
Adam Danz
on 28 Dec 2021
Edited: Adam Danz
on 28 Dec 2021
> 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 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)).
More Answers (1)
Voss
on 28 Dec 2021
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));
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!