Effacer les filtres
Effacer les filtres

nested if loop in for loop with different conditions?

6 vues (au cours des 30 derniers jours)
MUKESH KUMAR
MUKESH KUMAR le 21 Août 2019
Modifié(e) : MUKESH KUMAR le 22 Août 2019
this following code is OK
n = 0.8;
y = 1.6; % winding exponent y=1.6
K11 = 1;
K21 = 1;
K22 = 2;
T0 = 180;
Tw = 4;
% H = 1.4, gr = 14.5
Hgr = 20.3;
% ta = 30 ???
DTH_0i_ini = 12.7; % inital latter DTH_0i = DTH_0i
DTH_Hi_ini = 0 ; % inital latter DTH_Hi = Th
ThA = 30 + (45-30).*rand(48,1);
R = 1000;
% loading = read excel file and load data of Pek day 18th Aug 2022
% kVAR, kW, kVA, TEMP
rating = 990;
loading=600 + (1000-600).*rand(48,1);
K = loading./rating;
% calculate function f1, f2 and f3
time = linspace(30,1440,48);
for t= 1:48
f1(t) = 1 - exp(-time(t)/K11*T0);
f2(t) = K21*(1 - exp(-time(t)/K22*Tw))-(K21-1)*(1 - exp(-time(t)/(T0/K22)));
f3(t) = exp(-time(t)/(K11*T0));
end
Now error in following when applying the condtions, how can I set these conditions in nested if loop ,
When error im K(i-1) for i=1 can not calculated so we can take K initial is 1 and can apply K(i)>K(i-1) conditon from i=2:48.
DTH_0r = 38.3;
for i=1:48
if i==1
DTH_0i(i) = DTH_0i_ini;
DTH_Hi(i) = DTH_Hi_ini;
else
DTH_0i(i)= DTH_0i(i-1) + f1(i-1)*(DTH_0r * (((1+R*(K.^2) )/(1+R)).^n -DTH_0i(i-1)));
DTH_Hi(i) = Th(i-1);
% means when i=2,3,4... then DTH_Hi will be the Th(i-1) ,So first check K(i)>K(i-1) conditon and then find Th(i) from respective eqn and so on
if K(i)>K(i-1) % temp increse
Th(i) = ThA(i)+ DTH_0i(i) + f1(i)*(DTH_0r*(((1+R*(K.^2) )/(1+R)).^n -DTH0i(i))) + DTH_Hi(i) + (Hgr* K.^y - DTH_Hi(i))* f2(i);
else
Th(i) = ThA(i)+DTH_0r *((1+R*(K.^2))/R).^n + ( DTH_0i(i) - DTH_0r * ((1+R*(K.^2))/R).^n )* f3(i) + Hgr * K.^y;
end
end
end
  4 commentaires
Adam Danz
Adam Danz le 21 Août 2019
When I run your code, the error I get is
Unable to perform assignment because the left and right sides have a
different number of elements.
caused by this line
DTH_0i(i)= DTH_0i(i-1) + f1(i-1)*(DTH_0r * (((1+R*(K.^2) )/(1+R)).^n -DTH_0i(i-1)));
The right side of the equation produces a 48x1 vector (because K is 48x1) . But you're trying to put that into 1 element of the DTH_0i vector.
MUKESH KUMAR
MUKESH KUMAR le 22 Août 2019
Modifié(e) : MUKESH KUMAR le 22 Août 2019
There are two different equations for Th, When K(i)>K(i-1) then use first Th equation and otherwise use second one. and DTH_Hi will be the previous value of Th .
I tried this but is shows error of0' Index exceeds array bounds.'
DTH_0r = 38.3;
for i=1
DTH_0i(i) = DTH_0i_ini;
DTH_Hi(i) = DTH_Hi_ini;
K(i) = 1;
% temp increase eqn
Th(i) = ThA(i)+ DTH_0i(i) + f1(i)*(DTH_0r*(((1+R*(K.^2) )/(1+R)).^n -DTH_0i(i))) + DTH_Hi(i) + (Hgr* K.^y - DTH_Hi(i))* f2(i);
end
for i=2:48
DTH_0i(i)= DTH_0i(i-1) + f1(i)*(DTH_0r * (((1+R*(K.^2) )/(1+R)).^n -DTH_0i(i-1)));
DTH_Hi(i) = Th(i-1);
if K(i)>K(i-1)
% temp increse eqn
Th(i) = ThA(i)+ DTH_0i(i) + f1(i)*(DTH_0r*(((1+R*(K.^2) )/(1+R)).^n -DTH0i(i))) + DTH_Hi(i) + (Hgr* K.^y - DTH_Hi(i))* f2(i);
else
% temp decrease eqn
Th(i) = ThA(i)+DTH_0r *((1+R*(K.^2))/R).^n + ( DTH_0i(i) - DTH_0r * ((1+R*(K.^2))/R).^n )* f3(i) + Hgr * K.^y;
end
end

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Matrix Indexing 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