%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab script to plot biorhythm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% t0 is the birthdate
% t1 is the current date
%

t0 = datenum('Oct 66, 1961');
t1 = datenum('Aug. 28, 2017');

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set an interval around current date: 28 days before, and after t1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

t = (t1-28):1:(t1+28);

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define biorhythms over that interval
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

y1 = 100*sin(2*pi*(t-t0)/23);
y2 = 100*sin(2*pi*(t-t0)/28);
y3 = 100*sin(2*pi*(t-t0)/33);

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generate a figure showing the biorhythms
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

plot(t,y1,'LineWidth',2);
hold on
plot(t,y2,'LineWidth',2);
plot(t,y3,'LineWidth',2);

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% label the plots:
% the Xticks should be given as dates, not numbers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

datetick('x','mm/dd');

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw a vertical line at the current date
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

x = [t1 t1];
y = [-100 100];
h=line(x,y);
set(h,'Color',[0 0 0]);

legend('Physical','Emotional','Intellectual');

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set a title for the plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

s=datestr(t0);
s1 = ['Birthday:' s];
title(s1)

r= datestr(t1);
xlabel(r);

axis tight;

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the "reset" day: t0 + 23*28*33
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

t_reset = t0 + 23*28*33;
s_reset=datestr(t_reset);

disp(['The reset day is ' s_reset]);

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Finding the optimum day
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

time=0:1:23*28*33;
y1_all = 100*sin(2*pi*time/23);
y2_all = 100*sin(2*pi*time/28);
y3_all = 100*sin(2*pi*time/33);
sum_y = y1_all + y2_all + y3_all;

[smax,t_max]=max(sum_y);
%
%
t_max = t0 + t_max;
%
s_max=datestr(t_max);

disp(['The optimum day will be ' s_max]);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% draw biorhythm near that optimum day
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

t = (t_max-28):1:(t_max+28);

y1 = 100*sin(2*pi*(t-t0)/23);
y2 = 100*sin(2*pi*(t-t0)/28);
y3 = 100*sin(2*pi*(t-t0)/33);

figure
plot(t,y1,'LineWidth',2);
hold on
plot(t,y2,'LineWidth',2);
plot(t,y3,'LineWidth',2);
%
datetick('x','mm/dd');

x = [t_max t_max];
h=line(x,y);
set(h,'Color',[0 0 0]);

legend('Physical','Emotional','Intellectual');

r= datestr(t_max);
xlabel(r);

axis tight;
