李英冰農曆
標籤: MATLAB 萬年曆 分類: [E]【 MATLAB 】 2006-12-22 09:47
基於MATLAB的萬年曆計算
黃小川(200431610102)
測繪工程專業2004級9班
摘要:運用MATLAB編程實現1936—2032年農曆與公曆間的相互轉換,並求當日是星期幾。
關鍵詞:萬年曆 公曆轉化為農曆 農曆轉化為公曆 求當天是星期幾
正文:
1. 概述:知道公曆年月日求農曆日期,知道農曆年月日求公曆日期,並求當日是星期幾。
2. 相關知識及原理:
公曆:公曆是現在國際通用的曆法,又稱格列歷,通稱陽曆。公曆將一年分為十二個月,其中一、三、五、七、八、十、十二月有31天,俗稱「大月」;四、六、九、十一月有30天;平年二月有28天,閏年二月有29天,這樣公曆閏年一年有366天,平年有365天。
公曆閏年:若一年年數不能被4整除,或能被100整除但不能被400整除,則稱這一年為平年,否則為閏年。
農曆:農曆是基於對地球公轉和月相變化的觀測的。我國現採用的農曆結合了陽曆與陰曆(伊斯蘭曆)的記年方法:一年有十二個月,其中6個大月(30天),6個小月(29天),一個農曆年有354天。
農曆閏月:為了平衡公里年與農曆年的天數差別,農曆採用在適當時候設立閏月的方法,一般三年左右設一閏月,閏月可以是大月,也可以是小月。具體設置原則由節氣而定,方法較為複雜。
公曆與農曆間的轉換:公曆是純數學的記年法,而農曆是基於觀測結果之上的。兩者見的轉換比較複雜,無法用簡單的數學公式表示。因此,這裡採用的方法是首先查得1936年至2032年相關的資料,包括:該年農曆大年初一到公曆元旦的天數、該年農曆各月的天數、該年農曆有無閏月以及在何處閏月。這樣在公曆化為農曆時,只需算得改日距該年農曆大年初一的天數,對應該年農曆各月的天數,即可得到農曆的月份,及該日在該農曆月是第幾天;在農曆化為公曆時,同樣,需算得該日距該年元旦的天數,通過類似轉化,即可求得公曆日期。
求星期數:已知公曆求星期數時,計算出改日距一特定日期的天數,則該日星期數=特定日期星期數+距離天數除以7的餘數;已知農曆時,只需將農曆轉換為公曆,再由公曆日期求星期數即可。
3. 實例計算:
3.1數據說明:
runyear %1936到該年的閏年數
gongli_year %公曆年數
gongli_mongth %公曆月數
gongli_day %公曆日期
gongli_sumofdays %公曆各月累積天數
gongli_daysofyear %該日是該公曆年的第幾天
nongli_year %農曆年數
nongli_month %農曆月數
nongli_day %農曆日期
nongli_sumofdays %農曆各月累積天數
nongli_daysofyear %該日是該農曆年的第幾天
3.2MATLAB源程序:
%getdayofweek.m
%由公曆日期求星期數
runyear=floor((gongli_year-1933)/4); %到1936年的閏年數
weekday=rem((gongli_year-1936-runyear)*365+runyear*366
+gongli_sumofdays(gongli_month)+gongli_day,7)+1; %求星期數
weekdayoutput=["二","三","四","五","六","日","一"];
fprintf("這一天是星期%s,",weekdayoutput(weekday))
%gongli_input
%輸入公曆日期
gongli_year=input("請輸入一公曆個年份:");
while (gongli_year-1936)*(gongli_year-2032)>0; %輸入年份不符合要求
gongli_year=input("豬頭!年份應在1936至2032之間,請再輸一次:");
end
gongli_month=input("請輸入一個公曆月份:");
while (gongli_month-1)*(gongli_month-12)>0 %輸入月份不符合要求
gongli_month=input("豬頭!月份應在1至12之間,請再輸一次:");
end
sign=0; %判斷是否是閏年
if rem(gongli_year,4)==0
sign=sign+1;
end
if sign==1
gongli_sumofdays=[0,31,60,91,121,152,182,213,244,274,305,335,366];
else
gongli_sumofdays=[0,31,59,90,120,151,181,212,243,273,304,334,365];
end
gongli_day=input("請輸入一個公曆日期:");
while (gongli_day-1)*(gongli_day-(gongli_sumofdays(gongli_month+1)
-gongli_sumofdays(gongli_month)))>0
gongli_day=input("豬頭!這個月沒有這個日期,請再輸一次:");
end
%gongli2gongli.m
%將公曆日期轉化為農曆日期
gongli_daysofyear=gongli_sumofdays(gongli_month)+gongli_day;
%求該日是當公曆年的第幾天
if gongli_daysofyear<wannianli_matrix(gongli_year-1935,1)
%該日農曆年數比公曆年數少一
nongli_year=gongli_year-1;
if rem(gongli_year-1,4)==0
nongli_daysofyear=366+gongli_daysofyear-wannianli_matrix(nongli_year-1935,1);
else nongli_daysofyear=365+gongli_daysofyear-wannianli_matrix(nongli_year-1935,1);
end
else nongli_year=gongli_year;
%該日農曆年數與公曆年數相同
nongli_daysofyear=gongli_daysofyear-wannianli_matrix(nongli_year-1935,1);
end
nongli_sumofdays=0;nongli_month=3;
while nongli_daysofyear-1>nongli_sumofdays
nongli_sumofdays=nongli_sumofdays+wannianli_matrix(nongli_year-1935,nongli_month);
nongli_month=nongli_month+1;
end
nongli_day=nongli_daysofyear-(nongli_sumofdays-wannianli_matrix(nongli_year-1935,nongli_month-1));
nongli_month=nongli_month-3;
nongli_tiangan=["庚","辛","壬","癸","甲","乙","丙","丁","戊","已"];
nongli_dizhi=["申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"];
if wannianli_matrix(nongli_year-1935,2)~=0&nongli_month>wannianli_matrix(nongli_year-1935,2)
nongli_month=nongli_month-1;
if nongli_month==wannianli_matrix(nongli_year-1935,2)
fprintf("農曆%s%s年閏%1.0f月%1.0f日。
",nongli_tiangan(rem(nongli_year,5)+1),nongli_dizhi(rem(nongli_year,12)+1),nongli_month,nongli_day)
else fprintf("農曆%s%s年%1.0f月%1.0f日。
",nongli_tiangan(rem(nongli_year,5)+1),nongli_dizhi(rem(nongli_year,12)+1),nongli_month,nongli_day)
end
else fprintf("農曆%s%s年%1.0f月%1.0f日。
",nongli_tiangan(rem(nongli_year,5)+1),nongli_dizhi(rem(nongli_year,12)+1),nongli_month,nongli_day)
end
%nongli_input
%輸入農曆日期
nongli_year=input("請輸入一個農曆年份:");
while (nongli_year-1936)*(nongli_year-2031)>0; %輸入年份不符合要求
nongli_year=input("豬頭!年份應在1936至2031之間,請再輸一次:");
end
nongli_month=input("請輸入一個農曆月份:");
while (nongli_month-1)*(nongli_month-12)>0%輸入月份不符合要求
nongli_month=input("豬頭!月份應在1至12之間,請再輸一次:");
end
if wannianli_matrix(nongli_year-1935,2)==nongli_month %輸入月份和當年閏月相同
answer=input("是閏月嗎?(y/n):","s"); %判斷是否為閏月
while answer~="y"&answer~="n"
answer=input("到底是不是啊?(y/n):","s");
end
if answer=="y"
nongli_month=nongli_month+1;
end
elseif wannianli_matrix(nongli_year-1935,2)~=0&wannianli_matrix(nongli_year-1935,2)~=nongli_month
if nongli_month>wannianli_matrix(nongli_year-1935,2)
nongli_month=nongli_month+1;
end
end
nongli_day=input("請輸入一個農曆日期:");
while (nongli_day-1)*(nongli_day-wannianli_matrix(nongli_year-1935,nongli_month+2))>0
nongli_day=input("豬頭!這個月沒有這個日期,請再輸一次:");
end
sign=0;
if rem(nongli_year,4)==0
sign=sign+1;
end
if sign==1
gongli_sumofdays=[0,31,60,91,121,152,182,213,244,274,305,335,366];
else
gongli_sumofdays=[0,31,59,90,120,151,181,212,243,273,304,334,365];
end
gongli_year=nongli_year;
%nongli2gongli
%將農曆日期轉化為公曆日期
nongli_daysofyear=nongli_day; %求該日是當農曆年的第幾天
if nongli_month~=1
for k=1:nongli_month-1
nongli_daysofyear=nongli_daysofyear+wannianli_matrix(nongli_year-1935,k+2);
end
end
gongli_daysofyear=nongli_daysofyear+wannianli_matrix(nongli_year-1935,1);
if gongli_daysofyear>gongli_sumofdays(13) %該日公曆年數比農曆年數多一
gongli_daysofyear=gongli_daysofyear-gongli_sumofdays(13);
gongli_year=nongli_year+1;
end
gongli_month=1;
while gongli_daysofyear-1>gongli_sumofdays(gongli_month)
gongli_month=gongli_month+1;
end
gongli_month=gongli_month-1;
gongli_day=gongli_daysofyear-gongli_sumofdays(gongli_month);
getdayofweek
fprintf("%1.0f年%1.0f月%1.0f日",gongli_year,gongli_month,gongli_day)
%wannianli.m
%主程序
wannianli_matrix=[23, 3, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 29, 384
41, 0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 354
30, 7, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 384
49, 0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 354
38, 0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 354
26, 6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 384
45, 0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 355
35, 0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 354
24, 4, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 385
43, 0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 354
32, 0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 354
21, 2, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 384
40, 0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 354
28, 7, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 384
47, 0, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 0, 354
36, 0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 355
26, 5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 384
44, 0, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 29, 0, 354
33, 0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 355
23, 3, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 384
42, 0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 354
30, 8, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 29, 383
48, 0, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 355
38, 0, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, 354
27, 6, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 384
45, 0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 0, 355
35, 0, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 354
24, 4, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 384
43, 0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 355
32, 0, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 353
20, 3, 30, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 384
39, 0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 355
29, 7, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 384
47, 0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 354
36, 0, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 0, 355
26, 5, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 384
45, 0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 0, 354
33, 0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 354
22, 4, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 384
41, 0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 354
30, 8, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 384
48, 0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 0, 354
37, 0, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 355
27, 6, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 384
46, 0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 355
35, 0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 354
24, 4, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 384
43, 0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 354
32, 10, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 384
50, 0, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 354
39, 0, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 0, 354
28, 6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 384
47, 0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 355
36, 0, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 0, 355
26, 5, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 384
45, 0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 354
34, 0, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 354
22, 3, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 383
40, 0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 355
30, 8, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 30, 384
49, 0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 0, 354
37, 0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 355
27, 5, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 384
46, 0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 354
35, 0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 0, 354
23, 4, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 384
42, 0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 354
31, 0, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 0, 355
21, 2, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 384
39, 0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 354
28, 7, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 385
48, 0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 354
37, 0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 354
25, 5, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 384
44, 0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 354
33, 0, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 354
22, 4, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 384
40, 0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 355
30, 9, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 384
49, 0, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 29, 0, 354
38, 0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 355
27, 6, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 384
46, 0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 354
35, 0, 29, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 0, 354
24, 4, 29, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 384
42, 0, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, 354
31, 0, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 0, 355
21, 2, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 384
40, 0, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 354
28, 6, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 384
47, 0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 29, 0, 354
36, 0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 354
25, 5, 30, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 384
43, 0, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 29, 0, 354
32, 0, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 0, 355
22, 3, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 384];
%根據天文觀測得到的參數矩陣,每一行為一年(1936-2032)
%第一列為當年公曆元旦距農曆春節的天數
%第二列為當年在哪一個月閏月,若為0則沒有閏月
%第三到十五列為每個月的天數(閏月在所閏月的後面)
%第十六列為該年天數(本次編程沒有用到)
s=input("請選擇你要進行的計算(1/2):1.由公曆算農曆2.又農曆算公曆
");
while s~=1&s~=2
s=input("豬頭!請選擇1或2,再輸入一次:")
end
switch s
case 1
gongli_input
getdayofweek
gongli2nongli
case 2
nongli_input
nongli2gongli
end
3.3結果輸出:
4.結論與討論:
經過多次實驗,本程序進行公農曆轉化和求星期數的結果基本上符合事實,但在個別日期上會出錯,比如1932和2032年附近靠近邊界的日期、農曆閏月的日期等。
可以加以改進的地方:
1.從農曆產生的根源入手,探討地、月球的運動,計算農曆計年的規律,從而無限擴大查詢日期的範圍。
2.實現可視化和互動式界面,以日曆方式輸出。
3.加如節日和農曆節氣的顯示。
參考文獻
1. 對公曆和農曆的認識參考百度網www.baidu.com
2. 程序的部分思想及觀測矩陣wannianli_matrix參考CSDN社區
http://topic.csdn.net/t/20030106/16/1331826.html
體驗新版博客推薦閱讀:
※6月16日農曆是幾月幾?什麼星座?運勢如何?
※農曆五月有吉星照命的三大生肖
※為什麼農曆一月叫正月?十二月叫臘月?
※出生在農曆幾月的女人最旺夫,前五名,有你嗎 ~~?
※說說女人農曆生日尾數幾的人富貴一生
TAG:農曆 |