How to convert the for loop to a while loop?

1 visualización (últimos 30 días)
Avinash Gupta
Avinash Gupta el 30 de Mzo. de 2021
Editada: MG el 1 de Abr. de 2021
A = [7 3 2 1; 2 9 4 5; 1 3 13 4; 4 5 8 14 ]; % A is diagonally dominant
b = [1;2;3;4];
x = zeros(4,1);
x_new = zeros(4,1);
% Gauss Seidel using for loop
n = 4;
for iter = 1:25
for i = 1:n
num = b(i)-A(i,1:i-1)*x_new(1:i-1) - A(i,i+1:n)*x(i+1:n);
x_new(i) = num/A(i,i);
x = x_new;
end
disp(['At iteration = ',num2str(iter), ' x = ', num2str(x_new')]);
end
% The for loop works but the while loop doesn't
%% This is what I have done in the while loop.
% Gauss seidel using while loop
n = 4;
error = 0;
iter = 0;
while error >=1e-6
for i = 1:n
x_new(i) = (b(i)-A(i,1:i-1)*x_new(1:i-1)- A(i,i+1:n)*x(i+1:n))/A(i,i);
x = x_new;
error = abs(x-x_new);
end
iter = iter+1;
disp(['At iteration = ',num2str(iter), ' x = ', num2str(x_new')]);
end

Respuesta aceptada

MG
MG el 30 de Mzo. de 2021
Editada: MG el 30 de Mzo. de 2021
Hi Avinash,
I modified your code into something I believe is what you might want:
A = [7 3 2 1; 2 9 4 5; 1 3 13 4; 4 5 8 14 ]; % A is diagonally dominant
b = [1;2;3;4];
x = zeros(4,1);
x_new = zeros(4,1); %<---- modified
% Gauss seidel using while loop
n = 4;
error = ones(4,1); %<---- modified
iter = 0;
while prod( error >= 1e-6*ones(4,1) ) %<---- modified
for i = 1:n
x_new(i) = (b(i)-A(i,1:i-1)*x_new(1:i-1)- A(i,i+1:n)*x(i+1:n))/A(i,i);
error(i) = abs(x(i)-x_new(i)); %<---- modified
x(i) = x_new(i); %<---- modified to be after error
end
iter = iter+1;
disp(['At iteration = ',num2str(iter), ' x = ', num2str(x_new')]);
end
I then just notice that you have in your code this code sequence
A(i,1:i-1)*x_new(1:i-1)
and I don't know if that does what you expect, beceuse with i =1 then A(1,1:0) *x_new(1:0) is a multiplication of two empty matrices (that probably returns 0).
  3 comentarios
Avinash Gupta
Avinash Gupta el 31 de Mzo. de 2021
Hi, Michael. Thanks for the prompt response.
1. However, I do not understand this modification: while prod( error >= 1e-6*ones(4,1) )
Why can't we go with: while error >= 1e-5*? Kindly explain the difference
2. A(i,1:i-1)*x_new(1:i-1)-----> This is not so useful for x_new(1) and getting an empty matrix is fine at that step.It is redundant for x_new(1). However, it becomes useful for other x_new(i), i= 2,3,4
3. Also the error should be calculated before the update. Thanks for pointing it out
Ps: I am quite new to MATLAB and I am still navigating my way around
MG
MG el 31 de Mzo. de 2021
Editada: MG el 1 de Abr. de 2021
  1. You can use your original 'error >= 1e-6', it should do the same thing. I only did it to more explicitely show that it checks individually each of the elements in your 4 component 'error' array. If any is false, the loop stops. Replace 'prod' with 'any' if you instead want that every error(i) < 1e-6 before the while-loop ends.
  2. Fine, as long as it does what you intend (in practice that is a a multiplication of two empty arrows that here return 0, but I'm not really sure why it returns 0 and not an empty matrix).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by