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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Geoff Hayes
Geoff Hayes il 28 Dic 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.

Accedi per commentare.

Risposta accettata

Adam Danz
Adam Danz il 28 Dic 2021
Modificato: Adam Danz il 28 Dic 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 = 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)).

Più risposte (1)

Voss
Voss il 28 Dic 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));

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by