Hi all,
I am trying to find a way of storing the values of Error (err) for each iteration of the while-loop so I can plot the error against number of iterations.
The problem with the code below, is that I only get to see the error of the last iteration only.
Your help would really be appreciated.
while (max(err) >= tol) && (it < max_it)
it = it +1;
x_old = x;
for i = 1:n_eq
x(i) = (b(i) - (A(i,:) * x)) / A(i,i) + x(i);
end
err = abs((x - x_old) ./ x); %Calculates the error
end

 Respuesta aceptada

Jan
Jan el 29 de Oct. de 2017
Editada: Jan el 29 de Oct. de 2017
it = 0;
errOut = zeros(numel(x), max_it); % Pre-allocate
while (max(err) >= tol) && (it < max_it)
it = it + 1;
x_old = x;
for i = 1:n_eq
x(i) = (b(i) - (A(i,:) * x)) / A(i,i) + x(i);
end
err = abs((x - x_old) ./ x); %Calculates the error
errOut(:, it) = err(:);
end
errOut = errOut(:, 1:it);

1 comentario

Mohammed Al Muqbel
Mohammed Al Muqbel el 29 de Oct. de 2017
Amazing ! It worked!
Thanks very much for your time sir!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Oct. de 2017

Comentada:

el 29 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by