Adjust ingredient weights based on overall macro split in recipe












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!










share|cite|improve this question











$endgroup$

















    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!










    share|cite|improve this question











    $endgroup$















      0












      0








      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!










      share|cite|improve this question











      $endgroup$




      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!







      linear-algebra






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Jan 12 at 19:56







      Mathias Lund

















      asked Jan 12 at 19:42









      Mathias LundMathias Lund

      101




      101






















          1 Answer
          1






          active

          oldest

          votes


















          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.






          share|cite|improve this answer











          $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





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3071302%2fadjust-ingredient-weights-based-on-overall-macro-split-in-recipe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|cite|improve this answer











          $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
















          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.






          share|cite|improve this answer











          $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














          0












          0








          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.






          share|cite|improve this answer











          $endgroup$



          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.







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Jan 12 at 20:00

























          answered Jan 12 at 19:54









          MarkMark

          2,04522449




          2,04522449








          • 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














          • 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








          1




          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$
          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$
          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$
          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




          $begingroup$
          thank you, will check it out!
          $endgroup$
          – Mathias Lund
          Jan 12 at 21:15


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3071302%2fadjust-ingredient-weights-based-on-overall-macro-split-in-recipe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith