EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab quadprog
 

Matlab quadprog

Updated March 6, 2023

Matlab quadprog

 

 

Introduction to Matlab quadprog

The following article provides an outline for Matlab quadprog. The quadprog or quadratic programming is used to minimize or maximize the input objective function subjected to various constraints. Quadratic programming is used in mathematics to find a vector ‘x’, minimising a quadratic function defined as minx{1/2 * x^T *Hx + f^T * x}.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Or

Matlab quadprog 1

The following constraints are applied to the quadratic function:

  • Ax ≤ b (inequality constraint)
  • Ax = b (equality constraint)
  • LowerBound ≤ x ≤ UpperBound (bound constraint)

In the real world, quadratic programming is used to solve problems, including power generation, portfolio management, design optimization, etc.

Syntax of quadprog Function

Given below is the syntax mentioned:

x = quadprog (H, f, A, b)

Explanation:

  • x = quadprog (H, f, A, b) is used to minimize the quadratic function 1/2 * x^T * Hx + f^T * x subjected to the constraints A*x ≤ b.

Note that the input matrix ‘H’ must be a positive definite for quadratic programming if we need our problem to give a finite value minimum.

Examples of Matlab quadprog

Given below are the examples of Matlab quadprog:

Example #1

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 2 x 2 input matrix ‘H’ for the input objective function.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 3; -1 2] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 3] [Initializing the vector ‘f’ for the input objective function]

A = [1 0; -1 -2; 0 1];
b = [2; 2; 3];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

FinalPoint, FunctionVal, ExitFlag

Input:

Matlab quadprog 2

Output:

Output 1:

“1/2 * x^T *Hx + f^T * x

Output 2:

Matlab quadprog 4

As we can see in the output, our objective function is now minimized under constraints defined by us. Also, we have obtained the Exit Flag as 1, which implies we have obtained a local minimum for our objective function.

Example #2

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 3 x 3 input matrix ‘H’ for the input objective function.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 1 0; 1 1 1; 1 0 1] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 1; 1] [Initializing the vector ‘f’ for the input objective function]

A = [1 1 2; 1 -2 1; 1 2 1];
b = [1; -1; 1];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

Input:

Matlab quadprog 5

Output:

Output 1:

we will use a 3 x 3 input matrix

Output 2:

Matlab quadprog 7

As we can see in the output, our objective function is now minimized under constraints defined by us. Also, we have obtained the Exit Flag as 1, which implies we have obtained a local minimum for our objective function.

In the above 2 examples, the input matrix ‘H’ was convex, i.e. positive definite, and so we got a local minimum. If our matrix is not convex, we will not get a local minimum.

Example #3

This example will use the quadprog function to minimize the objective function “1/2 * x^T *Hx + f^T * x”. For this example, we will use a 3 x 3 input matrix.

Below are the steps to be followed:

  • Initialize a 3 x 3 input matrix ‘H’ for the input objective function, such that the matrix is not positive definite.
  • Initialize the vector ‘f’ for the input objective function.
  • Declare the required constraints.
  • Use the quadprog function to minimize the objective function.

Code:

H = [1 2 0; 3 1 2; -1 0 1] [Initializing the matrix ‘H’ for the input objective function]

f = [-1; 1; 1] [Initializing the vector ‘f’ for the input objective function]

A = [1 1 2; 1 -2 1; 1 2 1];
b = [1; -1; 1];
[Declaring the constraints] [FinalPoint, FunctionVal, ExitFlag, Op, L] = quadprog(H, f, A, b);
[Using the quadprog function to get the required values for the objective function]

Input:

Matlab quadprog 8

Output:

Output 1:

The problem is non-convex

Output 2:

minimize the objective function

As we can see in the output, the Exit Flag is-6, which implies we have not obtained any local minimum for our objective function.

Conclusion

We use quadprog function to find the local minimum of our objective function, which is subjected to some constraints. We will get local minimum only if the matrix ‘H’ is positive definite in nature.

Recommended Articles

This is a guide to Matlab quadprog. Here we discuss the introduction and examples of Matlab quadprog for better understanding. You may also have a look at the following articles to learn more –

  1. What is Matlab?
  2. Matlab limit
  3. Plot graph Matlab
  4. Absolute Value Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW