0
$\begingroup$

I have a math problem that I've been struggling with for a while, and I hope you guys can help me figure this out.

Say that I have a recipe containing the 3 ingredients with varying amount of grams of each ingredient:

Oats

Total: 38g

Protein: 10g, Carbs: 20g, Fat: 8g

Blueberries

Total: 2g

Protein: 0g, Carbs: 2g, Fat: 0g

Peanut Butter

Total: 6g

Protein: 3g, Carbs: 2g, Fat: 1g

In total, this is 13g of protein, 24g of carbs, and 9g of fat. The percentage split is: 28% protein, 52% carbs and 20% fat.

I wan't to be able to change the percentage split dynamically. So for instance, say that I would like 40% protein, 40% carbs and 20% fat in the whole recipe - how would I update the weights of each ingredient to end up with that particular split in the whole recipe?

Looking forward to the responses!

$\endgroup$
0
$\begingroup$

Let $o = (10, 20, 8)^T, b = (0, 2, 0)^T, p = (3, 2, 1)^T$

You are seeking $c_1, c_2, c_3$ such that $c_1 o + c_2 b + c_3 p = (0.4, 0.4, 0.2)^T$

You can solve equations like this using linear algebra. Just create a matrix with $[o|b|p]$ as columns and solve $[o|b|p](c_1, c_2, c_3)^T = (0.4, 0.4, 0.2)^T$ by inverting the matrix.

The amounts $c_1, c_2, c_3$ will give correct proportions but will then need to all be scaled up depending on how hungry you are. Be careful with the units also, each unit of $c_1$ specifies $38g$ of oats, each unit of $c_2$ means $2g$ of blueberries, and each unit of $c_3$ specifies $6g$ of peanut butter. The resulting recipe will have $0.4g$ of protein, $0.4g$ of carbs, and $0.2g$ of fat.

If the matrix does not have an inverse, you might still be able to solve the system by using the pseudoinverse, but you will likely be able to fix it by diversifying and adding more source ingredients.

$\endgroup$
  • 1
    $\begingroup$ That makes so much sense! Thank you so much! Will try to build a function in python right away, and get back to you :) $\endgroup$ – Mathias Lund Jan 12 at 20:06
  • $\begingroup$ hi again @Mark, wouldn't your solution only work if there are exactly three ingredients in the recipe? because if I add another one, I add another c4 and then I need to add another row to the percentage split vector - and there are only three variables of interest in that one (carbs, fat, protein) $\endgroup$ – Mathias Lund Jan 12 at 21:03
  • $\begingroup$ Yeah that's a good point I didn't think about. In general that will mean there are infinitely many solutions. To enumerate all of them, find one of them and then add on the kernel of the matrix. Or just simply don't use your least favorite one. But you might consider ranking the solutions by price and finding the cheapest one. To do that, use Linear Programming. You might find this tutorial interesting benalexkeen.com/linear-programming-with-python-and-pulp-part-4 $\endgroup$ – Mark Jan 12 at 21:09
  • $\begingroup$ thank you, will check it out! $\endgroup$ – Mathias Lund Jan 12 at 21:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.