Sale!

# MCEN 5125 Homework 4

\$30.00

MCEN 5125
Homework 4

1 Promblem 1: Diet Problem
The original diet problem data spreadsheet provides 64 different items with their corresponding Price, Calories,
Cholesterol, Total Fat, Sodium, Carbohydrates, Dietary Fiber, Protein, Vitamin A, Vitamin C, Calcium and Iron nutrients values per serving.
The second sheet provides the minimum and maximum value of those nutrients per day.
To be more clear on this problem, we have 11 nutrients and the price per serving as our variables. In addition to the
following constraints

Category:

## Description

MCEN 5125
Homework 4

1 Promblem 1: Diet Problem
The original diet problem data spreadsheet provides 64 different items with their corresponding Price, Calories,
Cholesterol, Total Fat, Sodium, Carbohydrates, Dietary Fiber, Protein, Vitamin A, Vitamin C, Calcium and Iron nutrients values per serving.
The second sheet provides the minimum and maximum value of those nutrients per day.
To be more clear on this problem, we have 11 nutrients and the price per serving as our variables. In addition to the
following constraints
• Each nutrients value for all food selections must greater than the minimum value.
• Each nutrients value for all food selections must smaller than the maximum value.
• The quantity for all food selections must equal or greater than zero.
Thus, we can conclude the problem as an LP in Inequality form:
minimize cT
x
sub ject to Ax 6 b
where c is the price per serving, x is the solution for the food selection with corresponding serving size. A is the
variable matrix which contains all the nutrients information. In addition the b is the constrains where contains the
minimum and maximum values for daily nutrients.
1.1 Part I
The first portion of the A matrix is the constrains equations which defines the minimum daily nutrients values needed.
CaloriesItem1 ∗ ServingSizeItem1 +CaloriesItem2 ∗ ServingSizeItem2 +···+CaloriesItem64 ∗ ServingSizeItem64 > Caloriesmin
CholesterolItem1 ∗ ServingSizeItem1 +CholesterolItem2 ∗ ServingSizeItem2 +···+CholesterolItem64 ∗ ServingSizeItem64 > Cholesterolmin
···
IronItem1 ∗ ServingSizeItem1 +IronItem2 ∗ ServingSizeItem2 +···+IronItem64 ∗ ServingSizeItem64 > Ironmin
It results the following matrix:
A1 =

CaloriesItem1 CaloriesItem2 CaloriesItem3 … CaloriesItem64
CholesterolItem1 CholesterolItem2 CholesterolItem3 … CholesterolItem64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
IronItem1 IronItem2 IronItem3 … IronItem64

and
b1 =

Caloriesmin
Cholesterolmin
. . . . . . . . . . . . .
IronImin

1
1.2 Part II
The second portion of the A matrix is the constrains equations which defines the maximum daily nutrients values
needed.
CaloriesItem1 ∗ ServingSizeItem1 +CaloriesItem2 ∗ ServingSizeItem2 +···+CaloriesItem64 ∗ ServingSizeItem64 6 Caloriesmax
CholesterolItem1 ∗ ServingSizeItem1 +CholesterolItem2 ∗ ServingSizeItem2 +···+CholesterolItem64 ∗ ServingSizeItem64 6 Cholesterolmax
···
IronItem1 ∗ ServingSizeItem1 +IronItem2 ∗ ServingSizeItem2 +···+IronItem64 ∗ ServingSizeItem64 6 Ironmax
It results the following matrix:
A2 =

CaloriesItem1 CaloriesItem2 CaloriesItem3 … CaloriesItem64
CholesterolItem1 CholesterolItem2 CholesterolItem3 … CholesterolItem64
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
IronItem1 IronItem2 IronItem3 … IronItem64

and
b2 =

Caloriesmax
Cholesterolmax
. . . . . . . . . . . . .
Ironmax

