Fixing code logic bug (Linear Algebra + MATLAB)

1 view (last 30 days)
The problem suggests comparing a2 and a3 output results. I believe the results (a2 and a3) are supposed to be the same, just through different methods (Jacobi vs. G-S). I tried deleting the first 'for-loop' and when outputting a2 and a3 I obtained the same result, but while they are the same, I am not sure they are correct. Thanks!
  2 Comments
Michael Haderlein
Michael Haderlein on 11 Aug 2014
I don't think that this forum is meant to solve your homework. Exercises are should be a training for you. You're not gonna learn a lot from copying the exercise to the forum and show the answer to your teacher. If you have a specific question, you're welcome to ask it.
Best regards,
Michael
Yu Jiang
Yu Jiang on 11 Aug 2014
Hi Nicolas
It would be more helpful if you could explain what the expect results are. Also, what output variable should we look at?
-Yu

Sign in to comment.

Accepted Answer

Yu Jiang
Yu Jiang on 11 Aug 2014
Edited: Yu Jiang on 11 Aug 2014
Hi Nicolas
From what I understand, two different approaches are used here and they should lead to the same results in the last few iterations.
So, I don't think a2 and a3 should be identically the same, since results in the first 5 iterations steps in the two different methods can be different. Therefore, what should be compared seems to be a2(:,6:10) and a3(:,6:10).
Also, you are right that there is a logic bug in the code. In method #1, 5129 iterations happened and therefore the matrix phi contains 5130 different columns and the last 5 columns contain the last few iteration results. This part is correct.
However, in the second method, the matrix phi with 5130 columns is reused. But there are actually less than 5130 iterations happened (there are only 2566 iterations in method #2 if you check the value of j). Therefore, the last 5 iteration results obtained from the second method are stored in phi(:, 2566-4, 2566), not phi(:, end-4, end). Data in columns 2577 to 5129 in phi are obtained from the first method and it has nothing to do with the second method.
To fix this bug, you may simply replace the last line to
a3(:,6:10) = phi(:,j-4:j)
Or, you could initialize the matrix phi by replacing the following line
phi(:,1) = ones(99,1);
with
phi = ones(99,1);
Then, eventually you should compare a2(:,6:10) and a3(:,6:10).
From what I testes here, they are the same.
To do the comparsion, you can simply try executing
find(a2(:,6:10) -a3(:,6:10))
The returned value should be
Empty matrix: 0-by-1
meaning the two matrix, or the results from the last iterations in both methods, are the same.
-Yu

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!