Calculating and and recording a vector during each iteration of a "while" loop.

10 views (last 30 days)
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.

Accepted Answer

James Tursa
James Tursa on 7 Apr 2021
Edited: James Tursa on 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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!