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
댓글 수: 0
답변 (1개)
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
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!