nested if loop in for loop with different conditions?
Show older comments
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 Comments
Joel Handy
on 21 Aug 2019
What is the rule for the first element TH? You may need initialize your loop first and start at i = 2.
DTH_0i = DTH_0i_ini;
DTH_Hi = DTH_Hi_ini;
% TH = some equation to set initial value of TH
for i=2:48
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);
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
Quick note: there's no such thing as an "if loop". "If" statements are called conditionals. They can be nested within for-loops or while-loops. But an if-else-end statement doesn't iterate.
Whenever you mention an error, you must include the entire copy-pasted error message (all of it).
Adam Danz
on 21 Aug 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
on 22 Aug 2019
Edited: MUKESH KUMAR
on 22 Aug 2019
Answers (0)
Categories
Find more on Loops and Conditional Statements 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!