MATLAB Answers

How do I save the values of A matrix and B vector after each step of the forward elimination phase of the gaussian elimination?

1 ビュー (過去 30 日間)
Zaid Samadi
Zaid Samadi 2020 年 4 月 12 日
回答済み: Vinai Datta Thatiparthi 2020 年 4 月 17 日
Here's the code I wrote, for which I get the solutions but I need help storing the matrice iterations as A1 and B1, A2 and B2 etc. when forming the upper-half triangle. What should I add to the forward elimination bit to do so? Thanks!
A = [10 -2 -1 2 3 1 -4 7; 5 11 3 10 -3 3 3 -4; 7 12 1 5 3 -12 2 3;...
8 7 -2 1 3 2 2 4; 2 -15 -1 1 4 -1 8 3; 4 2 9 1 12 -1 4 1;...
-1 4 -7 -1 1 1 -1 -3; -1 3 4 1 3 -4 7 6];
B = [0 12 -5 3 -25 -26 9 -7]';
A0 = A;
B0 = B;
n = length(B);
%forward elimination phase
for k = 1 : n
for i = k + 1 : n
lambda = A(i,k)/A(k,k);
A(i,:) = A(i,:) - lambda * A(k,:);
B(i) = B(i) - lambda * B(k);
end
end
%back substitution phase
X = zeros(n,1);
%X(n) = A(n,n)/B(n)
for x = n:-1:1
X(x) = ( B(x) - A(x,x+1:n) * X(x+1:n) ) / A(x,x);
end
for i = 1:n
fprintf('X%d = %6.2f mm\n', i, X(i))
end

  0 件のコメント

Sign in to comment.

回答 (1 件)

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi 2020 年 4 月 17 日
Hey Zaid,
Use the save command within the for loop for forward elimination. Since the names of the variables that you want to save as are changing with every iteration, use strcat to get filenames.
for k = 1 : n
for i = k + 1 : n
% Forward Elimination Code
end
filenameA = strcat('A',num2str(k),'.mat');
filenameB = strcat('B',num2str(k),'.mat');
save(filenameA, 'A');
save(filenameB, 'B');
end
Hope this helps!

  0 件のコメント

Sign in to comment.


Translated by