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

10 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

James Tursa
James Tursa el 7 de Abr. de 2021
Editada: James Tursa el 7 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by