I am trying to solve a quadratic optimization problem using the MATLAB's function quadprog. Actually I am trying to solve many (not one) quadratic optimization problems in series utilizing a for loop, where each qp depends on the results of the previous qp's results. The thing is that sometimes depending on the initial point, the warning: "Your Hessian is not symmetric. Resetting H=(H+H')/2." appears.
Question 1 & 2: Does that mean that the solver did not produce a (correct) solution? Or the case is that when the new hessian is used the resulting decision vector can be considered correct, the one that minimizes the objective function?
My (ignorant) idea is that in such case I should stop the simulation and try different initial point. BUT I expected that the exit flag would change from 1 to some other value, and with a simple if exitflag ~= 1 return end loop the process would stop. However this is not the case. The exit flag does not change yet the decision variable vector does not seem to be a correct answer.
I am trying to use fmincon in a while loop such that until the while condition is not satisfied, fmincon must be executed. Each time fmincon can't satisfy a specific condition (e.g., x(N)-7.6==Tol), the number N of nonlinear constraints should be updated (increased). How is this possible with fmincon?
Suppose I initially have 18 nonlinear equality equations (ceq(1) ... ceq(18)). When the while condition can't be satisfied, the number of nonlinear equality equations should be increased to 23 (ceq(1) ... ceq(23)) on the next iteration.
tnx for your innovative idea....let's give you more details about what I wanna do. I have some set of nonlinear algebraic equations so that I need to work with NLP (nonlinear programming) solvers. besides my cost function is minimum time problem. actually my nonlinear constrained equations are some dynamic governing equations which are discreticized in time coordinate. N is the number of discretization. based on Lagrange optimization technique, for finding optimal solution, gradient of scalar function(Lagrangian) should be added to system. as I mentioned in my question I need to test my problem with an initial N, then if the Xoutput of fmincon could'nt meet the constrained, it needs to increase the number of descritization. it continues, until the output optimal answer of fmincon get closed enough to my desired answer.
Peeling back the onion, it appears to me your question doesn't address your real problem, and you're going about solving your problem numerically in somewhat the wrong way. How you solve a problem numerically is related but rather different to how you would solve a problem analytically, writing out equations by hand. See my further comment at the end, and I'd advise talking to fellow students, fellow students in your lab, and/or the prof.
Several comments:
Your objective funciton #(x) norm(myfoctest(x)) will always return 0 because myfoctest returns the empty array [] as its first argument, and in Matlab, norm([]) is defined as 0.
Instead of minimize 0 subject to f(x)==0, it appears you intend to solve problem minimize norm(f(x)) subject to f(x)==0? I don't understand the purpose of constraint f(x)==0 in this context. Why not minimize norm(f(x))?
In your function, myfoctest Why is Ceq(2*N)=x(5*N+12)-x(5*N+11)-x(4*N+2) in the for loop? You're assigning the value of x(32)- x(31) - x(18) to Ceq(8) four separate times (i.e. for j=1:4). Is this what you intend? This error suggests to me there might be other errors in the way you've written myfoctest.
A number of these constraints are linear constraints. Entering them as non-linear constraints will make fmincon's job harder.
I don't know the original problem, but this feels to me that you're going about numerically solving it in a haphazard, screwy way. I've spotted several errors just glancing at the code, and I'd be concerned there are more if I actually understood the problem.
You have 5*N+13 variables and 5*N+13 non-linear equality constraints. Your feasible set may be a single point! Digression: many optimization algorithms start with a feasible point and take a step in a feasible direction. If the feasible set is a single point, there's no feasible direction... In your problem, the whole game is finding the one feasible point (if it even exists)?!
I doubt the main problem is that you need to "Update the number of nonlinear constraints in fmincon in Matlab."
It vaguely sounds like you've calculated the first order conditions of the Lagrangian, and you're entering those as constraints for the optimization problem? If so, that's very likely not what you should be doing to solve numerically.
Some suggestions...
I think you need to take a step back to the beginning: write down what you're trying to solve in a clean way, and then figure out how to solve it numerically in an efficient manner. My gut reaction is that this so far is a somewhat confused mess.
Further comment
Say you have some minimization problem (eg. an optimal control problem):
minimize f(x) subject to g(x) <= 0.
where f and g and convex, Slater's condition holds, and the first order conditions are necessary and sufficient conditions to achieve a minimum. You might solve this mathematically and get some first order conditions:
dL/dx = 0
You might think that the way to solve this problem numerically is to numerically solve the system of equations dL/dx (from the FOC). If dL/dx is a system of linear equations, this might be true, but in general, that's often an intractable way to go about solving the problem. Instead, you want to feed f and g directly to your optimization algorithms.
General points to keep in mind:
Solving a system of linear equations is efficient and fast.
Solving a convex optimization problem is efficient and fast.
In general, solving a system of non-linear equations or non-convex optimization can be horrible, horrible problems.
You have two cases. In first, some condition is satisfied, and in second, it's not. Make the while statement hold true in each case. In your loop add the flag variable that would change its value if your condition is not satisfied. For example you can put something like:
flag = (x(N)-7.6<Tol);
that would return 1 if condition is satisfied and 0 otherwise.
In your mycon function add flag as an input variable:
function [c,ceq] = mycon(all_variables_you_had_before,flag)
Then, add the logical block in mycon looking like:
if flag == 1
ceq = [___]; %//put your 18 conditions here
else
ceq = [___]; %//put your 23 conditions here
end
Finally, do not forget to add mycon(all_variables_you_had_before,flag) in the fmincon line in your main script:
x = fmincon(#myfun,x0,A,b,Aeq,beq,lb,ub,#(all_variables_you_had_before) mycon(all_variables_you_had_before,flag))
So, if the condition is satisfied, your fmincon would get the constraints as usual. But if the condition is not satisfied, the constraints would change. Hope that helps.
function [x]=runnested(x0,N)
r=ones(4,1);
N=length(r);
Tol=0.001;
for k=1:N
for i=1:N
x0=rand(5*N+13,1)
options = optimset('Largescale','off','algorithm','interior-point','Display','iter');
[x(i,:),fval,exitflag,output]=fmincon(#(x) norm(myfoctest(x)),x0,[],[],[],[],[],[],#myfoctest,options)
end
if x(N)-7.61<=Tol
break;
else
N=N+1;
end
end
function [C,Ceq]=myfoctest(x,N,r)
C=[];
r=ones(4,1);
N=length(r);
f=3.5e-6; %km/s^2
i1=10*(pi/180);
Ts=110; %sec
V0=7.79; %km/sec
a1=7.61; %km/sec
b1=0.01*a1;
a2=20*(pi/180); % rad %10 deg
b2=0.01*a2; %rad
Omeg0=10*(pi/180); %rad
Ceq=zeros(5*N+13,1);
for j=1:N-1
Ceq(j)=x(3*N+1+j)- x(3*N+j)-2*x(4*N+1+j)*Ts*f*sin(x(2*N+1+j))./(pi*sin(i1)*x(j)^2)
Ceq(N)=x(5*N+10)-x(5*N+9)-x(3*N+2) %x(5*N+10)-x(5*N+9)-x(4*N+7)
Ceq(N+j)=x(4*N+1+j)-x(4*N+j)
Ceq(2*N)=x(5*N+12)-x(5*N+11)-x(4*N+2)
Ceq(2*N+1)=x(3*N+1)*Ts*f*sin(x(2*N+1))+2*x(4*N+1)*Ts*f*cos(x(2*N+1))/(pi*V0*sin(i1))
Ceq(2*N+1+j)=x(3*N+1+j)*Ts*f*sin(x(2*N+1+j))+2*x(4*N+1+j)*Ts*f*cos(x(2*N+1+j))./(pi*x(j)*sin(i1))
Ceq(3*N+1)=1-x(5*N+9)*b1-x(5*N+10)*b1-x(5*N+11)*b2-x(5*N+12)*b2-x(5*N+8)*N*Ts/100-x(5*N+13)
Ceq(3*N+2)=-2*x(5*N+8)*x(5*N+2)
Ceq(3*N+3)=-2*x(5*N+9)*x(5*N+3)
Ceq(3*N+4)=-2*x(5*N+10)*x(5*N+4)
Ceq(3*N+5)=-2*x(5*N+11)*x(5*N+5)
Ceq(3*N+6)=-2*x(5*N+12)*x(5*N+6)
Ceq(3*N+7)=2*x(5*N+13)*cos(x(5*N+7))*sin(x(5*N+7))
Ceq(3*N+8)=V0-x(1)-Ts*f*cos(x(2*N+1))
Ceq(3*N+8+j)=x(j)-x(j+1)-Ts*f*cos(x(2*N+1+j))
Ceq(4*N+8)=Omeg0-x(N+1)+2*Ts*f*sin(x(2*N+1))/(pi*V0*sin(i1))
Ceq(4*N+8+j)=Omeg0-x(j+1)+2*Ts*f*sin(x(2*N+1+j))./(pi*x(j)*sin(i1))
Ceq(5*N+8)=-x(5*N+2)^2-N*Ts/100-N*Ts*x(3*N+1)/100
Ceq(5*N+9)=-x(5*N+3)^2-x(N)+a1+b1-b1*x(3*N+1)+7.61/100
Ceq(5*N+10)=-x(5*N+4)^2+x(N)+a1+b1-b1*x(3*N+1)-7.61/100
Ceq(5*N+11)=-x(5*N+5)^2-x(2*N)+a2+b2-b2*x(3*N+1)+0.35/100
Ceq(5*N+12)=-x(5*N+6)^2+x(2*N)+a2+b2-b2*x(3*N+1)-0.35/100
Ceq(5*N+13)=-(sin(x(5*N+7)))^2-x(5*N+1)
end
end
end
I am solving a multi objective optimisation problem. The pseudo code is as follows:
For h=1:10 % Number of points
1. Update some constraints % Update the constraint to find new points
2. Minimize F(X) % Objective function, is not updated at ittirations
Subject to the Updated constraints.
End
For each iteration , a new solution is obtained. But, for some iteration the solution is infeasible and fmincon returns exitflag of -2. So, they must be discarded from the possible solution set of the problem.
fmincon consumes a lot of time to recognise an infeasible point but for other points which have exit flag >0 it is fine. Thus, the total consumed time of the problem is significantly large due to these infeasible points.
How can I set the fmincon option to get the infeasible points faster considering the fact that I can only set the option for 1 time.
I am using the Matlab 2012 svm included in the stats package. I have a binary classification problem where I train a set of vectors and test another set of vectors as the following matlab code shows:
%set the maximum number of iterations
optSVM = statset('MaxIter', 1000000);
%train the classifier with a set of feature vectors
SVMtrainModel = svmtrain(training_vectors_matrix(:,2:end), training_vectors_matrix(:,1), 'kernel_function' , 'linear', 'options', optSVM, 'tolkkt', 0.01);
%read the test vectors
TestV = csvread(test_file);
%Test the feature vectors in the built classifier
TestAttribBin = svmclassify(SVMtrainModel, TestV(:,2:end))
It' s a quite simple code and would run normally. The training runs ok, but when I test the following error happens:
Subscript indices must either be real positive integers or logicals.
Error in svmclassify (line 140)
outclass= glevels(outclass(~unClassified),:);
So, are my feature vectors with any problem? If I run this same code in different feature vectors (training and testing vectors) the code runs ok. I already checked the feature vectors and there are no NaNs. What should be the cause of this problem?
This should be solvable keeping my generic solution to this problem in mind.
1) Run the code with dbstop if error
It will now stop at the line that you provided:
outclass= glevels(outclass(~unClassified),:);
2) Check the possible solutions.
In this case I assume that glevels and outclass are both
variables. The next thing to do would be to carefully examine
everything that could be an index.
Starting inside out:
The first index is ~unClassified, as the ~ operation did not fail, it is safe to say that this is now a logical vector.
The second and lastindex is outclass(~unClassified), this one is most likely not consisting of only numbers like 1,2,3,... or true,false values.
The test whether the values are all valid is quite simple, one of these two should hold:
To confirm that the values in x are logical: class(x) should return 'logical'
To confirm that the values in x are real positive integers: isequal(x, max(1,round(abs(x)))) should return 'true'.
This problem can be solve if you remove your NaN rows or data:
Features(~any(~isnan(Features), 2),:)=[];
maybe you have complex numbers too, then use this code:
Features3(any(isnan(Features3),2),:)=0;
Features3 =real(Features3);
first line, make all NaN values turn to zero and the second line turns all complex numbers to be real.
I have a little code, that should implement cepstrum deconvolution for minimum phase FIR filter design, but being nonmatlab guy I'm struggling with understanding it. Can someone help?
wn = [ones(1,m)
2*ones((n+odd)/2-1,m)
ones(1-rem(n,2),m)
zeros((n+od d)/2-1,m)];
y = real(ifft(exp(fft(wn.*real(ifft(log(abs(fft(x)))))))));
Mainly I don't understand the first line, ".*" symbol in second line and also probably at some point there should be conversion from real to complex domain in the second line, but I have no idea where. Any ideas?
In the first line you are constructing the matrix wn row by row.
.* operator means element-wise multiplication. * alone would mean matrix multiplication.
In fact you should pay attention to the size of x and wn which must be the same for the element-wise multiplication to have sense.
actually there isn't any conversion from real to complex in the second line. There are the functions log, fft, ifft that may return complex values depending on the input.
You can access the Matlab help by the commands help or doc (for example doc ones should produce the documentation of the ones function - this produce a matrix filled with ones of the size specified by it's arguments).
To quickly summon the help when you're inspecting some code you can use Matlab's inline help by pressing the button F1 when the cursor is at the end of a function name (just before the parenthesis).