0
$\begingroup$

I have to calculate food ratios for meal plans, and they essentially break down into four requirements: carbs, protein, fats, and fiber. Each meal needs to have a certain amount of each, and contains a certain number of food items. Each food item provides a certain amount of each requirement. A small margin of error is allowed. I've tried to abstract the problem as follows (constants used as an example that represents how much each food item provides):

a(2(carbs) + 1(protein) + 3(fats) + 0.5(fiber)) + b(3(carbs) + 0(protein) + 1(fats) + 4(fiber))

Now I need to solve for a and b such that my result is (for example only - my equation is likely malformed) 100 carbs, 90 protein, 40 fats, 70 fiber. In practice, there will be more than two food items - likely closer to twenty. How do I approach this problem?

  • 0
    This is called a linear optimization problem, have a look at the simplex algorithm.2017-02-10
  • 0
    @Moo Good question, but something that wasn't presented to me as part of the problem.2017-02-10

1 Answers 1

0

One can construct a rectangular matrix, $R$ whose columns are the different foods, and whose rows are the amount a unit of that food contributes to each of carbs, protein, fats, and fiber. The goal is the find another matrix, $X$ which is a column vector and contains the desired amount of each food (which is to be solved for). The matrix product $RX$ is then set equal another column matrix, $Q$, which contains the required amounts of each of carbs, protein, fats, and fiber.

The solution to this problem can be a bit non-trivial, because the matrix, $R$ is non-square, and hence we cannot speak of a unique inverse to this matrix. If you can find exactly 4 of the available foods, such that the corresponding vector, whose entries are the values per unit of that food for each of carbs, protein, fats, and fiber are linearly independent (meaning that none of the 4 foods' vectors' can be represented as a linear combination of any of the other 3 foods vectors), then you are guaranteed that the matrix $R$ is square, and invertible, which allows for an easy solution, namely in such a case, the matrix equation, $RX=Q$ has solution, $X=R^{-1}Q$. It is likely that you will be able to find at least 4 foods whose vectors are linearly independent, so you should be able to find (probably quite a few) exact solutions.

To find 4 foods with linearly independent vectors, choose 4 foods and construct the 4x4 square matrix whose rows (or columns, doesn't matter which for our purposes) are the vectors for those 4 foods, and compute that matrix's determinant. So long as the determinant isn't $0$, then the 4 foods you chose are linearly independent, and you can use the matrix $R$ with those 4 foods only (instead of all foods) and compute a solution using the matrix equation: $X=R^{-1}Q$. You will likely want to use a calculator or software that can compute determinants and take inverses of square matrices (the computations are rather tedious to do by hand).