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일

1 개 추천

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!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 4월 16일

댓글:

2020년 4월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by