Solving a Matlab Radiation Measurement Loop Over a Year

Click For Summary

Discussion Overview

The discussion revolves around a Matlab coding issue related to measuring radiation over a year, specifically focusing on looping through hourly radiation values and addressing an index out of bounds error in the code.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks advice on how to implement a nested loop to iterate over 365 days with 24 hourly measurements each day.
  • Another participant suggests using a nested loop structure to achieve the desired iteration.
  • A participant shares their code and describes an issue with an index out of bounds error when trying to access an element of the array I that exceeds its defined size.
  • Another participant explains that the error arises from attempting to access an index of the array I that does not exist and offers suggestions for restructuring the code to avoid such errors.
  • There is a recommendation for the participant to review basic Matlab tutorials for better programming practices.

Areas of Agreement / Disagreement

Participants generally agree on the cause of the index out of bounds error and provide suggestions for resolving it, but there is no consensus on the best coding practices, as some participants offer differing approaches to structuring loops.

Contextual Notes

The discussion includes limitations related to the specific implementation of the code and assumptions about the structure of the data being processed. There are unresolved aspects regarding the overall logic of the radiation measurement calculations.

Who May Find This Useful

Individuals interested in Matlab programming, particularly those working on simulations or measurements in scientific contexts, may find this discussion relevant.

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 ·
Replies
4
Views
3K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K