# For loops in non linear equaities - fmincon

1 visualizzazione (ultimi 30 giorni)
Tobias Dehn Nielsen il 3 Dic 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
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposte (1)

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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
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.

### Categorie

Scopri di più su Solver Outputs and Iterative Display in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!