File Exchange Pick of the Week

July 16th, 2010

sun_position.m

Bob's pick this week is sun_position.m by Vincent Roy.

Here at MathWorks headquarters in Natick, MA we are entering the dog days of Summer. Like many others to help stay cool during the day I minimize unwanted solar heating through windows with shades and blinds. Of course that conserves energy which also saves money!

From a technical aspect you can calculate the position of the Sun. Given a place and time sun_position does the math for you. For example, where is my Noon Sun today?

here.longitude = -71.34993; %deg
here.latitude  =  42.30013; %deg
here.altitude  =  55;       %m
noonToday = datenum('2010-07-16 12:00');
sun_position(noonToday,here)
ans = 
     zenith: 63.368
    azimuth: 84.585

Wikepedia helped get my head around what zenith and azimuth mean.

Thanks to Vincent for making that so easy for me to use. His submission also has a lot of good feedback from others. I appreciate submissions like this where people collaboratively make good work even better as the "updates" section makes clear. Keep up the great work, everyone!

Do you use solar calculations? Tell us about it here.


Get the MATLAB code

Published with MATLAB® 7.10

8 Responses to “sun_position.m”

  1. Ted Driver replied on :

    The picture you have of zenith is correct, it is always 90 degrees above your horizon (straight up, opposite of nadir), the angle the Sun makes at it’s highest point (not always at noon) is called usually called the elevation angle.
    Also, the highest point of the Sun’s elevation is called the transit time, and is not always at noon. See my web link (a java applet) that shows the actual elevation values at the Sun Transit time.
    Example: for your location specified, for July 17, the Sun Transit time is 12:51:35 (local), with an elevation of 68.829 degrees.

  2. Joachim replied on :

    I used this to calculate whether to buy real estate 20m left or right, since there will be higher buildings next to ours at some point in future. Calculating sun position helped to determine at what time in the morning we will get sun.

  3. Petter replied on :

    Your example is misleading. You are not calculating the position of the Sun for your noon. Look at the angles.

    The error is that the time is not specified in your particular time zone.

  4. Bob replied on :

    Ted, thanks for the information. Great point that noon is not necessarily highest.

    Joachim, very cool application – thanks for sharing!

    Petter, so whose Noon is 12:00 at my location?

  5. Petter replied on :

    I meant with respect to time zones. I noticed that the sun wasn’t at its highest point at 12:00. I assumed to was a time zone problem, since the time zone isn’t specified anywhere.

  6. Bob replied on :

    Rookie mistake: local time threw me. Eastern time zone is 5 hours from UTC, and we’re on Daylight Savings Time now. So Noon on my watch really means 12:00 UTC +(5-1=)4 hours.

    >> sun_position(noonToday+(5-1)/24,here)
    ans =
    zenith: 23.6043
    azimuth: 148.7892

    Where noonToday was really last Friday for consistency. That correction is close to Ted's transit elevation.

    >> transitTime = datenum('2010-07-16 12:51:35')+4/24;
    >> p = sun_position(transitTime,here)
    p =
    zenith: 20.9967
    azimuth: 180.0608
    >> 90-p.zenith
    ans =
    69.0033

    Close but I'm feeling picky. Anyone see what I might still be missing?

  7. Ted Driver replied on :

    Bob,
    It looks like you took the Transit time for the 17th (from my example above) and used it to calculate the zenith angle for the 16th. The transit time changes a little bit everyday. Your azimuth shows this too – it’s a little off of 180 degrees.
    Using the date of 7/17 in your example:

    >> transitTime = datenum('2010-07-17 12:51:35')+4/24;
    >> p = sun_position(transitTime,here)
    
    p = 
    
         zenith: 21.1656445823035
        azimuth: 180.004396015302
    
    >> 90 - p.zenith
    
    ans =
    
              68.8343554176965
    

    Which is much closer. I only have the transit time to the second precision, there are probably more decimal places in the time, which may give us closer answers. By the way, my Java Applet (referenced in my link) uses Jean Meeus’ algorithms as well, from his Astronomical Algorithms book. He has many test cases in the book that you can try too. Nice job!

  8. Bob replied on :

    Ted, good catch. So I nailed the time but was a full day off. (blush) If it was a snake, it would have bit me! LOL

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.