奇门遁甲起局Matlab代码

奇门遁甲起局Matlab代码

2021年底,西安疫情突然加重,许多高校封闭,学生被封在宿舍无法外出,线下考试也推迟了,下学期的课也没有安排上,总不能一直闲着,又因为最近对奇门遁甲这门术数学挺感兴趣,刚学习了奇门拆补法手动起局方法,想着这样繁琐的过程能不能转换成代码,于是,就耗费两天时间编写了下面的奇门遁甲起局程序。需要的道友可以私信来起一局看看运势,或者对matlab感兴趣的也可以来互相交流,这里因篇幅限制未放上全部代码(有600多行,太多了害),私信会以文档形式分享完整代码。

函数式M文件部分代码:

function [sizhuzhuanhuanjieguo,dun,jushu]=qimendunjia(year,month,day,time0,fen0,jieqi)

tiangan=’甲乙丙丁戊己庚辛壬癸甲乙丙丁戊己庚辛壬癸’;%十天干双排矩阵,双排是为了排布天盘干和天盘干是转排使用

dizhi=’子丑寅卯辰巳午未申酉戌亥子丑寅卯辰巳’;%十二地支矩阵,为了确定四柱地支

%确定年柱

y=mod((year-3),10);

if(y==0)

nianganshu=10;

else

nianganshu=y;

end %用公式确定年干

z=mod((year-3),12);

if(z==0)

nianzhishu=12;

else

nianzhishu=z;

end %用公式确定年支

niangan=tiangan(nianganshu);%呈现年干字符

nianzhi=dizhi(nianzhishu);%呈现年支字符

%确定月柱

if(nianganshu==1|nianganshu==6)

k=3;

else if(nianganshu==2|nianganshu==7)

k=5;

else if(nianganshu==3|nianganshu==8)

k=7;

else if(nianganshu==4|nianganshu==9)

k=9;

else if(nianganshu==5|nianganshu==10)

k=1;

end

end

end

end

end %以上使用五虎遁法确定正月起始天干位置

yueganshu=k-1+month-1;%从正月开始数,确定所测月份的月干数

yuezhishu=2+month-1; %正月建寅,所以从寅月开始数,确定所测月份的月支数

yuegan=tiangan(yueganshu);%呈现月干字符

yuezhi=dizhi(yuezhishu);%呈现月支字符

%确定日柱

if(mod(month,2)==0)

M=30;

else

M=0;

end

flag=(mod(year,4)==0&mod(year,100)~=0)|mod(year,400)==0;%确定是否为闰年

if(flag==0)

if (month==1|month==4|month==5)

shengyushu=1;

else if(month==2|month==6|month==7)

shengyushu=2;

else if(month==3)

shengyushu=0;

else if(month==8)

shengyushu=3;

else if(month==9|month==10)

shengyushu=4;

else if(month==11|month==12)

shengyushu=5;

end

end

end

end

end

end

else

if (month==2|month==4|month==5)

shengyushu=1;

else if(month==6|month==7)

shengyushu=2;

else if(month==3|month==1)

shengyushu=0;

else if(month==8)

shengyushu=3;

else if(month==9|month==10)

shengyushu=4;

else if(month==11|month==12)

shengyushu=5;

end

end

end

end

end

end

end

G=(year-1900)*5+floor((year-1900)/4)+9+M+day+shengyushu;%日干支计算重要公式

riganshu0=mod(mod(G,60),10);

rizhishu0=mod(mod(G,60),12);

if(riganshu0==0)

riganshu=10;

else riganshu=riganshu0;

end

if(rizhishu0==0)

rizhishu=12;

else

rizhishu=rizhishu0;

end

rigan=tiangan(riganshu);

rizhi=dizhi(rizhishu);

%确定时柱

fen=fen0/100;

time=time0+fen;

if((time>=0&&time<1)|(time>=23&&time<24))

shizhishu=1;

p=1;

else if(time>=1&&time<3)

shizhishu=2;

p=2;

else if(time>=3&&time<5)

shizhishu=3;

p=3;

else if(time>=5&&time<7)

shizhishu=4;

p=4;

else if(time>=7&&time<9)

shizhishu=5;

p=5;

else if(time>=9&&time<11)

shizhishu=6;

p=6;

else if(time>=11&&time<13)

shizhishu=7;

p=7;

else if(time>=13&&time<15)

shizhishu=8;

p=8;

else if(time>=15&&time<17)

shizhishu=9;

p=9;

else if(time>=17&&time<19)

shizhishu=10;

p=10;

else if(time>=19&&time<21)

shizhishu=11;

p=11;

else shizhishu=12;p=12;

end

end

end

end

end

end

end

end

end

end

end %以上先确定时辰对应地支

if (riganshu==1|riganshu==6)

q=1;

else if(riganshu==2|riganshu==7)

q=3;

else if(riganshu==3|riganshu==8)

q=5;

else if(riganshu==4|riganshu==9)

q=7;

else q=9;

end

end

end

end %以上使用五鼠遁来确定该日子时的天干排位

shiganshu=q-1+p; %从子时开始朝后数,直到到规定时辰,确定时干

shigan=tiangan(shiganshu); %呈现时干字符

shizhi=dizhi(shizhishu);%呈现时支字符

%输出结果

fprintf(‘阳历%d年%d月%d日%d时%d分对应的四柱八字如下:\n’,year,month,day,time0,fen0);

sizhuzhuanhuanjieguo=[niangan,nianzhi,’年’,yuegan,yuezhi,’月’,rigan,rizhi,’日’,shigan,shizhi,’时’];

disp(sizhuzhuanhuanjieguo);

fprintf(‘—————————————————————————————-\n’);

%确定阴遁阳遁与局数

……

起局结果:

Join The Discussion

Compare listings

Compare