0% found this document useful (0 votes)
11 views4 pages

TP Ota

TP OPTIMIZATION

Uploaded by

yacinea580
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

TP Ota

TP OPTIMIZATION

Uploaded by

yacinea580
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

TP2 : Solving a Quadratic Programming

A typical quadratic programming problem can be formulated as:

Minimize ½ xT Q x + cT x
Subject to : Ax £ b

Where :
• Q is a symmetric matrix (for the quadratic part),
• c is a vector (for the linear part),
• A is a matrix defining the linear inequality or equality constraints,
• b is a vector of bounds for the constraints, and
• x is the vector of decision variables you're optimizing.

Solving a Quadratic Program in MATLAB:

MATLAB provides a built-in function called quadprog to solve quadratic


programmingproblems. Below is a basic example of how to use it.

Example Problem:

Consider the following quadratic programming problem:

Minimize ½ xT Q x + cT x
Subject to : x1+x2 £ 1
-x1+x2 £ 1

Matlab Code :

% Define the Q matrix (quadratic term) and c vector (linear


term)
Q = [2 0; 0 2]; % Symmetric matrix (2x2)
c = [-2; -5]; % Linear coefficients (2x1)

% Define the inequality constraints (Ax <= b)


A = [1 1; -1 1]; % 2x2 matrix for linear inequalities
b = [1; 1]; % 2x1 vector for the bounds

% Solve the quadratic programming problem


options = optimoptions('quadprog', 'Display', 'off'); % suppress
output
[x, fval] = quadprog(Q, c, A, b, [], [], [], [], [], options);

% Display the solution


disp('Optimal solution:');
disp(x);

disp('Optimal value of the objective function:');


disp(fval);
Instructions:

1. Define the objective function:


o The quadratic part is represented by the matrix Q.
o The linear part is represented by the vector c.
2. Set up the constraints:
o The constraints are represented by the matrix A (for the inequality part) and
the vector b.
o The quadprog function requires the constraints to be in the form Ax≤b.
3. Solve the quadratic programming problem:
o Use the quadprog function to compute the optimal solution for the decision
variables x1 and x2.
o The function will return the optimal values of x and the value of the objective
function at the optimal solution.
4. Display the results:
o After solving the optimization problem, display the optimal values of x and
the optimal value of the objective function.

Expected Learning Outcomes:

By completing this exercise, students should be able to:

1. Understand Quadratic Programming:


o Comprehend the formulation of a quadratic programming problem, including
the quadratic objective function and linear constraints.
o Recognize the difference between linear programming and quadratic
programming.
2. Use MATLAB’s quadprog Function:
o Understand the syntax and usage of the quadprog function in MATLAB for
solving quadratic programming problems.
o Be able to input the objective function and constraints into quadprog properly.
3. Interpret Results:
o Interpret the output of the optimization problem (the optimal values of the
decision variables and the objective function).
o Understand the implications of the results in the context of the problem.

Additional Practice:

1. Change the Constraints:


o Modify the inequality constraints (e.g., change the matrix A and vector b) and
solve the quadratic program again to see how the optimal solution changes.
2. Add Equality Constraints:
o If you're comfortable, you can add equality constraints to the problem by
defining Aeq (the equality constraint matrix) and beq (the equality constraint
vector). Then, use quadprog with these added constraints.
3. Use Bound Constraints:
o Experiment with adding lower and upper bounds for the decision variables. In
MATLAB, this can be done using the lb and ub parameters in quadprog
Explanation:

1. Objective function:
o The quadratic part is represented by the matrix Q. For example, if you want to
minimize ½ xTQx+cTx, you use Q = [2 0; 0 2] and c = [-2; -5].
2. Constraints:
o The constraints Ax≤b are defined using matrices A and vector b. Here, A
= [1 1; -1 1] represents two inequality constraints, and b = [1; 1] defines the
bounds on these inequalities.
3. Function quadprog:
o quadprog(Q, c, A, b, [], [], [], [], options) solves the quadratic programming problem.
The empty vectors [] indicate that we have no equality constraints in this case.
o The function returns x, the vector of optimal decision variables, and fval, the
value of the objective function at the optimal solution.
4. Display the solution :
o The disp commands display the optimal values of x and the objective function
at the optimal solution.

Running the Code:

When you run this code, MATLAB will solve the problem and output the optimal values for x1
and x2, along with the value of the objective function at the solution.

Output Example:

Optimal solution:
1.0000
0.0000

Optimal value of the objective function:


-7.0000

Key Notes:

1. Quadratic matrix (Q): It must be symmetric. If you provide a non-symmetric matrix,


MATLAB will throw an error.
2. Constraints: You can add equality constraints using Ax = b by specifying them in the
appropriate arguments (the last two arguments of quadprog for equality constraints).
3. Bounds: You can also set bounds on individual variables using the last two input
arguments, lb (lower bound) and ub (upper bound), if necessary.
More Advanced Usage:

• If you have equality constraints, use the Aeq and beq parameters in quadprog:

Aeq = [1, 2]; % Example equality constraint: x1 + 2*x2 = 5


beq = 5;
[x, fval] = quadprog(Q, c, A, b, Aeq, beq, [], []);

• You can also handle non-linear constraints using optimization toolboxes like fmincon,
but quadprog is specifically for quadratic programs with linear constraints.

You might also like