It is so liberating to be doing something different while using the tools that you know and come to appreciate. After a conversation with one of my old high school friends, this matlab code was written in about half a day, for the fun of it of course. This code should be capable of producing a "diet" plan based on a Proportional-Differential controller, and also outputs the predicted mass evolution. I would personally be testing it out and hopefully mathematics/physics will prevail. :-)
Edit: Added some references to the numbers in the source code after a suggestion from a friend.
function Diet_PD
%Using the Mifflin-St.Jeor model for BMR, modified with a linear interpolation of metabolic adaptation rate for calory intake changes. Controller scheme used is the Proportional-Differential controller (widely used in engineering)
clear all; clc;
%User inputs
t = 0:1:1*365;
h = 165; %Height (cm)
a = 27; %Age (years)
s = 1; %Gender (1 - Male, 2 - Female)
m0 = 75; %Initial mass (kg)
mtar = 60; %Target mass (kg)
phys_frac = 1.3; %1.2 <= Physical activity fraction <= 1.9 (Default: 1.2 = Sedentary, 1.375 = Light activity, 1.55 = Moderate, 1.725 = Very active, 1.9 = Professional/Hard labour)
Tmin = 1000; %Minimum calorie intake per day
Tmax = 5000; %Maximum calorie intake per day
%Controller gain constants (Trial & Error/Tuning methods)
K1 = 2000; %Proportional Controller gain
K2 = 10; %Derivative Controller gain
%Do not mess with the values after this line if not familiar
%Initial metabolic adaptation fraction calculation (Numbers from Martin et al, Human Physiology, doi:
10.1038/oby.2007.354., 2007)
x0 = [m0 met_ad_frac];
a1 = 1-1437/1523;
a2 = 1-1484/1514;
dadt = (1437/1523-1484/1514)/90;
met_ad_frac = ((a1+a2)-(270*dadt))/2;
if(s==1)
gen = 5;
else
gen = -161;
end
x0 = [m0 met_ad_frac];
[mplot] = odeft(@deriv_RHS,t,x0,5,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar);
figure(1)
plot(t,mplot(:,1),'ok');
xlabel('Days'); ylabel('Mass (kg)');
source = max(Tmin,K1*(-mplot(:,1)+mtar) + K2*phys_frac*(10*mplot(:,1)+6.25*h-5*a+gen)/7700);
source = min(Tmax,source);
figure(2)
plot(t,source,'ok');
xlabel('Days'); ylabel('Energy Intake (kCal)');
return
function [wdot] = deriv_RHS(t,x0,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar)
T = K1*(-x0(1)+mtar) + K2*phys_frac*(10*x0(1)+6.25*h-5*a+gen)/7700;
if(T < Tmin)
T = Tmin;
elseif(T > Tmax)
T = Tmax;
end
if(x0(2)<=0)
x0(2)=0;
end
wdot(1,1) = T/7700 - (1-x0(2))*phys_frac*(10*x0(1)+6.25*h-5*(a+t/365)+gen)/7700;
wdot(2,1) = dadt;
end
end
Edit: Added some references to the numbers in the source code after a suggestion from a friend.
function Diet_PD
%Using the Mifflin-St.Jeor model for BMR, modified with a linear interpolation of metabolic adaptation rate for calory intake changes. Controller scheme used is the Proportional-Differential controller (widely used in engineering)
clear all; clc;
%User inputs
t = 0:1:1*365;
h = 165; %Height (cm)
a = 27; %Age (years)
s = 1; %Gender (1 - Male, 2 - Female)
m0 = 75; %Initial mass (kg)
mtar = 60; %Target mass (kg)
phys_frac = 1.3; %1.2 <= Physical activity fraction <= 1.9 (Default: 1.2 = Sedentary, 1.375 = Light activity, 1.55 = Moderate, 1.725 = Very active, 1.9 = Professional/Hard labour)
Tmin = 1000; %Minimum calorie intake per day
Tmax = 5000; %Maximum calorie intake per day
%Controller gain constants (Trial & Error/Tuning methods)
K1 = 2000; %Proportional Controller gain
K2 = 10; %Derivative Controller gain
%Do not mess with the values after this line if not familiar
%Initial metabolic adaptation fraction calculation (Numbers from Martin et al, Human Physiology, doi:
10.1038/oby.2007.354., 2007)
x0 = [m0 met_ad_frac];
a1 = 1-1437/1523;
a2 = 1-1484/1514;
dadt = (1437/1523-1484/1514)/90;
met_ad_frac = ((a1+a2)-(270*dadt))/2;
if(s==1)
gen = 5;
else
gen = -161;
end
x0 = [m0 met_ad_frac];
[mplot] = odeft(@deriv_RHS,t,x0,5,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar);
figure(1)
plot(t,mplot(:,1),'ok');
xlabel('Days'); ylabel('Mass (kg)');
source = max(Tmin,K1*(-mplot(:,1)+mtar) + K2*phys_frac*(10*mplot(:,1)+6.25*h-5*a+gen)/7700);
source = min(Tmax,source);
figure(2)
plot(t,source,'ok');
xlabel('Days'); ylabel('Energy Intake (kCal)');
return
function [wdot] = deriv_RHS(t,x0,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar)
T = K1*(-x0(1)+mtar) + K2*phys_frac*(10*x0(1)+6.25*h-5*a+gen)/7700;
if(T < Tmin)
T = Tmin;
elseif(T > Tmax)
T = Tmax;
end
if(x0(2)<=0)
x0(2)=0;
end
wdot(1,1) = T/7700 - (1-x0(2))*phys_frac*(10*x0(1)+6.25*h-5*(a+t/365)+gen)/7700;
wdot(2,1) = dadt;
end
end
No comments:
Post a Comment