how to create constraints in fmincon

13 次查看(过去 30 天)
Hi all,
I have a dataset with two variables x and y as follows:
10 0.006 11 0.017 12 0.026 ...
50 0.003 51 0.002 52 0.001
I am trying to fit it with a nonlinear function with 4 parameters a, b, c, and d, of which c and d should be 10<c<52 and 10<d<52. The estimated y should be greater than 0. How can I create such constraints in fmincon? I was able to get the estimate using fminsearch. But many time, c and d and y do not meet the criterion.
lb and ub options in fminsearch are for y, not for parameters (right?) I am new with Matlab.
Thank you for warm support!
Rdu
  1 个评论
Yu Jiang
Yu Jiang 2014-8-4
  • In the function fmincon (see documentation), the input arguments lb and ub are in fact the lower and upper bound for the decision variables a, b, c, d, but not for the output y. If you need to keep y as a non-negative number, you may need to impose that constraint by choosing appropriate coefficient matrices Aeq and Beq.
  • It would be helpful if you can provide more details regarding the nonlinear function you are using and the objective function that you are minimizing by using fmincon.
  • I also wonder if you have tried to use the Curve Fitting Toolbox (see documentation) , which seems to be more suitable to be used here than fmincon.
-Yu

请先登录,再进行评论。

采纳的回答

Yu Jiang
Yu Jiang 2014-8-5
This should be an optimization problem with nonlinear constraints, and the problem should be able to be solved using fmincon (See Documentation).
Before using fmincon, two functions need to be defined. One is the objective function, one is the nonlinear constraint function.
Let us denote the nonlinear function as foo(x,a,b,c,d), which is the mixture Beta distribution as you mentioned previously. Then, the objective function can be defined as
% Objective function
function err = myObj(p,x,y)
a = p(1);
b = p(2);
c = p(3);
d = p(4);
err = 0;
for i = 1:length(x)
err = (foo(x(i) , a, b, c, d) - y(i))^2;
end
end
% End the of the objective function
Save the above two functions as two separate MATLAB files of which the filenames are the same as the function names.
Now, the nonlinear constraint also needs to be defined. Since it is required that f(x,a,b,c,d)>=0 for all x, a way to defined the nonlinear constraint is
% Constraint function
function [nl,nl2] = nlcon(p,x)
a = p(1);
b = p(2);
c = p(3);
d = p(4);
nl = [];
for i = 1:length(x)
nl = [nl; -foo(x(i), a,b,c,d)];
end
nl2= [];
end
% End of the constraint function
Notice that I put a negative sign before foo. This is because fmincon requires the nonlinear constraint function to be smaller or equal to zero.
Now, in MATLAB command line or a script file, you can try the following steps
Step 1) Give values to x and y, such as
>> x =[10:52]; y = [0.006, 0.017, 0.026, ,..., 0.003, 0.002, 0.001]
Step 2) Specify the upper and lower bounds
>> lb = [10; 10; -inf; -inf]; % Assuming 10 is the lower bound for |a| and |b|
>> ub = [52; 52; inf; inf]; % Assuming 52 is the upper bound for |a| and |b|
Step 3) Specify the initial condition
>> p0 = [5;5;5;5]; % Give some initial guess for a,b,c,d
Step 4) Solve the optimization problem using fmincon
>> xx = fmincon(@(p)myObj(p,x,y),p0,[],[],[],[],lb,ub,@(p)nlcon(p,x))
Note that in the above code I use anonymous functions (See Documentation) to pass x and y. This is because fmincon requires the objective function and the constraint function to only contain the decision variable p as their arguments, while the functions I defined have (p,x,y) or (p,x) as the arguments. To ensure the functions satisfy the requirements of fmincon, I defined to anonymous functions which only depend on p.
-Yu
  9 个评论
Yu Jiang
Yu Jiang 2014-8-8
编辑:Yu Jiang 2014-8-8
Hi Rdu
I believe fmincon and fminsearch use different algorithms to solve optimization problems. As you may have noticed, fmincon and fminsearch are used for bounded and unbounded optimization problems, respectively.
By testing your code on my machine, I noticed that fminsearch took 0.12 second and stopped because the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-08 and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-06
However, fmincon took 0.81sec and stopped due to the objective function is non-decreasing in feasible directions, to within the selected value of the function tolerance, and constraints are satisfied to within the default value of the constraint tolerance.
I think this is a proof to show the two functions have quite different search algorithms. In fact, if you compare est1 ans est2, you may find they are not too different. I guess by setting appropriate optimset, you may bring them closer.
I understand that, in your figure, the differences between the curves can be easily observed. I think that is due to the high non-linearity of the function, which may greatly amplify the slight difference between est1 and est2.
-Yu
Rdu
Rdu 2014-8-8
Thank you for the explanation, Yu! I appreciate your time invested in my question in the past week. Good wishes to you! Let's close this question.
Rdu

请先登录,再进行评论。

更多回答(1 个)

Rdu
Rdu 2014-8-5
Thank you, Yu! I think my question is different from those in documentation and most at this website. My dataset has two variables x=[10:52}, and y=[0.006, 0.017, 0.026,..., 0.003, 0.002, 0.001]. I am going to estimate a mixture Beta distribution. function is f(x)={g(a+b)/[g(a)*g(b)]}*[(d-c)^(-a+b-1)]*[(x-c)^(a-1)]*[(d-x)^(b-1)]. a and b should be greater than 10 but less than 52. And the estimated f(x) should be greater than 0. How to program it using fmincon?
Appreciation.
Rdu
  1 个评论
Rajalekshmi kishhore
can we have a separate function for linear constraint as was done for nonlinear constrint.if not how to mention sum linear constraint

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by