Solving a Matlab Radiation Measurement Loop Over a Year

In summary, the conversation discussed a code in Matlab that measures radiation on a specific latitude over a year using nested loops. The code also dealt with a recurring problem of index out of bounds and offered advice on structuring code better. The speaker recommended reading basic Matlab tutorials to improve programming skills.
  • #1
ShaunaLB
2
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
  • #2
Yes, just use a nested loop. For example;

Code:
for d = 1:365
   for h = 1:24
      blahh...
   end
end
 
  • #3
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
 
  • #4
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.
 
  • #5


I understand the importance of accurately measuring radiation over a long period of time. It seems like you have a good start with your code and parameters for the radiation per hour. To solve your problem of looping the 24-hour iteration 365 times, I would suggest using a for loop that iterates over the 365 days in the year. Within this loop, you can include your 24-hour iteration loop and have it run for each day. This will ensure that your code measures the radiation for each day over the entire year. Additionally, you may want to consider storing your radiation measurements in an array or matrix for easier analysis and visualization. I hope this suggestion helps you in solving your Matlab radiation measurement loop over a year. Best of luck!
 

1. How do I set up a radiation measurement loop in Matlab?

To set up a radiation measurement loop in Matlab, you will need to define the variables for the start and end dates, as well as the interval of time between measurements. Then, use a for loop to iterate through each time interval and collect the radiation data using appropriate functions and calculations.

2. How can I plot the radiation data collected over the year?

To plot the radiation data, you will first need to store it in an array or matrix. Then, use the plot() function to create a line graph or the scatter() function to create a scatter plot. You can also customize the plot by adding labels, titles, and adjusting the axes.

3. How can I calculate the average radiation level for each month?

To calculate the average radiation level for each month, you will first need to group the data by month using the month() function. Then, use the mean() function to calculate the average for each month. You can also plot the monthly averages on a bar chart using the bar() function.

4. Is there a way to export the data from Matlab to a spreadsheet?

Yes, you can use the xlswrite() function in Matlab to export your data to an Excel spreadsheet. Make sure to specify the file name and the data range you want to export.

5. How can I include error bars in my radiation data plot?

To include error bars in your plot, you will need to calculate the standard deviation of your data using the std() function. Then, use the errorbar() function to plot the data with error bars. You can also customize the appearance of the error bars by specifying the color, width, and style.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top