Mathematical model - weight loss model based on difference equation (based on python)

background

        At present, the more standard standard for evaluating weight is the BMI index, which is equal to the square of weight divided by height. my country's reference standard believes that a BMI index of less than 18.5 is thin, 18.5 to 23.9 is normal, 24 to 27.9 is overweight, and greater than 28 is obese.

        Under normal circumstances, the body's calorie intake through food is roughly balanced with the calorie consumption of metabolism and exercise, so the body weight remains basically unchanged, and when the energy conservation is broken, it will cause weight changes.

        Weight loss plans are considered in terms of reducing calorie intake and increasing exercise

model assumptions

 1. Weight gain is proportional to the calories absorbed, with an average weight increase of 1kg per 8000kcal

2. The weight loss caused by the normal metabolism of the body is proportional to the body weight. The calorie consumption per kilogram of body weight per week is generally between 200kcal and 320kcal, and it varies from person to person. This is equivalent to a person weighing 70kg who consumes 2000kcal to 3200kcal per day

3. Exercise-induced weight loss is proportional to body weight and related to exercise form and exercise time 

4. For safety and health, it is best not to absorb less than 10,000kcal of calories per week, and the weekly reduction should not exceed 1,000kcal, and the weekly weight loss should not exceed 1.5kg 

Calories burned per kilogram of body weight per hour of exercise
    sports foot run dance waist bicycle swim
 calorie consumption 3.1 7.0 3.0 4.4 2.5 7.0

Note: The data is for reference only

basic model

        Record the body weight in the kth week (beginning) as w(k)(kg), and the calorie intake in the kth week as c(k)(kcal), k=1, 2.... Let the heat conversion coefficient be α. The metabolic consumption coefficient of the deep body is β. According to the assumptions of the model, the basic equation of weight change under normal circumstances (regardless of exercise) is (as long as this equation can be expressed, the main part is basically done, and a while loop or a for loop can be used)

w(k+1)=w(k)+\alpha c(k)-\beta w(k),k=1,2...

        According to hypothesis 1, \alpha=\frac{1}{8000}kg/kcal, after determining a person's metabolic consumption coefficient β, it can be pushed from the weekly intake of calories c(k) to the change of ta body weight w(k) according to the above formula. When increasing exercise, it is necessary to change the consumption The value of coefficient β will be given below

example

        A person is 1.7 meters tall, weighs 100kg, and has a BMI of 34.6. It is known that he absorbs 20,000kcal of calories per week, and his weight has not changed for a long time. The goal is to reduce his weight to 75kg and maintain it.

        At this time, set a two-stage plan. In the first stage, the intake of calories will be reduced from 20,000kcal to 10,000kcal; in the second stage, the intake of calories will be maintained until the weight loss goal is reached (or increase exercise to speed up the process)

        Therefore, based on the above information, the following information can be drawn:

c=2000kcal,w=100kg,\beta=\frac{\alpha c}{w}=\frac{20000/8000}{100}=0.025

        According to the requirements of the first stage, the absorbed heat should be reduced to 10000, so c(k) can be obtained

c(k)=20000-1000k,k=1,2...

        At this point, by bringing in the basic model, you can get the difference equation about body weight

w(k+1)=(1-\beta)w(k)+\alpha (2000-1000k)=0.975w(k)+2.5-0.125k

        Taking w(1)=100kg as the initial value, and programming according to the above formula, the weight at the beginning of the 11th week is 93.616kg

Code

        Define a loseplan function for making plans and displaying weekly weight changes. Of course, first determine whether the BMI index is too large. The parameters are weight, height, and calorie intake. The default calorie reduction per week is 1000kcal. Sport is set, which is the second stage of weight change under exercise.

def losePlan(weight,height,c,lose=1000,sport=False,category='跳舞'):
    bmi = round(weight/height**2)
    if bmi > 26:
        goalw = 26 * height ** 2
        print('您目前体重指数为{},属于超重范围,正在为您计算第一阶段减食计划所用时间'.format(bmi))
        print('您的目标体重为{}'.format(round(goalw, 2)))

        Secondly, α is given and known and will not change, β is different according to each person

