Calculating and and recording a vector during each iteration of a "while" loop.
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Wojciech Polyn
am 7 Apr. 2021
Bearbeitet: James Tursa
am 7 Apr. 2021
I have searched the forums for similar answered questions and tried the solutions, but I unfortunately I can't get any of them to work for me.
(...)
lambda = 1;
lambda0 = 2;
while abs(lambda0 - lambda) > (1*10^-6)
R = M * R;
R = R / norm(R,1);
lambda0 = lambda;
lambda = R' * M * R / (R' * R);
end
Rfinal = R;
I've performed a while loop during which, through iterations, I reach a final set of values saved as vector "R". The final vector "R" is saved as "Rfinal".
Afterwards, in each iteration of that previous while loop, I need to record the values of as "power_err" using the "R" of the current iteration. This will allow me to plot log10(power_err) for the iterations.
I'm unable to write a proper code to fulfil that task. Any help is appreciated.
0 Kommentare
Akzeptierte Antwort
James Tursa
am 7 Apr. 2021
Bearbeitet: James Tursa
am 7 Apr. 2021
E.g., using a counter to store all of your iteration data in a cell array and then post process this:
k = 1; % the counter
Rcell = cell(1,1000); % some size larger than you expect
Rcell{1} = R;
while abs(lambda0 - lambda) > (1*10^-6)
R = M * R;
R = R / norm(R,1);
lambda0 = lambda;
lambda = R' * M * R / (R' * R);
k = k + 1;
Rcell{k} = R; % store current R in cell array
end
Rfinal = R;
power_err = zeros(1,k);
for x=1:k
power_err(x) = your calculation here involving Rfinal and Rcell{x}
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!