MATLAB: nlp477
← ChE 477: Process Optimization
close all
clear all
clc
% Nonlinear Programming (NLP) Examples
% type "help fmincon" for more information
% Ugur Akman 2004
% Example
% OBJ: min f(x1,x2) = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1)
% h1: x(1)^2 + x(2) - 1
% g2: -x(1)*x(2) - 10
% Step 1: Write an M-file objfun.m
function f = objfun(x)
f = exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
%Step 2: Write an M-file confuneq.m for the nonlinear constraints
function [c, ceq] = confuneq(x)
% Nonlinear inequality constraints
c = -x(1)*x(2) - 10;
% Nonlinear equality constraints
ceq = x(1)^2 + x(2) - 1;
% NOTE:
% If you have more than one inequality/equality then write them as
% c = [... ; ... ; ...] & ceq = [... ; ... ; ...]
% If you do not have any inequality/equality then givee them as empty sets
% c = [] & ceq = []
% NOTE:
% The direction and form of inequalities must be "g(x) < 0"
%Step 3: Invoke constrained optimization routine
x0 = [-1,1]; % Make a starting guess at the solution
options = optimset('LargeScale','off');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confuneq,options)
[c,ceq] = confuneq(x) % Check the constraint values at x
% NOTE:
% If you have bounds (side constraints) on variables then you go as
% follows:
% lb = [0,0]; % Set lower bounds
% ub = [ ]; % If no upper bounds
% fmincon(@objfun,x0,[],[],[],[],lb,ub,@confun,options)
%After 21 function evaluations, the solution produced is
x =
-0.7529 0.4332
fval =
1.5093
c =
-9.6739
ceq =
4.0684e-010
% NOTE: fmincon
% Find a minimum of a constrained nonlinear multivariable function subject
% to constraints where x, b, beq, lb, and ub 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.
% Syntax
% x = fmincon(fun,x0,A,b)
% x = fmincon(fun,x0,A,b,Aeq,beq)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
% [x,fval] = fmincon(...)
% [x,fval,exitflag] = fmincon(...)
% [x,fval,exitflag,output] = fmincon(...)
% [x,fval,exitflag,output,lambda] = fmincon(...)
% [x,fval,exitflag,output,lambda,grad] = fmincon(...)
% [x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...)
% Descriptionfmincon attempts to find a constrained minimum of a scalar
% function of several variables starting at an initial estimate. This is
% generally referred to as constrained nonlinear optimization or nonlinear
% programming.
% x = fmincon(fun,x0,A,b) starts at x0 and attempts to find a
% minimum x to the function described in fun subject to the linear
% inequalities A*x <= b. x0 can be a scalar, vector, or matrix.
% x = fmincon(fun,x0,A,b,Aeq,beq) minimizes fun subject to the linear
% equalities Aeq*x = beq as well as A*x <= b. Set A=[] and b=[] if no
% inequalities exist.
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub) defines a set of lower and upper
% bounds on the design variables in x, so that the solution is always in the
% range lb <= x <= ub. Set Aeq=[] and beq=[] if no equalities exist.
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization
% to the nonlinear inequalities c(x) or equalities ceq(x) defined in nonlcon.
% fmincon optimizes such that c(x) <= 0 and ceq(x) = 0. Set lb=[] and/or
% ub=[] if no bounds exist.
% x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with
% the optimization options specified in the structure options. Use optimset
% to set these options. Set nonlcon = [] if there are no nonlinear
% inequality or equality constraints.
% [x,fval] = fmincon(...) returns the value of the objective function fun
% at the solution x.
% [x,fval,exitflag] = fmincon(...) returns a value exitflag that
% describes the exit condition of fmincon.
% [x,fval,exitflag,output] = fmincon(...) returns a structure output
% with information about the optimization.
% [x,fval,exitflag,output,lambda] = fmincon(...) returns a structure
% lambda whose fields contain the Lagrange multipliers at the solution x.
% [x,fval,exitflag,output,lambda,grad] = fmincon(...) returns the value
% of the gradient of fun at the solution x.
% [x,fval,exitflag,output,lambda,grad,hessian] = fmincon(...) returns the
% value of the Hessian at the solution x.
Published with MATLAB® 7.0