LU Decomp in a For loop

Hello,
I am trying to write a code that decomposes matrix A to solve for vector b for 50 timesteps
I am given the b0 vector and am supposed to go from there till I reach the vector b50. Each iteration should overwtire the old vector with the new vector.
This is my code so far:
%find lower and upper of the A matrix Ax=b
% L U X = b
% d = b/L
% U X = d
% L d = b
[L U] = lu(A);
b0 = b;
d0 = L\b0;
x0 = U\d0
%%%%%
b1 = x0
d1 = L\b1;
x1 = U\d1
%%%%
b2 = x1
d2 = L\b2;
x2 = U\d2
%%%% and so on.....
but I want to continue this till b50
How would I be able to write this in a for loop for fifty iterations? I know that might be a basic question, but I am a beginner.
Thank you

Respuestas (1)

Geoff Hayes
Geoff Hayes el 17 de Abr. de 2020

1 voto

Layla - since your b* are really just the x*, you probably only need one 2D array for x that will store the results for each iteration. Storing all the data in one array is preferrable to storing the data in multiple variables. Try the following
x = []; % alternatively you can pre-size this if you know the number of rows of each x*
[L U] = lu(A);
x(:,1) = b;
for k = 1:51
b = x(:, k);
d = L\b;
x(:, k+1)= U\d;
end
We iterate 51 times so that we calculate the x0,x1,x2,...,x50 i.e. 51 columns of x.

1 comentario

Layla Bitar
Layla Bitar el 17 de Abr. de 2020
Thank you so much, this makes and sense and it really helped!

Iniciar sesión para comentar.

Categorías

Más información sobre Particle & Nuclear Physics en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Abr. de 2020

Comentada:

el 17 de Abr. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by