MATLAB Answers

0

How do I increment the value of a variable after each statement by 1?

Asked by BrokenDreams on 15 Jul 2019
Latest activity Commented on by Steven Lord
on 15 Jul 2019
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 Comments

Sign in to comment.

Tags

Products


Release

R2019a

1 Answer

Answer by KALYAN ACHARJYA on 15 Jul 2019
Edited by KALYAN ACHARJYA on 15 Jul 2019

C=zeros(1,50);
for i=1:50
C(i)=....%
end

  4 Comments

Show 1 older comment
I still dont get it. My function looks like this. How do i stop explicitly typing ceq(1) ceq(2) etc. Kindly provide the code. ThanksCapture.PNG
Create an array instead of assigning to individual elements.
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(:);

Sign in to comment.