# For loops in non linear equaities - fmincon

2 views (last 30 days)
Tobias Dehn Nielsen on 3 Dec 2021
Commented: Walter Roberson on 3 Dec 2021
Hi
I wanna use a for loop in the non linear equity for the fmincon. I wanna minimize variable x(1) and yet even when a solution is posible, the exitflag is sometimes -1. Why cant i use a for loop in a non linear equity for the fmincon?
Here is a simplified version of my code:
function [c,ceq] = nonlconTest(x)
n = x(1);
P = x(2)
T = zeros(50,1) + 200
T(1) = 20
for i = 1:n
E(i) = -86.095*T(i) + 200471;
ang(i) = P *E(i)
end
c = [];
ceq = [sum(ang) - 10];
end

Walter Roberson on 3 Dec 2021
function [c,ceq] = nonlconTest(x)
n = x(1);
That tells us that n is one of the variables being allowed to vary.
for i = 1:n
That is a calculation that is discrete in n and so is discrete in the first variable x(1) . However, fmincon() requires the functions to be continuous in the inputs.
Exception: if you had set upper bound and lower bound to be identical for the first variable, then fmincon would be okay with that.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 3 Dec 2021
syms n
solve( (200471-17219)*n + 15497.1 == 10)
ans = vpa(ans)
ans = You need a negative x(1) for that to hold. But a negative x(1) would cause the code to fail with ang never have been assigned to.
So, what you are asking for is not possible.