1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {[m, d] =} easter (y)
18 ## @deftypefnx {Function File} {datenum =} easter (y)
20 ## Return the month (@var{m}) and day (@var{d}) of Easter in the
21 ## Gregorial calendar on a given year or years.
26 function varargout = easter (y)
28 ## This uses the Meesus/Jones/Butcher Gregorian algorithm as described
29 ## on http://en.wikipedia.org/wiki/Computus#Algorithms
35 f = floor ((b + 8)/25);
36 g = floor ((b - f + 1)/3);
37 h = mod ((19*a+b-d-g+15), 30);
40 L = mod ((32 + 2*e + 2*i - h - k), 7);
41 m = floor ((a + 11*h + 22*L)/451);
42 mon = floor ((h + L - 7*m + 114)/31);
43 day = 1 + mod ((h + L - 7*m + 114), 31);
46 varargout = {mon(:), day(:)};
48 varargout{1} = reshape (datenum (y(:), mon(:), day(:)), size (y));
54 ## Validate that it calculates the correct date for a decade
55 %!assert(easter(1990), datenum(1990, 4, 15))
56 %!assert(easter(1991), datenum(1991, 3, 31))
57 %!assert(easter(1992), datenum(1992, 4, 19))
58 %!assert(easter(1993), datenum(1993, 4, 11))
59 %!assert(easter(1994), datenum(1994, 4, 3))
60 %!assert(easter(1995), datenum(1995, 4, 16))
61 %!assert(easter(1996), datenum(1996, 4, 7))
62 %!assert(easter(1997), datenum(1997, 3, 30))
63 %!assert(easter(1998), datenum(1998, 4, 12))
64 %!assert(easter(1999), datenum(1999, 4, 4))
65 ## Validate vector and matrix inputs
66 %!assert(easter([2000 2001]), [datenum(2000, 4, 23) datenum(2001, 4, 15)])
67 %!assert(easter([2002;2003]), [datenum(2002, 3, 31);datenum(2003, 4, 20)])
68 %!assert(easter([2004 2005;2006 2007;2008 2009]), [datenum(2004, 4, 11) datenum(2005, 3, 27);datenum(2006, 4, 16) datenum(2007, 4, 8);datenum(2008, 3, 23) datenum(2009, 4, 12)])
69 %!assert(easter([2002;2003]), [datenum(2002, 3, 31);datenum(2003, 4, 20)])