How do I increment the value of a variable after each statement by 1?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am trying to solve equations using fmincon. I have around 50 non linear constraints.
At the moment i am defining them like
c(1)=..
c(2)=...
.
.
.
c(50)=..
how do i make it simpler like c++ where i can do k=0. c(++k)=....
0 Commenti
Risposte (1)
KALYAN ACHARJYA
il 15 Lug 2019
Modificato: KALYAN ACHARJYA
il 15 Lug 2019
C=zeros(1,50);
for i=1:50
C(i)=....%
end
4 Commenti
Steven Lord
il 15 Lug 2019
If all your constraints are linear combinations of elements in x, consider writing them as a matrix.
constraints = [-1 1 0 0 0 1 zeros(1, 15); ... % -x(1) + x(2) + x(6);
0 -1 1 zeros(1, 18); ... % -x(2) + x(3)
zeros(1, 13) -1 1 zeros(1, 6)]; % -x(14) + x(15)
ceq = constraints*x(:);
Alternately instead of using zeros like I did, you could explicitly type out the 0's. This is more verbose (potentially much more verbose) but would make the structure of your constraint matrices explicit and visible. In the example below, each element of the constraints matrix is a single character wide. I could have typed -1 explicitly, but then to get the coefficients to line up I would need to type " 0" and " 1" in later columns.
N = -1; % N = Negative one
P = 1; % P = Positive one
% 1 1 1 1 1 1 1 1 1 1 2 2
% 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
constraints = [N P 0 0 0 P 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 N P 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 N P 0 0 0 0 0 0];
ceq = constraints*x(:);
Vedere anche
Categorie
Scopri di più su Historical Contests 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!