"fatboy")); include_once "calc_tools.php"; if(sizeof($_POST)) { // Autofill for me, cuz I'm lazy if(($_SERVER[REMOTE_ADDR]==$HOME_IP) && $_POST[t_kg]=="" && $_POST[t_lbs]=="") { $q = $db->query("select weight from weights order by id desc limit 1"); $r = $db->fetch_assoc($q); $_POST[s_lbs] = 350; $_POST[t_lbs] = $r[weight]; $_POST[g_lbs] = 200; $_POST[start] = "2017-01-01"; $_POST[feet] = 6; $_POST[inches] = 1; $_POST[gender] = "m"; $_POST[years] = 45; $_POST[activity_mod] = 1.2; $_POST[c_target] = 2500; } $smarty->assign("post", $_POST); $start_kg = (0+$_POST[s_kg])>0 ? 0+$_POST[s_kg] : $_POST[s_lbs]/2.2046; $today_kg = (0+$_POST[t_kg])>0 ? 0+$_POST[t_kg] : $_POST[t_lbs]/2.2046; $goal_kg = (0+$_POST[g_kg])>0 ? 0+$_POST[g_kg] : $_POST[g_lbs]/2.2046; $c_target = 0+$_POST[c_target]; $start_date = date("Y-m-d", strtotime($_POST[start])); $start_day = to_days($start_date); $today_date = date("Y-m-d"); $today_day = to_days($today_date); $total_days = $today_day - $start_day; $height = (0+$_POST[feet])>0 ? ((0+$_POST[feet])*30.48 + (0+$_POST[inches])*2.54) : $_POST[cm]; $gender = $_POST[gender]; $age = 0+$_POST[years]; $activity_mod = 0+$_POST[activity_mod]; if(FB_DEBUG) error_log("Gender: $gender Age: $age Height: $height cm Start Date: $start_date($start_day) Today: $today_date($today_day) Datediff: $total_days Start Weight: $start_kg kg Current Weight: $today_kg Goal Weight: $goal_kg Calorie Target: $c_target"); // First figure out caloric deficit per day average $c_def = ($start_kg - $today_kg) / $total_days * 7716; if(FB_DEBUG) error_log("Lost ".($start_kg-$today_kg)."kg in $total_days days, deficit of $c_def"); // Then find mid-weight caloric requirement via Miff/St Jeor $c_mid = miffstjeor(($start_kg+$today_kg)/2,$height,$age,$gender,$activity_mod); $c_proj = $c_mid - $c_def; $c_today = miffstjeor($today_kg, $height, $age, $gender, $activity_mod); $smarty->assign("c_proj", round($c_proj/5)*5); $smarty->assign("c_today", round($c_today/5)*5); if(FB_DEBUG) error_log("Mean caloric requirement: $c_mid - Projected daily intake @ $c_proj"); // From the projected daily, we can calculate the weight floor, again with miff/stjeor w = c/(10*mod) - .625*cm + .5*years - .5 (with w in kg) $floor_kg = miffstjeorkg($c_proj,$height,$age,$gender,$activity_mod); $tfloor_kg = miffstjeorkg($c_target,$height,$age,$gender,$activity_mod); if(FB_DEBUG) error_log("Weight floor: $floor_kg kg (".($floor_kg*2.2046).") lbs) Target floor: $tfloor_kg kg (".($tfloor_kg*2.2046).")"); // We have the floor, start weight and current weight, we can make our curve // y=abⁿ+c where y is weight, a is the initial weight, c is the weight floor, n is the number of days and b is the exponential function to be determined // when n=0 (first day), a=y-c (y being max weight, c being weight floor) $exp_a = $start_kg - $floor_kg; $texp_a = $start_kg - $tfloor_kg; // To find b, we take today's weight for y, number of days since max as n, $exp_a for a and weight floor for c. // $WEIGHT = $exp_a * b ^ ($TOTAL_DAYS_FROM_MAX) + $w_floor // b^($TOTAL_DAYS_FROM_MAX) = ($WEIGHT - $w_floor) / $exp_a // b = ($TOTAL_DAYS_FROM_MAX)√($WEIGHT - $w_floor) / $exp_a if(($today_kg - $floor_kg) < 0) $floor_kg = $today_kg-.001; $exp_b = pow((($today_kg - $floor_kg) / $exp_a), 1 / $total_days); // For target, find single day difference $c_start = miffstjeor($start_kg,$height,$age,$gender,$activity_mod); $tdayloss_kg = ($c_start - $c_target)/7716; $texp_b = 1 - ($tdayloss_kg / ($start_kg - $tfloor_kg)); if(FB_DEBUG) error_log("y=ab^n + c a=$exp_a b=$exp_b c=$floor_kg n=$total_days (results in metric, multiply y by 2.2046 for lbs)"); if(FB_DEBUG) error_log("y_t=ab^n + c a=$texp_a b=$texp_b c=$tfloor_kg n=1 (results in metric, multiply y by 2.2046 for lbs)"); // Eat to BMR calc $smarty->assign("bmr_day", round(miffstjeor($today_kg,$height,$age,$gender,1)/5)*5); $bmr_floor = miffstjeorkg(0,$height,$age,$gender,$activity_mod); $bmr_a = $start_kg - $bmr_floor; $bmr_losskg = (miffstjeor($start_kg,$height,$age,$gender,$activity_mod)-miffstjeor($start_kg,$height,$age,$gender,1))/7716; $bmr_b = 1 - ($bmr_losskg / $bmr_a); if(FB_DEBUG) error_log("y_b=ab^n + c a=$bmr_a b=$bmr_b c=$bmr_floor n=1"); // We now know y=abⁿ+c, so calculate date offsets (from max_date) for the target and goal weights // $target = $exp_a * $exp_b ^ n + $w_floor -> $exp_b ^ n = ($target-$w_floor)/$exp_a -> n = log(($target-$w_floor)/$exp_a) / log($exp_b) $target_offset = ceil(log(($goal_kg - $floor_kg)/$exp_a) / log($exp_b)); $target_days = $target_offset - 1; $target_date = date("Y-m-d",strtotime("$start_date +$target_offset day")); if(FB_DEBUG) error_log("Project days til target $goal_kg kg: $target_days ($target_date)"); $pngname = ip2long($_SERVER[REMOTE_ADDR]).".png"; $proc = proc_open("/usr/bin/gnuplot", array(0 => array("pipe", "r"), 1=>array("pipe", "w"), 2=>array("file", "/tmp/gnuplot.err", "a")), $pipes, "/tmp"); $pipe = $pipes[0]; if(intval($target_days)>0) { $tdays_plot = ", \"query("select unix_timestamp(d), weight into outfile '/home/fatboy/mysql_data/weights.txt' from weights where d>='$_POST[start]'"); } $ftime = strtotime($start_date)-946713600; $ttime = strtotime("$start_date +1 year")-946713600; $targtime = strtotime($target_date); if(intval($target_days)>0) $tdays_plot = ", \"assign("img", "/projection/$pngname"); } $smarty->display("projection.tpl");