Fmincon Intro

Only available on StudyMode
  • Download(s) : 346
  • Published : November 14, 2011
Open Document
Text Preview
FMincon Introduction
Advanced Econometrics Professor:Florian Pelgrin Assistant:Edward Shyu

1

Outline
Fminunc Fmincon Single Variable Multiple Variables Maximize a function Equality constraints Lower and upper bounds nonlinear constraints Output options Gradient Examples: constraints

2

Purpose: Minimize a function
Given a function f(x), find value x that results in the smallest f(x). Example: f(x) = (x-2)^2

Define function to minimize
[x fval] = fminunc(@myfunUnc,x0) Define objective function (for example: myfunUnc) Save in a file of the same name: myfunUnc.m

Guess a value for x
[x fval] = fminunc(@myfunUnc,x0,options) Try x0 = 0

Call fminunc: get x, fval
[x fval] = fminunc(@myfunUnc,x0,options) Results: x = 2.0000 fval = 7.5417e-018 ≈ 0

Inequality Constraints
Find the value of x that minimizes a function of x f(x) = cos(x) Given some inequality constraints on x 0 ≤ x ≤ 2π Minimum expected: x = π = 3.14 f(x) = 0

Fmincon inputs: function f(x)
[x fVal] = fmincon(@myfun,x0,A,b) @myfun: gives the ‘handle’ of function ‘myfun’, which contains the function to minimize. Save function in m-file (myfun.m)

Fmincon inputs: constraints
[x fVal] = fmincon(@myfun,x0,A,b) Constraints described by A*x ≤ b For 0 ≤ x ≤ 2π, rewrite as: -1*x ≤ 0 1*x ≤ 2π In Matrix notation:  − 1 0

  1  × x = 2π      { {
A b

In Matlab: A = [-1; 1] b = [0 ; 2*pi]

Fmincon inputs: starting value
[x fVal] = fmincon(@myfun,x0,A,b) x0: guess a starting value Try to guess a starting value for x that is close to the correct value that minimizes f(x). Try starting value = 1 In Matlab: x0 = 1

Fmincon input/output

Output: x = 3.1416 fVal = -1

Fmincon output: check

x = 3.1416 = π fVal = -1

Example 2: Multiple x values
Minimize f(x) = -x1x2x3 given the constraint 0 ≤ x1 + 2x2 + 3x3 ≤ 100 Now there is more than one variable to find: (x1, x2 and x3)

Fmincon inputs: function f(x)
[x, fVal] = fmincon(@myfun2,x0,A,b) The x-variables are in vector x Define f(x) = -x1*x2*x3  x1  x =  x2     x3   

Fmincon inputs: constraints
[x, fVal] = fmincon(@myfun2,x0,A,b) Constraint: 0 ≤ x1 + 2x2 + 3x3 ≤ 100 Rewrite as -1x1 - 2x2 - 3x3 ≤ 0 x1 + 2x2 + 3x3 ≤ 100 In matrix notation: In Matlab: A = [-1 -2 -3; 123] b = [0; 100]

− 1 − 2 − 3  0  1  × x = 100 2 3  2 144 44  2 3 13 A b

Fmincon inputs: starting values
[x, fVal] = fmincon(@myfun2,x0,A,b) Guess starting values

10 x 0 = 10   10  
In Matlab: x0 = [10 ; 10 ; 10]

Input and output
Output: x= 33.3333 16.6666 11.1111 fVal = -6.1728e+003

Maximize a function
To maximize a function, minimize the negative of that function. max f(x) = min –f(x)

Equality Constraints
[x fVal] = fmincon(@myfun,x0,A,b,Aeq,beq) Define Aeq and beq such that Aeq * x = beq Example: Equality constraint: x1 + x2 + x3 = 10 In Matlab: Aeq = [1 1 1] Beq = 10

 x1  [1424 ]×  x2  = [10] 1 1 1 3   { Aeq  x3  beq  

Equality Constraints
[x fVal] = fmincon(@myfun,x0,A,b,Aeq,beq) If there are no inequality constraints, set A=[] b=[] Call function: [x fVal] = fmincon(@myfun,x0,[],[],Aeq,beq)

Lower bound, upper bound
[x fVal] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub) Define lower and upper bounds for x values Example: -10≤x1≤10 -20≤x2≤20 -5 ≤x3≤50 In Matlab: lb = [ -10; -20;-5] ub =[ 10; 20; 50] Call function: x =fmincon(@myfun,x0,[],[],[],[],lb,ub) Remember, if an input value is not needed, fill in with [ ]

Nonlinear constraints
[x fVal] = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon) Define mycon function in file mycon.m function [c,ceq] = mycon(x) c = ... % C(x) ≤ 0 ceq = ... % ceq(x) = 0 Call function: x = fmincon(@myfun,x0,[],[],[],[],[],[],@mycon) Matlab will minimize @myfun given the constraint that c(x) ≤ 0 and ceq(x) = 0 This gives you more flexibility in defining your constraints, in case you cannot write them linearly as A*x≤b or Aeq*x ≤ beq

Nonlinear constraints
[x fVal] =...
tracking img