MATLAB Answers

nested if loop in for loop with different conditions?

조회 수: 3(최근 30일)
MUKESH KUMAR
MUKESH KUMAR 21 Aug 2019
편집: MUKESH KUMAR 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;
% 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

표시 이전 댓글 수: 1
Adam Danz
Adam Danz 21 Aug 2019
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
Adam Danz 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
MUKESH KUMAR 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

댓글을 달려면 로그인하십시오.

답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by