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

26 ビュー (過去 30 日間)
BrokenDreams
BrokenDreams 2019 年 7 月 15 日
コメント済み: Steven Lord 2019 年 7 月 15 日
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)=....

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 15 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 15 日
C=zeros(1,50);
for i=1:50
C(i)=....%
end
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 7 月 15 日
Create an array instead of assigning to individual elements.
Steven Lord
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 ExchangeHistorical Contests についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by