LU Decomp in a For loop

1 Ansicht (letzte 30 Tage)
Layla Bitar
Layla Bitar am 16 Apr. 2020
Kommentiert: Layla Bitar am 17 Apr. 2020
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

Antworten (1)

Geoff Hayes
Geoff Hayes am 17 Apr. 2020
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 Kommentar
Layla Bitar
Layla Bitar am 17 Apr. 2020
Thank you so much, this makes and sense and it really helped!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by