using fmincon for min problem with nonlinear constraints
Show older comments
Hello,
I tried to solve a min problem with nonlinear constraints with fmincon, but I always get an error massage.
Here are the functions I use:
function[F]= KalibrierungCIR(x)
h=sqrt(x(1)^2+2*x(3)^2);
A=(((2*h*exp(x(1)+h)*Maturity/2))./(2*h+(x(1)+h)*(exp(Maturity*h)-1))).^(2*x(1)*x(2)/x(3)^2);
B=(2*(exp(Maturity*h)-1))./(2*h+(x(1)+h)*(exp(Maturity*h)-1));
PCIR=A.*exp(-B.*x(4));
lambda_Dach=-(log(PCIR(2:length(PCIR)))-log(PCIR(1:length(PCIR)-1)))./ (Maturity(2:length(Maturity))-Maturity(1:length(Maturity)-1));
F=sum(lambda_Dach-lambda)/length(lambda);
Hier i get a scalar. My constraints are:
function [c] = mycon(x)
c = [x(3)^2-2*x(2)*x(1)];
ceq=[];
Then I type:
lb=[0.00000001 0.00000001 0.0000001 0.0001]; x0=[0.1 0.1 0.1 0.1]; [x,fval]=fmincon(@KalibrierungCIR,x0,[],[],[],[],lb,[],@myfun)
and get the error message:
Error using feval Undefined function 'myfun' for input arguments of type 'double'.
Error in fmincon (line 681) [ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue.
I have realy no idea where the problem is! I would be very greatful if someone could help me.
Answers (1)
Matt J
on 28 Sep 2013
0 votes
You've named your constraint function "mycon", but where you invoke fmincon, you instead call it "myfun".
6 Comments
Anastasia
on 28 Sep 2013
Your objective function is generating NaN values. Use
>>dbstop if naninf
to trap the occurrence of that and investigate why it is doing so.
It might be because your bound constraints are being violated at some iterations. You could try using the 'sqp' algorithm or the 'interior-point' algorithm with the "AlwaysHonorConstraints' option turned on.
Anastasia
on 28 Sep 2013
Matt J
on 28 Sep 2013
Yes. Or you could try the sqp algorithm.
Anastasia
on 29 Sep 2013
Matt J
on 29 Sep 2013
You might have a version of MATLAB that pre-dates optimoptions. If so, you will have to use optimset.
Categories
Find more on Solver-Based Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!