MATLAB Solving a Matlab Radiation Measurement Loop Over a Year

AI Thread Summary
To measure radiation over a year at a specific latitude using MATLAB, a nested loop structure is recommended. The outer loop should iterate over the 365 days, while the inner loop iterates through the 24 hours of each day. A common issue encountered is an "index out of bounds" error, which occurs when trying to access an element in an array that doesn't exist. In this case, the code attempts to access I(25), but the array I is only defined for 24 elements. To resolve this, ensure that the indexing corresponds to the defined size of the array. Additionally, it's advised to avoid using "and" statements within for loops in MATLAB. Instead, use a more straightforward approach to define ranges, such as "for j = 19:24". Improving code structure and reviewing MATLAB tutorials can enhance programming skills and prevent such errors.
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.
 

Similar threads

Replies
4
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
Back
Top