Running for loop on an equation
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
aliza mustafa
il 1 Ott 2022
Commentato: aliza mustafa
il 1 Ott 2022
Hi everyone,
I have the equation:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1142030/image.png)
I have:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1142035/image.png)
and
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1142040/image.png)
I have to compute
for 0, 1, 2, ... 10.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1142045/image.png)
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.
0 Commenti
Risposta accettata
Torsten
il 1 Ott 2022
Modificato: Torsten
il 1 Ott 2022
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./vecnorm(u)
%null(S-eye(6))
3 Commenti
Torsten
il 1 Ott 2022
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.
Più risposte (1)
Davide Masiello
il 1 Ott 2022
Modificato: Davide Masiello
il 1 Ott 2022
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
Beware of the fact that, to call
, you must write
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1142125/image.png)
u(:,n+1)
This is because, unlike C, Matlab's indexing starts from 1 rather than 0.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!