Matlab Coding Display Help

I have this code:
function [A] = Gauss_Elim(A)
%Performs Gaussian Elimination%
N = length(A);
tol = 1e-6;
%for each pivot along the diagonal
for ii = 1:N-1
%for each row under the pivot
for jj=ii+1:N
if abs(A(jj,ii)) > tol
%factor is ratio between pivot value
%and entry below pivot in row jj
fac=A(ii,ii)/A(jj,ii);
%row replacement
A(jj,:) = fac*A(jj,:)-A(ii,:);
end
end
end
end
This code produces the correct answer, but how would I display the steps the code takes to get the correct answer?

回答 (2 件)

Dave B
Dave B 2021 年 11 月 2 日

1 投票

Do you mean you would like to see what's in A on each iteration? Here's 4-5 ways depending on how complicated you want it to be (I'll just do the full code and run it for the last one!)
  • You can remove the ; at the end of the line
A(jj,:) = fac*A(jj,:)-A(ii,:)
  • You can add the line disp(A)
A(jj,:) = fac*A(jj,:)-A(ii,:);
disp(A)
  • You could show it graphically by adding a call to imagesc on each iteration of the loop
A(jj,:) = fac*A(jj,:)-A(ii,:);
imagesc(A) % consider specifying CLim on the axes, as this will be changing on each iteration...
  • For the image solution, you might consider storing the (step-wise) output of A so that you can play the movie again later, or write it to a video file, etc.
function [B,A] = Gauss_Elim(A)
...
B=nan(N,N,sum(1:N)+1);
cnt=1;
for ii = 1:N-1
...
A(jj,:) = fac*A(jj,:)-A(ii,:);
B(:,:,cnt)=A;
cnt=cnt+1;
...
  • You could plot each step into a separate subplot (this could get slow if you have a large matrix)
Gauss_Elim(rand(10))
ans = 10×10
0.3995 0.4408 0.4586 0.4269 0.1814 0.0349 0.4339 0.6469 0.4362 0.6070 0 -0.3572 -0.1512 -0.1629 -0.1583 0.0285 -0.4141 -0.3145 -0.0164 -0.5157 0 0 -0.1919 0.1574 -0.0239 0.5764 0.3408 0.4011 0.2645 -0.1513 0 0 0 0.7394 0.1896 0.2065 1.1101 0.4552 -1.9977 1.2333 -0.0000 0 0 0 -0.9684 1.1577 -0.3696 1.7478 3.2485 -1.8617 0.0000 0 0 0 0.0000 -5.2155 -0.2409 -1.6482 -7.9881 3.7745 -0.0000 0 0 0 -0.0000 0 0.1991 0.1445 0.2797 0.4623 -0.0000 0 0 0 -0.0000 0 -0.0000 -1.9966 -2.5243 -5.4185 -0.0000 0 -0.0000 0.0000 -0.0000 0 0.0000 0.0000 17.8712 1.0736 0.0000 0 0.0000 -0.0000 0.0000 0 0.0000 -0.0000 0 28.2920
function [A] = Gauss_Elim(A)
t=tiledlayout('flow');
colormap turbo
%Performs Gaussian Elimination%
N = length(A);
tol = 1e-6;
%for each pivot along the diagonal
for ii = 1:N-1
%for each row under the pivot
for jj=ii+1:N
if abs(A(jj,ii)) > tol
%factor is ratio between pivot value
%and entry below pivot in row jj
fac=A(ii,ii)/A(jj,ii);
%row replacement
A(jj,:) = fac*A(jj,:)-A(ii,:);
imagesc(nexttile,A)
clims=get(t.Children,'CLim');
if iscell(clims)
clims=cell2mat(get(t.Children,'CLim'));
end
caxis([min(clims(:,1)) max(clims(:,2))])
drawnow
end
end
end
end

1 件のコメント

Nick
Nick 2021 年 11 月 2 日
Yes, sorry I meant to display the matrix A on each iteration. Thank you for the help!

サインインしてコメントする。

Image Analyst
Image Analyst 2021 年 11 月 3 日

0 投票

To show each step (each line of code as it's being executed), you can tell it
echo on
To show A, remove the semicolon from the end of the line.
A(jj,:) = fac*A(jj,:)-A(ii,:)

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 11 月 2 日

回答済み:

2021 年 11 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by