File Exchange

image thumbnail

Solar Position Calculator

version (57.6 KB) by Mark Mikofski
Basic analytical calculation of solar zenith and azimuth without atmospheric refraction.


Updated 26 Jul 2016

View License

%SOLARPOSITION Calculate solar position using most basic algorithm
% This is the most basic algorithm. It is documented in Seinfeld &
% Pandis, Duffie & Beckman and Wikipedia.
% returns ZENITH & AZIMUTH for all DATE & TIME pairs at LATITUDE, LONGITUDE.
% PHI_X is projection on x-z plane & PHI_Y is projection on y-z plane.
% cellstring or matrix N x [YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS] for N
% times.
% LATITUDE [degrees] and LONGITUDE [degrees] are the coordinates of the site.
% TIME_ZONE [hours] of the site.
% ROTATION [degrees] clockwise rotation of system relative to north.
% DST [logical] flag for daylight savings time, typ. from March to November
% in the northern hemisphere.
% References:
% Mark A. Mikofski
% Copyright (c) 2013

Cite As

Mark Mikofski (2020). Solar Position Calculator (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)

Jaco Luus

Im sure it works well if you can get it working, but the fact that there are no clear instructions on how to use this, makes it quite annoying

Jan Antoni Mikulski

Mark Mikofski

Imane, sorry for typos:
* "linters" --> "lines"
* "reply" --> "replace"

Also if you prefer MATLAB, please try pvlib-MATLAB:

Mark Mikofski

Hi Imane,

You also need to have "datefig" from the File Exchange:

Or you can remove linters from:
ax = datefig.ployy(...
to the end, which will also remove the plot

Or you can reply it with the built in MATLAB plotyy:
[ax, h1, h2] = plotyy (...
For more information see:

If you're okay with using Python, I would recommend pvlib instead:
There's already a solar position example in the gallery
You can ask questions on the Google group forum:!forum/pvlib-python
Or on stack overflow with the [pvlib] tag:

If you are a student, pvlib is participating in the Google Summer of Code with NumFOCUS:
There's more info here:

I hope this helps, good luck!


Mark Mikofski

Hi Imane,

I also realized that the example uses the NREL SOLPOS code which you would need to compile into a mex file. The MathWorks did not allow me to post this file on the File Exchange because of NREL's disclaimers, but you can get the mex file from my Gist here:

But as the directions at the top of the file say, you would also need the solpos source and headers which you can download for free from NREL here:

Then you can compile the NREL SOLPOS file using MATLAB built-in "mex" function:
mex solpos_mex.c
for more information see:

Again, if you are open to using Python, the NREL SOLPOS algorithm is also available here:
you can install it in any Python environment using this command: pip install SolarUtils

Also NREL makes SOLPOS available as an online calculator here:

The example only uses SOLPOS to compare the analytic calculations to more accurate SOLPOS model, so if you want, you can comment out the lines that call solpos() or use it's output.

If you have more questions, you can repost here, or on the pvlib google group, or you can try StackOverflow.

Take care,

imane ourraoui

I tried to run the program you posted but it doesn't work


Not easy to use at first instance, I was looping over single lines of lat / lon / date / time and had strange behaviour giving me 6 numbers. But then I figured it out (by looping over twin lines) and it's giving great results compare to the NREL/SPA ! Thanks for it Mark :)

Mark Mikofski

Hi QINGYANG LIU and others, The ROTATION parameter does not affect the ANGLES output which contains the (unrefracted) zenith and azimuth of the sun in a right-hand coordinate system with X to the east, Y to the north, and Z straight up. The ROTATION parameter represents an alternate coordinate system, call it XYZ-prime, that has X-prime and Y-prime rotated clockwise from X and Y. This affects the PROJECTION output, which are the angles on the ZX-prime plane and ZY-prime planes measured from Z. An example would be a solar PV power plant that was oriented 175 degrees from north (or southeasterly). The PROJECTION angles phi_x and phi_y are useful for calculating the angle of incidence of DNI on the PV panels or the position of a single axis tracker. Typically the X-prime axis would be aligned with a row of PV panels, and the Y-prime axis is perpendicular to them. Hope this helps! Thanks for using this. Also check out this cool MATLAB implementation by Meysam Mahooti of the NREL SPA algorithm:

Srinivas Kolluru

Sreelakshmi J

Mark Mikofski

Hi Steven (and others), I found the source code for the NREL solpos mex file. I pasted a gist of it online ( which you can append to the end of the NREL solpos.c source code which you can download for free ( you will also need the header file ( and to make the mex function work you'll need the appropriate compiler for your platform. (optionally you can comment or remove the void function S_decode from the original NREL solpos.c source code on lines 933 to 998 but I don't think it's necessary). The MATLAB mex file is based on NREL stest00.c source code (

Steven Hong

I'm trying to run your example, but where is the solpos function? Is it from NERL website? But they only have c code, which matlab doesn't recognize.

Mark Mikofski

There's also a Gist of the Solar Position Calculator on GitHub here:

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: SOLAR_GUI

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!