How do I increment the value of a variable after each statement by 1?
26 ビュー (過去 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(:);
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!