I am trying to make an algorithm within Matlab. I have 4 constraints that I need to hit: any suggestions are appreciated.

1 view (last 30 days)
Is there a code that I can develop to the following problem.
Each entry has attributes of calories, protein, fats, carbs.
Is there a way in Matlab to find the multipliers x,y,z,v for portions sizes so that the attributes hit the 4 sum constraints in red?
Screenshot 2019-03-19 at 12.41.59.png

Answers (1)

Steven Lord
Steven Lord on 19 Mar 2019
No, not without involving negative amounts of some of these foodstuffs.
Consider if you ate just one of these entries exclusively. That would require 2000/30 portions of pasta, 2000/70 of bread, 2000/100 of chicken, or 2000/15 of avocado to reach the 2000 calories you want to achieve. How much fat would you have in each of those scenarios?
portions = 2000./[30 70 100 15];
fat = portions.*[5 6 7 8];
That puts you well over the limit of 15 for your fat intake; the minimum you could do would be 140 for a diet of pure chicken. That would also violate your protein and carb constraints, providing 400 of each.
If you tell us more about the problem (are those constraints equality or inequality constraints, and are they minimum requirements or maximum allowances?) and/or show us the code you've written we may be able to offer other suggestions. But since this sounds like a homework assignment, without seeing code we'll probably just offer pointers to useful functions.
  1 Comment
Jram
Jram on 19 Mar 2019
Thanks Steven, the numbers I added were arbitrary, just to make the point, not real values. Ideally there would be way more food entries to use multipliers for and target requirements could have a +- 10% on them.
Is there a way to loop through multipliers until all constraints are met in matlab? Do you have any suggestions?
Thanks a lot
G

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!