Info

此问题已关闭。 请重新打开它进行编辑或回答。

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 天)
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

回答(1 个)

Vinai Datta Thatiparthi
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!

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by