How do I increment the value of a variable after each statement by 1?
20 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
KALYAN ACHARJYA
2019-7-15
编辑:KALYAN ACHARJYA
2019-7-15
C=zeros(1,50);
for i=1:50
C(i)=....%
end
4 个评论
Steven Lord
2019-7-15
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(:);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!