Loop a 24-hour radiation cycle 365 times in Matlab

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
3 replies · 3K views
ShaunaLB
Messages
2
Reaction score
0
I have a code in Matlab where I need to measure the radiation on a specific latitude over a year. I have my time steps in hours and I am iterating over a year. I have parameters for the amount of radiation per hour in a 24 hour period, but my problem is that I want to loop this 24 iteration loop 365 times. Any suggestions??
Thanks in advance
 
Physics news on Phys.org
Yes, just use a nested loop. For example;

Code:
for d = 1:365
   for h = 1:24
      blahh...
   end
end
 
thanks,
I also have a reocurring problem with my index being out of bounds. here is my code
clear
clf
n=8760;
dt=1/24;
Vp=2.9;
alpha=0.025;

for i=1:n
for j=1:24
if j>=1 && j<5;
I(j)=1;
end
if j>=5 && j<7;
I(j)=978 +1;
end
if j>=7 && j<17;
I(j)=1955.7;
end
if j>=17 && j<19;
I(j)=-978 + 1955.7;
end
if j>=19 && j<=24
I(j)=1;
end
end
F(i)=5*I(i);

end
plot (F)

and I keep getting the error:
? Attempted to access I(25); index out of bounds because
numel(I)=24.

Error in ==> Itest2 at 26
F(i)=5*I(i);

if the issue of index out of bounds could also be explained that would be greatly helpful
 
The problem is that you are trying to access a dimension of the array I that doesn't exist. You can't call the value form I(25) because it only goes to 24.

Also, you need to structure your code better. Matlab isn't fortran or the original C. For loops should never contain "and" statements. If you want to loop from value 19 to 24 for example, then go like this;

Code:
for j = 19:24
   do stuff...
end

%or for steps of 2

for j = 18:2:24
   do stuff
end

I recommend reading some MATLAB basic tutorials to help get you up to speed with programming.