# Goodmorning.Can some explain to me some steps of this program (downloaded from "Gershgorin Discs Plot Version by Mario Berge" I have highlighted them.

26 次查看（过去 30 天）
Federica Mina 2023-5-15

Goodmorning.Can some explain to me some steps of this program (downloaded from "Gershgorin Discs Plot Version by Mario Berge" I have highlighted them. And I want to ask also how can I first plot just the "row" circles and then the "column" circles.
% gerschdisc.m
%
% This function plots the Gershgorin Discs for the matrix A passed as an argument.
% It will also plot the centers of such discs, and the actual eigenvalues
% of the matrix.
function gershdisc(A)
error(nargchk(nargin,1,1));
if size(A,1) ~= size(A,2)
error('Matrix should be square');
return;
end
% For each row, we say:
for i=1:size(A,1)
% The circle has center in (h,k) where h is the real part of A(i,i) and
% k is the imaginary part of A(i,i) :
h=real(A(i,i)); k=imag(A(i,i));
% Now we try to compute the radius of the circle, which is nothing more
% than the sum of norm of the elements in the row where i != j
r=0;
for j=1:size(A,1)
if i ~= j
r=r+(norm(A(i,j)));
end
end
% We try to make a vector of points for the circle:
N=256;
t=(0:N)*2*pi/N;
% Now we're able to map each of the elements of this vector into a
% circle:
plot( r*cos(t)+h, r*sin(t)+k ,'-');
% We also plot the center of the circle for better undesrtanding:
hold on;
plot( h, k,'+');
end
% For the circles to be better graphed, we would like to have equal axis:
axis equal;
% Now we plot the actual eigenvalues of the matrix:
ev=eig(A);
for i=1:size(ev)
rev=plot(real(ev(i)),imag(ev(i)),'ro');
end
hold off;
legend(rev,'Actual Eigenvalues');
end

### 回答（1 个）

Dinesh 2023-6-9
Hi Federica!
In the given code the highlighted part code is used create a vector 't' of 'N+1' equally spaced points around the circumference of a circle. Here's what each line does:
1. N = 256;: This line sets the value of N to 256. This determines the number of points we want to create on the circle.
2. t = (0:N)*2*pi/N;: This line creates the vector t by performing the following steps:
3. (0:N) creates a row vector from 0 to N, where each element represents the index of a point on the circle.
4. 2*pi/N scales the vector by multiplying each element by 2*pi/N. This step ensures that the points span the full circumference of the circle, from 0 to 2*pi.
So, the resulting vector 't' contains 'N+1' equally spaced points around the circle, covering the range from 0 to 2*pi. These points will be later used to map each point on the circumference of the circle when plotting the Gershgorin Discs.
To plot the row circles and column circles seperately you can introduce two separate loops: one for rows and one for columns.
a sample code would look like this
% Plot row circles
for i = 1:size(A,1)
h = real(A(i,i));
k = imag(A(i,i));
r = 0;
for j = 1:size(A,1)
if i ~= j
r = r + norm(A(i,j));
end
end
N = 256;
t = (0:N)*2*pi/N;
plot(r*cos(t) + h, r*sin(t) + k, '-');
hold on;
plot(h, k, '+');
end
Similarly for the column circles Change the
for i = 1:size(A,2)
...
for j = i:size(A,2)
...
end
end
Hope this helps,
Thank you!
##### 1 个评论显示 无隐藏 无
Federica Mina 2023-6-11

### 类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

### Community Treasure Hunt

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

Start Hunting!