Eating a balanced diet using linear algebra, Part 2

Last week, I reduced a matrix to solve for the amount of each food you need to eat to reach various nutrient goals. Unfortunately, 4/10 of the variables were negative, which makes no sense in the real world because you can’t eat negative food. Today, I will post a workaround I have for this issue.

The problem was that there was only one solution for the system of equations. If there were more solutions, I could limit them so that each of the variables are positive. In other words, I need my system of equations to have free variables. To completely avoid negative values, I need at least as many free variables as there are fixed variables. Therefore, I will set up a new system of equations using 12 foods and 6 nutrients. Thanks to my friend who suggested 9 of these foods:

cooked pink salmon, cooked chicken thighs, fried eggs, boiled pinto beans, cooked rice, cooked russet potatoes, cooked broccoli, fresh apples, almonds, white bread, milk, butter

The 6 nutrients I will keep track of are:

fat, carbohydrates, fiber, sugar, protein, vitamins (highest vitamin percentage)

Also different from last week’s post, I will use the percentage of each nutrient in 1 kg (1000g) of a food. The target will be 100%, but because I am counting the highest of all vitamin percentages, the target for vitamins will be 300%. I will still use the nutrition facts provided by WolframAlpha.

Assembling the matrix, Part 2

Like last time, each food will represent a column of the matrix, and each row will represent a nutrient. The matrix will have dimensions 6×12. The foods I listed will be listed in order left to right, with the nutrients in order top to bottom.

[8122811510724280557321248100063878971194470170230100020360329212374449148501000012123445640818418428021004925022721805153585415168781710078830529143078177864721246242124500300]\left[\begin{array}{cccccccccccc|c} 81 & 228 & 115 & 10 & 7 & 2 & 4 & 2 & 805 & 57 & 32 & 1248 & 100\\ 0 & 6 & 3 & 87 & 89 & 71 & 19 & 44 & 70 & 170 & 23 & 0 & 100\\ 0 & 2 & 0 & 360 & 32 & 92 & 123 & 74 & 449 & 148 & 5 & 0 & 100\\ 0 & 0 & 12 & 12 & 3 & 44 & 56 & 408 & 184 & 184 & 280 & 2 & 100\\ 492 & 502 & 272 & 180 & 51 & 53 & 58 & 5 & 415 & 168 & 78 & 17 & 100\\ 788 & 305 & 291 & 430 & 78 & 177 & 864 & 72 & 1246 & 242 & 124 & 500 & 300\\ \end{array}\right]

Fully row-reduced, the matrix takes the form:

[100000−0.5250.483−7.1090.0450.395−12.581−0.778010000−3.9703.529−11.7091.5072.370−20.686−1.0790010008.210−6.90234.940−2.455−4.40660.0953.5340001000.615−2.3252.664−0.788−1.7133.843−0.0610000100.537−7.0252.789−1.460−4.74410.0330.084000001−1.17112.268−6.2615.1668.356−18.0761.320]\left[\begin{array}{cccccccccccc|c} 1 & 0 & 0 & 0 & 0 & 0 & −0.525 & 0.483 & −7.109 & 0.045 & 0.395 & −12.581 & −0.778 \\ 0 & 1 & 0 & 0 & 0 & 0 & -3.970 & 3.529 & −11.709 & 1.507 & 2.370 & −20.686 & −1.079 \\ 0 & 0 & 1 & 0 & 0 & 0 & 8.210 & −6.902 & 34.940 & −2.455 & −4.406 & 60.095 & 3.534 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0.615 & −2.325 & 2.664 & −0.788 & −1.713 & 3.843 & −0.061\\ 0 & 0 & 0 & 0 & 1 & 0 & 0.537 & −7.025 & 2.789 & −1.460 & −4.744 & 10.033 & 0.084 \\ 0 & 0 & 0 & 0 & 0 & 1 & −1.171 & 12.268 & −6.261 & 5.166 & 8.356 & −18.076 & 1.320 \end{array}\right]

I rounded all of the numbers to 3 decimal places to reduce unnecessary clutter. Next, we can label the 12 foods f1 through f12 and use these variables as parameters for the solution:

f1=−0.525f7+0.483f8−7.109f9+0.045f10+0.395f11−12.581f12−0.778f_1=-0.525f_7+0.483f_8-7.109f_9+0.045f_{10}+0.395f_{11}-12.581f_{12}-0.778
f2=−3.970f7+3.529f8−11.709f9+1.507f10+2.370f11−20.686f12−1.079f_2=-3.970f_7+3.529f_8-11.709f_9+1.507f_{10}+2.370f_{11}-20.686f_{12}-1.079
f3=8.210f7−6.902f8−24.940f9−2.455f10−4.406f11−60.095f12+3.534f_3=8.210f_7-6.902f_8-24.940f_9-2.455f_{10}-4.406f_{11}-60.095f_{12}+3.534
f4=0.615f7−2.325f8−2.664f9−0.788f10−1.713f11−3.843f12−0.061f_4=0.615f_7-2.325f_8-2.664f_9-0.788f_{10}-1.713f_{11}-3.843f_{12}-0.061
f5=0.537f7−7.025f8−2.789f9−1.460f10−4.744f11+10.033f12+0.084f_5=0.537f_7-7.025f_8-2.789f_9-1.460f_{10}-4.744f_{11}+10.033f_{12}+0.084
f6=−1.171f7+12.268f8−6.261f9+5.166f10+8.256f11−18.076f12+1.320f_6=-1.171f_7+12.268f_8-6.261f_9+5.166f_{10}+8.256f_{11}-18.076f_{12}+1.320

I researched the problem of keeping all variables non-negative, and I realized that I went down a rabbit hole typing out these equations.

Limiting all of the variables to be nonnegative is a very difficult task. It’s a strange optimization problem where I need to add 6 more variables and find the minimum of some function of them. I found a Matlab program that can perform this task. It’s called “Nonnegative Linear Least Squares”, which implies that it’s used in statistics for calculating a regression line, but it can also solve matrix equations where all variables are positive.

So, I made a Matlab account and opened the program:

To solve this problem, I had to define C to be the coefficient matrix and d to be the nutrition goal vector. Then, I ran the command that found the nonnegative solution.

This is the solution I got for the 12 foods:

A truly balanced diet, planned using linear algebra:

No salmon

No chicken thighs

No fried eggs

14 grams of pinto beans

186.5 grams of cooked rice

No potatoes

159.7 grams of broccoli

12.6 grams of apples

No almonds

462.4 grams of white bread (Roughly 12 slices)

No milk

57.3 grams of butter (Almost exactly half a stick of butter)

In conclusion, the math is suggesting that my diet should consist of rice and beans (But way more rice than beans), bread and butter, broccoli, and maybe an apple slice or two for dessert. This is a very carb-heavy diet, and I think this is because the FDA suggests 300 grams of carbohydrates per day, which feels like a lot. It’s also vegetarian, which is interesting. Considering everything, I would eat this way for maybe a week, and then I would get really bored of the same six foods and have to eat something else.


Comments

Leave a Reply