Lagrange Multiplier - Using - Fmincon

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

The Lagrange multiplier is a technique used in optimization problems

where you want to optimize a function subject to equality constraints. Here's a


simple example in MATLAB using the fmincon function:

b and beq are vectors, A and Aeq are matrices, c(x) and ceq(x) are functions that return vectors, and f(x)
is a function that returns a scalar. f(x), c(x), and ceq(x) can be nonlinear functions.
x, lb, and ub can be passed as vectors or matrices; see Matrix Arguments.
example
x = fmincon(fun,x0,A,b) starts at x0 and attempts to find a minimizer x of the function described
in fun subject to the linear inequalities A*x ≤ b. x0 can be a scalar, vector, or matrix.
Suppose you want to maximize the function f(x, y) = x^2 + y^2 subject
to the constraint g(x, y) = x + y - 1 = 0.
Here's how you can implement it using Lagrange multipliers:
% Objective function

fun = @(x) x(1)^2 + x(2)^2;

% Initial guess
x0 = [0.5, 0.25];

% Solve the optimization problem using fmincon with Lagrange multiplier


options = optimoptions('fmincon', 'Display', 'iter');
[x_opt, fval_opt] = fmincon(fun, x0, [], [], [], [], [], [], @nonlcon, options);
% Display the results
disp('Optimal variables:');
disp(x_opt);
disp('Optimal function value:');
disp(fval_opt);
function [C Ceq]=nonlcon(x)
% Constraint function file
C = [];
Ceq = [x(1) + x(2) - 1];

>> Lagrange_multiplier_using_fmincon

First-order Norm of

Iter F-count f(x) Feasibility optimality step

0 3 3.125000e-01 2.500e-01 2.500e-01

1 6 5.312500e-01 0.000e+00 3.750e-01 3.953e-01


2 9 5.003858e-01 0.000e+00 8.333e-02 1.964e-01

3 12 5.000000e-01 0.000e+00 3.725e-10 1.964e-02

Local minimum found that satisfies the


constraints.

Optimization completed because the objective function is non-


decreasing in feasible directions, to within the value of the
optimality tolerance, and constraints are satisfied to within
the value of the constraint tolerance.

<stopping criteria details>


Optimal variables:
0.5000 0.5000
Optimal function value:
0.5000

In this example, fun is the objective function, and nonlcon is the equality
constraint. The fmincon function is then used to find the optimal solution subject
to the given equality constraint.

This is a basic example, and you can adapt it to more complex


optimization problems with additional constraints as needed. The
fmincon function allows you to handle both equality and inequality
constraints in a similar manner.

You might also like