Effacer les filtres
Effacer les filtres

Help using loop and range interval

2 vues (au cours des 30 derniers jours)
Damilola
Damilola le 10 Nov 2014
Commenté : Damilola le 10 Nov 2014
I have a wind speed data (v) as a column vector that varies at different instant, I want to create a model = 0.5*1.29*9.62*jan.^3*cp; that uses different coefficient (cp) at a particular range of wind speed v. If v is less than 3, cp = 0, if v falls in a range between 3 and 10 (inclusive) then cp = 0.4, if v falls in a range greater than 10 and less or equal to 14 cp = 0.2 and if v is greater than 14 but less than 25 cp = 0.1 and if v is greater than 25 cp = 0.001. I want to create this model and have a final result of column vector that I will plot. Below is a loop that I tried to write.This is my first time trying to create a loop and also first time trying to have a range interval in matlab code. When I run this code I get an error message "Operands to the and && operators must be convertible to logical scalar values." Please I need help as I don't know how to solve this error. Thanks
jan = airport_spd;
model = 1.29*9.62*jan.^3*cp;
for v = jan
if v < 3
cp= 0;
model = 0.5*1.29*9.62*v.^3*cp;
elseif v >=3 & v<=10
cp = 0.4;
model = 0.5*1.29*9.62*v.^3*cp;
elseif v > 10 && v<= 14
cp = 0.2;
model = 0.5*1.29*9.62*v.^3*cp;
elseif v >14 && v < 25
cp = 0.1;
model = 0.5*1.29*9.62*v.^3*cp;
elseif v > 25
cp = 0.001;
model = 0.5*1.29*9.62*v.^3*cp;
end
end

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 10 Nov 2014
Modifié(e) : Andrei Bobrov le 10 Nov 2014
v = randi([0,30],60,1); % Let your data
k0 = 0.5*1.29*9.62;
c = [0;.4;.2;.1;.001];
k = [-inf;3;10;14;25;inf];
[~,b] = histc(v,k);
model = k0*v.^3.*c(b);
with loop for..end
n = numel(v);
model = zeros(n,1);
k0 = 0.5*1.29*9.62;
for ii = 1:n
if v(ii) < 3
cp = 0;
elseif v(ii) >=3 && v(ii) < 10
cp = 0.4;
elseif v(ii) >= 10 && v(ii) < 14
cp = 0.2;
elseif v(ii) >= 14 && v(ii) < 25
cp = 0.1;
elseif v(ii) >= 25
cp = 0.001;
end
model(ii) = k0*v(ii).^3*cp;
end

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by