1.3 Part III
The last portion of the A matrix is the constrains equations which defines any serving size must equal or greater than
0.
ServingSizeItem1 > 0
ServingSizeItem2 > 0
···
ServingSizeItem64 > 0
It results the following matrix:
A3 =

1 0 0 … 0
0 1 0 … 0
. . . . . . . . . . . . . . . .
0 0 0 … 1

and
b3 =

0
0
.
0

1.4 Solution
In MATLAB, the default linprog solves:
X = linprog(f,A,b)
minimize f
0
x
sub ject to Ax 6 b
2
In order to solve the problem with matrix we have now, we need to form:
A =

−A1
A2
−A3

and
b =

b1
b2
b3

and
f =

PriceItem1
PriceItem2
. . . . . . . . . .
PriceItem64

It gives us the following solution:
• 0.24 serving of Carrots,Raw
• 3.54 serving of Potatoes, Baked
• 2.17 serving of Skim Milk
• 3.60 serving of Peanut Butter
• 4.82 serving of Popcorn,Air-Popped
which satisfies all the nutrients requirements:
• 2000.00 cal Calories
• 9.54 mg Cholesterol
• 65.00 g Total Fat
• 897.22 g Sodium
• 300.00 g Carbohydrates
• 75.59 g Protein
• 5000.00 IU Vitamin A
• 61.71 IU Vitamin C
• 800.00 mg Calcium
• 21.55 mg Iron
As the result, the total cost is 0.96 dollars.
1.5 Play with the Problem
However the diet looks terrible to me, so we can modify the result so that the diet meeting my preference. One way
to achieve is to modify the price for the item, increase the price for the item I don’t like so that the algorithm will not
pick them since it’s trying to minimum the total cost.
After the modification, I get the following result:
• 3.89 serving of Potatoes, Baked
• 3.60 serving of Oranges
• 2.49 serving of Chocolate Chip Cookies
• 0.79 serving of Scrambled Eggs
• 2.10 serving of Taco
• 0.34 serving of Splt Pea Hamsoup
which satisfies all the nutrients requirements:
• 2000.00 cal Calories
• 300.00 mg Cholesterol
• 63.04 g Total Fat
• 2347.49 g Sodium
• 300.00 g Carbohydrates
• 25.00 g Dietary Fiber
• 73.45 g Protein
• 5000.00 IU Vitamin A
• 318.43 IU Vitamin C
• 800.00 mg Calcium
• 24.39 mg Iron
3
Although the final cost increased to 2.40 dollars, but it is still super cheap, and now the diet looks more eatable.
2 Problem 2: Scaffolding Problem
We can start this problem by doing the statics analysis, we can obtain the following equations:
T3 +T4 −W2 = 0
−4T3 +4T4 = 0
T1 +T2 −T3 −W1 = 0
−8T1 +8T2 −4T3 = 0
In addition, each rope has the maximum tension it can take, so the tension on each rope mush smaller than the
maximum tension. Since we are solving this problem in Equality Form, we need to introduce some slack, now we
have the following equations:
T1 +S1 = 120
T2 +S2 = 160
T3 +S3 = 100
T4 +S4 = 100
where S are slack variables.
Also for all tension on ropes must greater than 0, we can do this in linprog by setting the lower bound.
2.1 Building Matrix
Now we can start building our matrix:
x =

T1
T2
T3
T4
W1
W2
S1
S2
S3
S4

From statics equations, we have:
A1 =

0 0 1 1 0 −1
0 0 −4 4 0 0
1 1 −1 0 −1 0
−8 8 −4 0 0 0

,

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

For maximum tension constrains, we have:
A2 =

1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0

,

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

finally,
A =

A1
A2

4
and b equal to:
b =

0
0
0
0
120
160
100
100

We are solving for maximum of W1 +W2,
f =

0
0
0
0
−1
−1
0
0
0
0

