Running for loop on an equation

1 回表示 (過去 30 日間)
aliza mustafa
aliza mustafa 2022 年 10 月 1 日
コメント済み: aliza mustafa 2022 年 10 月 1 日
Hi everyone,
I have the equation:
(1)
I have:
and
I have to compute for 0, 1, 2, ... 10.
I did it this way in MATLAB:
S = [1/2, 1/3, 1/2, 1/3, 1/3, 1/2;
1/2, 0, 0, 0, 0, 0;
0, 1/3, 1/2, 0, 0, 0;
0, 0, 0, 0, 1/3, 0;
0, 1/3, 0, 1/3, 0, 0;
0, 0, 0, 1/3, 1/3, 1/2];
u_0 = [0, 0, 1, 0, 0 ,0]';
for i = 0:10
u_k = S.*u_i;
I am stuck here. How can I write the equation (1) in matlab? Any help will be highly appreciated. Thanks alot in advance.

採用された回答

Torsten
Torsten 2022 年 10 月 1 日
編集済み: Torsten 2022 年 10 月 1 日
S = [1/2, 1/3, 1/2, 1/3, 1/3, 1/2;
1/2, 0, 0, 0, 0, 0;
0, 1/3, 1/2, 0, 0, 0;
0, 0, 0, 0, 1/3, 0;
0, 1/3, 0, 1/3, 0, 0;
0, 0, 0, 1/3, 1/3, 1/2];
u_0 = [0, 0, 1, 0, 0 ,0]';
u = zeros(6,10);
u(:,1) = u_0;
for i = 2:11
u(:,i) = S*u(:,i-1);
end
u
u = 6×11
0 0.5000 0.5000 0.4583 0.4444 0.4433 0.4441 0.4444 0.4445 0.4445 0.4444 0 0 0.2500 0.2500 0.2292 0.2222 0.2216 0.2220 0.2222 0.2222 0.2222 1.0000 0.5000 0.2500 0.2083 0.1875 0.1701 0.1591 0.1535 0.1507 0.1494 0.1488 0 0 0 0 0.0278 0.0278 0.0285 0.0278 0.0278 0.0278 0.0278 0 0 0 0.0833 0.0833 0.0856 0.0833 0.0834 0.0833 0.0833 0.0833 0 0 0 0 0.0278 0.0509 0.0633 0.0689 0.0715 0.0728 0.0734
%u./vecnorm(u)
%null(S-eye(6))
  3 件のコメント
Torsten
Torsten 2022 年 10 月 1 日
To run it for 1,2,...,10, you must run the loop from 2 to 11. Think about it.
I corrected it above in my code.
aliza mustafa
aliza mustafa 2022 年 10 月 1 日
Thank you so much for your guidance.

サインインしてコメントする。

その他の回答 (1 件)

Davide Masiello
Davide Masiello 2022 年 10 月 1 日
編集済み: Davide Masiello 2022 年 10 月 1 日
I suspect you want to do something like this
S = [ 1/2, 1/3, 1/2, 1/3, 1/3, 1/2;...
1/2, 0, 0, 0, 0, 0;...
0, 1/3, 1/2, 0, 0, 0;...
0, 0, 0, 0, 1/3, 0;...
0, 1/3, 0, 1/3, 0, 0;...
0, 0, 0, 1/3, 1/3, 1/2;...
];
u = zeros(6,11);
u(:,1) = [0, 0, 1, 0, 0 ,0]';
for col = 2:11
u(:,col) = S*u(:,col-1);
end
u
u = 6×11
0 0.5000 0.5000 0.4583 0.4444 0.4433 0.4441 0.4444 0.4445 0.4445 0.4444 0 0 0.2500 0.2500 0.2292 0.2222 0.2216 0.2220 0.2222 0.2222 0.2222 1.0000 0.5000 0.2500 0.2083 0.1875 0.1701 0.1591 0.1535 0.1507 0.1494 0.1488 0 0 0 0 0.0278 0.0278 0.0285 0.0278 0.0278 0.0278 0.0278 0 0 0 0.0833 0.0833 0.0856 0.0833 0.0834 0.0833 0.0833 0.0833 0 0 0 0 0.0278 0.0509 0.0633 0.0689 0.0715 0.0728 0.0734
Beware of the fact that, to call , you must write
u(:,n+1)
This is because, unlike C, Matlab's indexing starts from 1 rather than 0.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by