Matlab Coding Display Help
Afficher commentaires plus anciens
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?
Réponses (2)
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))
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 commentaire
Nick
le 2 Nov 2021
Image Analyst
le 3 Nov 2021
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,:)
Catégories
En savoir plus sur Spline Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
