|
Hi, I'm stuck in an Constrained Optimization problem.. I'm
using the fmincon function with matrices... and it's
appearing this error:
??? Error using ==> optimfcnchk at 287
NONLCON must be a function.
Error in ==> fmincon at 302
[confcn, msg] =
optimfcnchk(NONLCON,'fmincon',length(varargin),funValCheck,gradconstflag,false,1);
Error in ==> Algorithm_1 at 92
[x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);
This is my code:
global CA CB Z LAMBD miu
global A F P x
% Enter value of variables
CA=input('Enter the value of C1:');
CB=input('Enter the value of C2:');
Z=input('Enter the value of Z:');
n=input('Enter the number of states,n:');
LAMBD=input('Enter the value of Lambda:');
x0=LAMBD;
lb=0.1;
ub=0.9;
CC=0.0001;
disp('Enter "1", if you want to work with a Tridiagonal
Matrix:');
disp('Enter "2", if you want to work with a Hessenberg
Matrix:');
ToH=input('\n\n');
%Creating a matrix A, and F and P vectors
syms miu
if ToH==1 % It is chosen a Tridiagonal matrix
A=sym(zeros(n,n));
A(1,1)=1-LAMBD;
A(2,1)=LAMBD;
A(n,n)=1-miu;
A(n-1,n)=miu;
for i=2:n-1
for j=1:n
if i==j
A(j,i)=1-LAMBD-miu;
elseif j==i-1
A(j,i)=miu;
elseif j==i+1
A(j,i)=LAMBD;
end
end
end
elseif ToH==2 %It is chosen a Hessenberg matrix
A=sym(zeros(n,n));
for i=1:n
for j=1:n
if (i==j && j~=n && j~=1)
A(j,i)=1-LAMBD-(j-1)*miu;
elseif (i==n && j==n)
A(j,i)=1-(n-1)*miu;
elseif (i==1 && j==1)
A(j,i)=1-LAMBD;
elseif j==i+1
A(j,i)=LAMBD;
elseif j<i
A(j,i)=miu;
end
end
end
else %If "1" or "2" it's not the option chosen
disp('***********NOT VALID OPTION***********');
return
end
AA=vpa(A);
P=zeros(n,1);
F=zeros(1,n);
for i=1:n
F(i)=i-1;
if i==1
P(i,1)=1;
else P(i,1)=0;
end
end
A;
P;
F;
a=1; % Storage Variable
t1=clock; %Initial vector for calculating the time
optimizacion
err=CC+1; % Assignment of error to start the optimization
while (err>CC) % Minimizing
[x,feval]=fmincon(@funn,x0,[],[],[],[],[],[],lb,ub,@nonlin1);
clc
vxg(a,1)=x; % storage the xmin
vxg(a,2)=feval; % storage the g(x)min
x=miu;
miu=vxg(a,1);
A1=eval(A); % It's evaluated matrix A with the value of x
obtained
P=A1*P; % Matrix A is multiplied by P vector
miu=sym('miu');
if a>=2 % if it's optimizated two or more times
err=abs(vxg(a,1)-vxg(a-1,1)); % Error calculation
if err<0.0000000001 %If the error is very small
err=1; %it's assigned as 1
end
else err=CC+1; % it's optimizated only one time
end
a=a+1;
end
t2=clock; % Vector 2 to calculate the optimization
tp=t2-t1; % Calculating time of optimization
time=tp(1,4)*3600+tp(1,5)*60+tp(1,6);
Funn.m
%Objective Function
function g=funn(x,CA,CB,F,P)
global M miu
x=miu;
M=((AA).^Z);
g=(CA*x+CB*(P*(F*M)));
nonlin1.m
% Nonlinear Constraint
function [c,ceq]=nonlin1(x,LAMBD)
x=miu;
c=(LAMBD/x)-1;
ceq=[];
I appreciated if somebody could help me..I'm a newer user in
Matlab...
Thanks..
Adriana
|