problem with while condition

Hey everyone,
I am using a while loop to calculate some radiation data for one year. I don't have much experience using matlab and have never used a while loop before so I think I included at least one mistake in my code. The loop s=0:23 is supposed to calculate the radiation data for one day. If the height of the sun is below 0 degrees (like at night times) the radiation data Clearness_syn is supposed to be zero. If the solar height is more than 0 degress I need my while-loop. If the calculated radiation data is too small (below 0) or to large (larger than Clearness_max), I want matlab to go back and recalculate. It shouldnt go forward in the code as long as these two conditions aren't fullfilled. So in the end I should not get values that are below zero for Clearness_syn. But I do... So I am guessing my while condition/loop is somehow wrong??
for t=1:8760
.......................% calculation of the different needed parameters, i left this out here
dif=1; % Initial value for dif
while dif==1
for s=0:23
if gamma(t+s)<=0 % Gamma is the height of the sun in degrees
ypsilon(t+s)=(Clearness_day(t)-Clearness_mean(t))/standarddev(t); % Initial value
Clearness_syn(t+s)=0;
elseif gamma(t+s)>0
rnum=standarddev(t+s)*sqrt(1-Autokorr(t+s)^2)*((rand^0.135-(1-rand)^0.135)/0.1975);
ypsilon(t+s)=Autokorr(t+s)*ypsilon(t+s-1)+rnum;
Clearness_syn(t+s)=Clearness_mean(t+s)+standarddev(t+s)*ypsilon(t+s);
Clearness_max=0.88*cosd(pi*(hour(datum(t))-12.5)/30); % maximal allowed value vor Clearness_syn
Clearness_min=0; % minimal allowed value for Clearness_syn
C=find(Clearness_syn(t:t+s));
C=Clearness_syn(C);
C=mean(C);
if Clearness_syn(t+s)<Clearness_min
dif=1;
elseif Clearness_syn(t+s)>Clearness_max
dif=1;
else
dif=0;
end
end
end
end
Thanks alot!!

5 Comments

Isn't your while loop supposed to be inside the for loop (particularly inside the elseif), if I understood your description correctly.
In any case, is there anywhere in your code that is going to give you different results if you run it with the same inputs? If not, why would going back to recalculate the value make any difference?
I use random numbers within the code, that's why recalculations can make a difference...
I guess putting the while into my for loop would make sense. Although now I got the problem, that matlab only calculates the radation data for the first t+s in which the height of the sun is above zero degrees... So I think there is still something wrong here... Did I put that initial dif=1 at the right spot??
Show your modified code with the while loop inside the for loop. You're probably not very far off what you need.
I cancelled the whole thing now... thanks anyways :D

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 18 Nov 2014

Commented:

on 18 Nov 2014

Community Treasure Hunt

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

Start Hunting!