Generate a vector of equations using loop
Show older comments
I have bunch of quadratic terms that I want to transform them into a vector of quadratic equations.
nlcon_funs=cell(size_con,1);
for i=1:size_con
nlcon_funs{i}=@(x) x'*Qc{i}*x+lc{i}'*x;
end
nlcon=@(x) nlcon_funs;
Where nlcon is a vector of equations which is expected to have the form
@(x) [x'*Qc{2}*x+lc{2}'*x;x'*Qc{2}*x+lc{2}'*x;...]
Since I have Qc and lc with pretty big size, it is impossible for me to write out nlcon directly, that's why I was trying to use a loop to create such form
@(x) [x'*Qc{2}*x+lc{2}'*x;x'*Qc{2}*x+lc{2}'*x;...]
with given Qc and lc. However, it does not work as expected. Can someone tell me where I went wrong and how to fix it thanks!
Answers (1)
Walter Roberson
on 7 Jan 2016
If you are using fmincon or similar then the nlcon cannot be a vector of function handles: it would have to be a single function that invoked the vector of handles on each argument, such as
nlcon = @(x) cellfun(@(fun) fun(x), nlcon_funs)
But then you might as well
nlcon = @(x) cellfun(@(QC,LC) x'*QC*x + LC'*x, qc, lc)
which removes the problem of building the function handles.
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!