LU Decomp in a For loop

1 次查看(过去 30 天)
Layla Bitar
Layla Bitar 2020-4-16
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

回答(1 个)

Geoff Hayes
Geoff Hayes 2020-4-17
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 个评论
Layla Bitar
Layla Bitar 2020-4-17
Thank you so much, this makes and sense and it really helped!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by