As the result, we have:
W1 = 170
W2 = 200
3 Appendix
3.1 MATLAB Code: Diet Problem
1 % MCEN 5125
2 % Homework 4
3 % Hanwen Zhao
4 % MEID: 650−703
5
6 c l e a r a l l
7 c l o s e a l l
8 c l c
9
10 % r e a d t h e e x c e l d a t a
11 [ ~ , ~ , f o o d ] = x l s r e a d ( ’ Di et P r o bl em D at a . x l s x ’ , 1 ) ;
12 [ ~ , ~ , n u t r i e n t ] = x l s r e a d ( ’ Di et P r o bl em D at a . x l s x ’ , 2 ) ;
13 % fo rm t h e f i r s t t h i r d A m a t r i x
14 % A1x >= bmin
15 % A1 n e e d s t o be n e g a t i v e s i n c e t h e d e f a u l t i n l i n p r o g i s <=
16 A1 = −1 ∗ t r a n s p o s e ( c e l l 2 m a t ( f o o d ( 2 : end , 2 : end −1) ) ) ;
17 % fo rm t h e s e c o n d t h r i d A m a t r i x
18 % A2x <= bmax
19 A2 = t r a n s p o s e ( c e l l 2 m a t ( f o o d ( 2 : end , 2 : end −1) ) ) ;
20 % fo rm t h e l a s t p o r t i o n o f t h e A m a t r i x
21 % I x >= 0
22 % Again t h e A3 n e e d s t o be n e g a t i v e
23 A3 = −1 ∗ e ye ( l e n g t h (A1 ) ) ;
5
24 % t h e f i s t h e p r i c e o f e a c h item , we want t o mi ni nmi z e t h i s
25 f = t r a n s p o s e ( c e l l 2 m a t ( f o o d ( 2 : end , end ) ) ) ;
26 % t h e f i r s t p a r t o f t h e b i s t h e minimum v a l u e o f n u t r i e n t s
27 % t h e b1 n e e d s t o be n e g a t i v e
28 b1 = −1 ∗ c e l l 2 m a t ( n u t r i e n t ( 2 : end , 3 : end −1) ) ;
29 % t h e s e c o n d p a r t o f t h e b i s t h e maximum v a l u e o f n u t r i e n t s
30 b2 = c e l l 2 m a t ( n u t r i e n t ( 2 : end , end ) ) ;
31 % t h e t h i r d p a r t o f t h e b m a t r i x a r e z e r o s
32 b3 = z e r o s ( l e n g t h (A1 ) , 1 ) ;
33 % now we ca n fo rm t h e f i n a l m a t r i x
34 A = [A1 ; A2 ; A3 ] ;
35 b = [ b1 ; b2 ; b3 ] ;
36 % c a l l l i n p r o g
37 r e s u l t = l i n p r o g ( f ,A, b ) ;
38 % g e t t h e i n d e x and q u a n t i t y o f t h e f o o d s e l e c t i o n
39 i n d e x = f i n d ( r e s u l t ~=0 ) + 1;
40 q u a n t i t y = r e s u l t ( i n d e x −1) ;
41 % p r i n t t h e r e s u l t
42 f p r i n t f ( ’ I n o r d e r t o meet t h e n u t r i e n t s r e q u i r e m e n t s , t h e f o l l o w i n g i t e m s a r e
s e l e c t e d : \ n ’ )
43 f o r i = 1: l e n g t h ( i n d e x )
44 f p r i n t f ( ’ %4.2 f s e r v i n g o f %s \ n ’ , q u a n t i t y ( i ) , f o o d { i n d e x ( i ) , 1 } )
45 end
46 % c a l c u l a t e t h e f i n a l n u t r i e n t v a l u e s
47 n u t r i e n t R e s u l t = −A∗ r e s u l t ;
48 n u t r i e n t R e s u l t = n u t r i e n t R e s u l t ( 1 : 1 1 ) ;
49 % p r i n t o ut t h e v a l u e s
50 f p r i n t f ( ’ \ n \ nThe f i n a l n u t r i e n t v a l u e s c o n t a i n s \ n ’ )
51 f p r i n t f ( ’ %4.2 f c a l C a l o r i e s \ n ’ , n u t r i e n t R e s u l t ( 1 ) )
52 f p r i n t f ( ’ %4.2 f mg C h o l e s t e r o l \ n ’ , n u t r i e n t R e s u l t ( 2 ) )
53 f p r i n t f ( ’ %4.2 f g T o t a l _ F a t \ n ’ , n u t r i e n t R e s u l t ( 3 ) )
54 f p r i n t f ( ’ %4.2 f g Sodium \ n ’ , n u t r i e n t R e s u l t ( 4 ) )
55 f p r i n t f ( ’ %4.2 f g C a r b o h y d r a t e s \ n ’ , n u t r i e n t R e s u l t ( 5 ) )
56 f p r i n t f ( ’ %4.2 f g D i e t a r y _ F i b e r \ n ’ , n u t r i e n t R e s u l t ( 6 ) )
57 f p r i n t f ( ’ %4.2 f g P r o t e i n \ n ’ , n u t r i e n t R e s u l t ( 7 ) )
58 f p r i n t f ( ’ %4.2 f IU Vit ami n A \ n ’ , n u t r i e n t R e s u l t ( 8 ) )
59 f p r i n t f ( ’ %4.2 f IU Vit ami n C \ n ’ , n u t r i e n t R e s u l t ( 9 ) )
60 f p r i n t f ( ’ %4.2 f mg Calci um \ n ’ , n u t r i e n t R e s u l t ( 1 0 ) )
61 f p r i n t f ( ’ %4.2 f mg I r o n \ n ’ , n u t r i e n t R e s u l t ( 1 1 ) )
62
63 f p r i n t f ( ’ \ n \ nThe t o t a l c o s t i s %4.2 f d o l l a r s . \ n ’ , f ∗ r e s u l t )
3.2 MATLAB Code: Scaffolding Problem
1 % MCEN 5125
2 % Homework 4
3 % Hanwen Zhao
4 % MEID: 650−703
5
6 c l e a r a l l
7 c l o s e a l l
8 c l c
9 % B u i l d i n g c o n s t r a i n m a t r i x f rom s t a t i c s e q u a t i o n s
10 A1 = [ 0 , 0 , 1 , 1 , 0 , −1;
11 0 , 0 , −4 , 4 , 0 , 0;
12 1 , 1 , −1 , 0 , −1 , 0;
6
13 −8 , 8 , −4 , 0 , 0 , 0 ];
14 A1 = [A1 , z e r o s ( 4 , 4 ) ] ;
15 % Add s l a c k v a r i b a l e s s o t h a t t e n s i o n i s s m a l l e r t h a n t h e maximum
16 A2 = [ e ye ( 4 , 6 ) , e ye ( 4 , 4 ) ] ;
17 % combine t h e p r e v i o u s p r o blem
18 A = [A1 ; A2 ] ;
19 % s t a t i c s e q u a t i o n s ha ve 0 on r h s
20 b1 = z e r o s ( 4 , 1 ) ;
21 % d e f i n e t h e maximum t e n s i o n s
22 b2 = [ 1 2 0 ; 1 6 0 ; 1 0 0 ; 1 0 0 ] ;
23 b = [ b1 ; b2 ] ;
24 % −1 f o r W1 and W2 s o we a r e mi nimi z e
25 f = [ z e r o s ( 4 , 1 ) ; −1; −1; z e r o s ( 4 , 1 ) ] ;
26 % c a l l l i n p r o g and s e t l ow e r bound t o 0
27 r e s u l t = l i n p r o g ( f , [ ] , [ ] , A, b , z e r o s ( 1 0 , 1 ) , [ ] ) ;
28 W1 = r e s u l t ( 5 )
29 W2 = r e s u l t ( 6 )
7