nested if loop in for loop with different conditions?

3 views (last 30 days)
MUKESH KUMAR on 21 Aug 2019
Edited: MUKESH KUMAR on 22 Aug 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;
% kVAR, kW, kVA, TEMP
rating = 990;
% 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
MUKESH KUMAR on 22 Aug 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