필터 지우기
필터 지우기

For loops in non linear equaities - fmincon

조회 수: 2 (최근 30일)
Tobias Dehn Nielsen
Tobias Dehn Nielsen 2021년 12월 3일
댓글: Walter Roberson 2021년 12월 3일
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

답변 (1개)

Walter Roberson
Walter Roberson 2021년 12월 3일
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
Walter Roberson
Walter Roberson 2021년 12월 3일
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 2021년 12월 3일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by