File Exchange

image thumbnail

Sun/Moon Rise/Set

version (1.05 MB) by Meysam Mahooti
Rising and setting times of Sun and Moon and twilight times


Updated 04 Feb 2018

View License

The program Sunset calculates the rising and setting times of the Sun and the Moon, as well as the beginning and ending of twilight over a period of 10 days. Data to be entered are the starting date, the geographical coordinates of the observing site, and the difference between Local Time and Universal Time.
The SinAlt.m calculates the sine of the solar or lunar altitude at hourly intervals. These values are interpolated in Quad.m and examined for zero points. If a root is found, and the Sun or Moon was below the horizon at the beginning of the day, then the event is a sunrise or moonrise. Otherwise, the Sun or Moon is setting. Finally, it may happen that two zero points are discovered in the interval being examined. To determine which of these two points is a rising and which a setting, we check to see if the altitude at the vertex is above or below the horizon. The process continues until the end of the day. It is then possible to determine whether the celestial body being considered is circumpolar, or whether it remains below the horizon for the entire day.
The times of civil, nautical, or astronomical twilight are calculated in a similar manner, in that a constant value of sin(−6◦), sin(−12◦) or sin(−18◦) is subtracted from the sine of the altitude of the horizon.
Montenbruck O., Pfleger T.; Astronomy on the Personal Computer; Springer Verlag, Heidelberg; 4th edition (2000).

Cite As

Meysam Mahooti (2020). Sun/Moon Rise/Set (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)


Accuracy is improved using planetary positions (SunPos.m and MoonPos.m) with respect to true equator and equinox of date rather than mean equator and equinox of date.

SinAlt.m is modified and CityName.m is added to give the name of city you have entered its geographical latitude and longitude.

Description is updated.

The image is added.

Quad.m is improved.

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