For loops in non linear equaities - fmincon

4 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Walter Roberson
Walter Roberson il 3 Dic 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 Commenti
Walter Roberson
Walter Roberson il 3 Dic 2021
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
So T(1) is 20 and the rest of T are 200.
You are doing n iterations. For the first one, T(1) is 20, and you caculate
E(1) = -86.095*20 + 200471
for the rest, E(2:n) you calculate
-86.095*200 + 200471
You calculate ang as P*E(i) . But P is constant relative to that, so when you calculate sum(ang) you have sum(P*E(1) + P*E(2) + ... P*E(end)) which is P * sum(E(1) + E(2) + ... E(end))
So what is the sum? It is going to be
-86.095*20
+ 200471
-86.095*200 * (n-1)
+ 200471
which will be
-86.095*(200*n-200+20) + 200471 * n
which is
-86.095*200*n + 200471 * n + 180*86.095
which is
(200471-17219)*n + 15497.1
That is then being multiplied by P, and the result must equal 10 because this is an equality constraint.
What value of n satisfies that?
Walter Roberson
Walter Roberson il 3 Dic 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.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by