a = 1 / 8000  # 热量转换系数
beta = a * c / weight  # 代谢消耗系数

        Next, I enter the stage of the difference equation. I use a while loop to make a difference. If I use a for loop, I think it is necessary to calculate the time before proceeding. The whlie loop does not. So define how many weeks it takes to get a time acquisition to complete the first stage

time = 0
while c != 10000:
    c = c - lose
    time += 1
    weight = weight * (1 - beta) + a * (20000 - 1000 * time)
    print('第{}周减少摄入热量为{},此时体重{}'.format(time,c,round(weight, 3)))
print('第一阶段减食所需时间为{}周'.format(time))

        At this time, write the parameters and run the code to get the weight change of the first stage every week, as shown in the figure below

 

second stage

        In the case of no exercise, the lower limit of the weekly calorie scale is required to be 10000kcal, so the equation at this time is

w(k+1)=(1-\beta)w(k)+\alpha c_{min}=0.975w(k)+1.25,k=11,12...

        The second stage ends when the body weight is reduced to 75.14kg or less

        If you want to speed up the process, you need to increase exercise. According to the previous exercise consumption table, record the calorie consumption as γ, exercise t hours a week, then change β in the basic model to β+αγt, and the equation at this time is

w(k+1)=w(k)+\alpha c(k)-(\beta +\alpha \gamma t )w(k) 

        Let the exercise method be running, 8 hours a week (actually, who runs for so long...)

if sport:
    sport_cate = {'foot': 3.1, 'run': 7.0, 'dance': 3.0,
                'pq': 4.4, 'bicycle': 2.5, 'swim': 7.9}
    for i in sport_cate.keys():
        if category == i:
            s = sport_cate[category]

            while weight > 75:
                weight = weight + a * c - weight * (beta + a * s * 8)
                time += 1
                print("第{}周的体重为{}".format(time, round(weight, 3)))

        # 假设不运动
else:
    while weight >= 75:
        a = 1 / 8000
        weight = weight * 0.975 + a * c
        time += 1
    print("第{}周的体重为{}".format(time, round(weight, 3)))

        With codes for results, it takes another 22 weeks to reach target weight when not exercising, and 15 weeks when exercising.

 

full code

def losePlan(weight,height,c,lose=1000,sport=False,category='跳舞'):
    bmi = round(weight/height**2)
    if bmi > 26:
        goalw = 26 * height ** 2
        print('您目前体重指数为{},属于超重范围,正在为您计算第一阶段减食计划所用时间'.format(bmi))
        print('您的目标体重为{}'.format(round(goalw, 2)))
        a = 1 / 8000  # 热量转换系数
        beta = a * c / weight  # 代谢消耗系数
        time = 0
        while c != 10000:
            c = c - lose
            time += 1
            weight = weight * (1 - beta) + a * (20000 - 1000 * time)
            print('第{}周减少摄入热量为{},此时体重{}'.format(time,c,round(weight, 3)))
        print('第一阶段减食所需时间为{}周'.format(time))

        if sport:
            sport_cate = {'foot': 3.1, 'run': 7.0, 'dance': 3.0, 'pq': 4.4, 'bicycle': 2.5, 'swim': 7.9}
            for i in sport_cate.keys():
                if category == i:
                    s = sport_cate[category]

                    while weight > 75:
                        # weight = weight + a * c - weight * (beta + a * s * 8)
                        weight = 0.97*weight + 1.25
                        time += 1
                        print("第{}周的体重为{}".format(time, round(weight, 3)))

        # 假设不运动
        else:
            while weight >= 75:
                a = 1 / 8000
                weight = weight * 0.975 + a * c
                time += 1
                print("第{}周的体重为{}".format(time, round(weight, 3)))

    else:
        print('您的体重指数为{},体重正常'.format(bmi))


if __name__ == '__main__':
    weight = 100
    height = 1.7
    c = 20000
    name = 'run'
    losePlan(weight,height,c,sport=True,category=name)

        

Summarize

        The code is not very long, but enough. When I was sorting out later, I found that it would be better to start exercising from the first stage, and the original book also expressed it like this, I didn’t notice it, so the code part is more correct only when the second stage does not exercise .

        If you have any doubts about the code, you can private message or comment in the comment area.

Bibliography: Jiang Qiyuan, Xie Jinxing and Ye Jun. Mathematical Models (Fifth Edition)

Guess you like

Origin blog.csdn.net/qq_60471758/article/details/124403388