Inverse Matrices: A Food Calculator

The forward problem was: eat amounts v of foods A, B, C and compute the nutrient totals Nv. This page solves the inverse problem: first choose the nutrient totals t you want for carbohydrate, fat, and protein, then ask how much of each food should be eaten. That is, solve Nv = t for the unknown v. When N is invertible, the answer is unique:

Nv = t   ⇒   v = N−1t,     N−1N = NN−1 = I.

1. The nutrient matrix N — three food labels, side by side

Edit any entry (grams per 100 g of food). Column = food, row = nutrient.

g per 100 g Food A Food B Food C
Carbohydrate
Fat
Protein
N  =
N1,1N1,2N1,3 N2,1N2,2N2,3 N3,1N3,2N3,3
entries Ni,j  (row i = nutrient, column j = food)
=

2. The target vector t — the nutrients you want to eat

Slide to choose the desired total grams of each nutrient for the meal.

Carbohydrate
Fat
Protein
t  =
t1 t2 t3
entries ti  (row i = nutrient)
=

3. Does N−1 exist? — the determinant decides

A 3×3 matrix is invertible exactly when det N ≠ 0, where

det N = N1,1(N2,2N3,3N2,3N3,2)
det N = N1,2(N2,1N3,3N2,3N3,1)
det N = + N1,3(N2,1N3,2N2,2N3,1).

det N = 0 — N is singular.
Therefore, N−1 does not exist. Depending on t, the system Nv = t has either no solution or infinitely many solutions.

Since det N ≠ 0, the inverse exists:

N−1 =
1 / det N
·
adj(N)

Here adj(N) is the adjugate matrix of N.

4. The solution v = N−1t — how much of each food to eat

1 / det N
·
adj(N)
·
t
=
v = N−1t

5. Check: Nv = t — undoing the inverse

Feed the computed amounts back into the forward problem. Multiplying by N undoes multiplying by N−1: N(N−1t) = (NN−1)t = It = t. The target vector is recovered exactly.

N
·
v
=
Nv

Back to Collection