Is it possible to include a 'for loop' in a constraint file for optimization using fmincon solver?

I am solving a multiperiod optimization problem using fmincon solver.Some of the constraints are non linear.The constraints are to be repeated for all periods.Is it possible to repeat the constraints for all periods by including a for loop in the mfile for constraints?

4 Comments

I am afraid that I do not understand your question. A nonlinear constraint function is a MATLAB function. It can have any valid MATLAB commands, including if, while, for, or anything else.
But I think that is not really what you are asking. So can you please rephrase?
Alan Weiss
MATLAB mathematical toolbox documentation
Sir, I have some constraint,for example, P(i)=a*h(i)*s(i); in which i varies from 1 to 12.The objective is to be satisfied such that this constraint is satisfied for every i from 1 to 12 simultaneously.So, instead of writing the same constraint seperately for each i,can I use a for loop? Similarly, I have other constraints of the same type.So, is it possible to include all these constraints inside a for loop?The constraint file is to be written as function[c ceq]=....So, if I use a for loop,can I write the script as fori=1:12 ceq=[P(i)-a*h(i)*s(i)];
for i = 1:12; ceq(i) = P(i)-a*h(i)*s(i); end
However it would make more sense to vectorize:
ceq = P - a .* h .* s;

Sign in to comment.

Answers (0)

Asked:

on 5 Feb 2018

Commented:

on 6 Feb 2018

Community Treasure Hunt

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

Start Hunting!