Linear Programming Using MATLAB: Maximization Problems Group #1
Linear Programming Using MATLAB: Maximization Problems Group #1
Maximization Problems
Group #1
Group Members
• 792 Naveerah Masood • 840 Rabia Aslam
• 796 Hira Sohail • 841 Fizza Farooq
• 801 Rameen Yaqoob • 857 Huma Ijaz
• 804 Zonia Shareef • 866 Habiba Bashir
• 806 Saman Saif • 869 Amna Khanum
• 807 Farkhana Idress • 873 Iqra Waris
• 808 Maria Yamin • 870 Ghania Khan
• 822 Afifa Mariam • 881 Saba
• 828 Khadijha Zafar • 882 Maria Yousaf
• 833 Zunaira Rasheed • 891 Madiha
• 834 Rafia Nadeem • 892 Haleema Nasir
• 899 Habiba Syed
OUTLINE
• What is MATLAB?
• Varibles
• Matrixes
• Operations
• Functions
• Linear Programming
• LP using MATLAB
• Maximization problems
What is Matlab?
• Matlab is basically a high level language
which has many specialized toolboxes for
making things easier for us.
• Matlab stands for MATrix Matlab
LABoratory.
High Level
Languages such as
C, Pascal etc.
Assembly
Matlab Screen
Variables
• Don’t have to declare type
• Don’t even have to initialise
• Just assign in command window
>>
>> a=12; % variable a is assigned 12
Matlab comment
prompt operator
suppress
assign
command
operator
output
Matrices
• Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A= square brackets to define
3 2 1 matrices
5 1 0
semicolon for next row in
2 1 7 matrix
Generating Matrix from functions
x = zeros(2,2)
• zeros(M,N) MxN matrix of zeros x =
0 0
0 0
x = ones(2,3)
x =
• ones(M,N) MxN matrix of ones 1 1 1
1 1 1
x = rand(1,3)
x =
• rand(M,N) MxN matrix of uniformly 0.9501 0.2311 0.6068
distributed random
numbers on (0,1)
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
' complex conjugate transpose
Manipulating Matrices
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A% matrix division
>> B./A % element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
Matrices Operations
What is Function?
A function is a group of statements that together perform a task. In MATLAB,
functions are defined in separate files.
A function file is also an M-file, just like a script file,
Functions can accept more than one input arguments and may return more than one
output arguments.
It has a function definition line on the top that defines the input and output explicitly.
The names of the file and of the function should be the same.
Functions operate on variables within their own workspace.
Types of function in the MATLAB
• Anonymous Functions
• Primary and Subfunctions
• Private Functions
• Nested Functions
How to Create function in matlab
Linear Programming
• Linear programming is a method that is used to find a minimum or
maximum value for a function. That value is going to satisfy a
known set of conditions constraints. Constraints are the inequalities
in the linear programming problem. Their solution is graphed as a
feasible region, which is a set of points.
• Linear programming is often used in business to find maximum
profit or minimum cost. The first step in solving linear programming
problems is to set up a function that represents cost, profit, or some
other quantity to be maximized or minimized subject to the
constraints of the problem.
Linear Programming
Mathematical formulation
The variables x1, x2, ... xn satisfy the inequalities
a11 x1 a12 x2 a1n xn b1
a21 x1 a22 x2 a2 n xn b2
am1 x1 am 2 x2 amn xn bm
and x1 ≥0, x2 ≥0, ... xn ≥0 . Find the set of values of x1, x2, ...
xn that minimizes (maximizes)
x1 f1 x2 f 2 xn f n
Note that apq and fi are known
Linear Programming in MATLAB
General procedure
1. Restate problem in terms of equations and
inequalities
2. Rewrite in matrix and vector notation
3. Call MATLAB function linprog to solve
Maximization Problem
• A farmer has 10 acres to plant in wheat and rye. He has
to plant at least 7 acres. However, he has only $1200 to
spend and each acre of wheat costs $200 to plant and
each acre of rye costs $100 to plant. Moreover, the farmer
has to get the planting done in 12 hours and it takes an
hour to plant an acre of wheat and 2 hours to plant an
acre of rye. If the profit is $500 per acre of wheat and
$300 per acre of rye how many acres of each should be
planted to maximize profits?
Maximization Problem
• Important
linprog() tries to minimize the objective function. If
you want to maximize the objective function.Then you have
to multiply it with -1 in order to make it minimize.
Solution
Decision variables
x1 is number of acres of wheat to plant
x2 is number of acres of rye to plant
Constraints
• "has 10 acres to plant in wheat and rye"
– In math this is x1 + x2 ≤ 10
• " has to plant at least 7 acres"
– In math this is x +x ≥ 7
1 2
Solution
Constraints
• "he has only $1200 to spend and each acre of wheat costs $200 to
plant and each acre of rye costs $100 to plant"
– In math this is 200x1 +100x2 ≤ 1200
• "the farmer has to get the planting done in 12 hours and it takes an
hour to plant an acre of wheat and 2 hours to plant an acre of rye "
– In math this is
x1 + 2x2 ≤ 12
Solution
Objective function
• "... the profit is $500 per acre of wheat and $300 per acre
of rye"
– In math this is
z=500x1 + 300x2
Solution
Change x1 + x2 ≥7 to -x1 - x2 ≤ -7 in order to make it
readable for MATLAB. x x 10
1 2
Constraints: x1 x2 7
200 x1 100 x2 1200
x1 2 x2 12
x1 0
x2 0
1 1 10
1 1 7
x 500 0
A , b , x
1
, f and lb
200 100 1200 x2 300 0
1 2 12
Solution
• function m=maximiza()
• f=[-500,-300]; %minimized objective
• A=[1 1;-1 -1;200 100;1 2]; %constraints
• b=[10;-7;1200;12]; %constriants
• Aeq=[]; %if there exist equality in problem
• beq=[]; %if there exist equality
• lb=[0 0]; %defined that (x1,x2)>=0
• ub=[]; % no restriction on the upper bound
• [x,z]=linprog(f,A,b,Aeq,beq,lb,ub) %linear
programming command
• ze=z*(-1) %multiply output by -1 in order to
maximize the result
• end
maxization.m
Solution
In Command Window
>> maxization
Optimization terminated.
x=
4.0000
4.0000
z=
-3.2000e+03
ze =
3.2000e+03
Solution
Script file
maxization.m
• x=
• 1.5000
• 5.2500
• z=
• -96.7500
• ze =
click to see script file linearproblem.m
• 96.7500
Any Question
The End