Loop Updating Vectors for Number of Iterations

조회 수: 7 (최근 30일)
Denzel Philip Belleza
Denzel Philip Belleza 2020년 10월 7일
댓글: Denzel Philip Belleza 2020년 10월 7일
Hello,
I am trying to create a loop that will update a vector for a given number of iterations but keep getting errors. The x0 vector is solved and I would like to set as the first iteration. I would appreciate any help :)
What I am Trying to Implement:
x0 = A\b
x1 = A\x0
x2 = A\x1
...
xn = A\x(n-1)
My Code:
clear, clc;
% Given Information
A = [3 2 3; 5 5 6; 9 8 9];
b = [1; 2; 3];
x0 = A\b; % Calculate x0 vector
% My Attempt at Loop
b_ = zeros(size(b)); % Initialize?
x_ = zeros(size(x0)); % Initialize?
for k = 1:200
x(k(1)) = x_ + x0 % First Iteration
b(k) = b_ + x(k) % Update b vector
x(k+1) = A\b(k) % Updated x vector
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 7일
Is it correct that your b is always the previous x (except for the first time) ? Your use of those updates with 0 is confusing the issue if they are not needed.
Denzel Philip Belleza
Denzel Philip Belleza 2020년 10월 7일
Hi Walter,
Yes, I am trying to have b always updated to the previous x.

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

답변 (1개)

Asad (Mehrzad) Khoddam
Asad (Mehrzad) Khoddam 2020년 10월 7일
Simply use
A = [3 2 3; 5 5 6; 9 8 9];
b = [1; 2; 3];
x=b;
for k=1:200
x=A\x;
end

카테고리

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