- N = 256;: This line sets the value of N to 256. This determines the number of points we want to create on the circle.
- t = (0:N)*2*pi/N;: This line creates the vector t by performing the following steps:
- (0:N) creates a row vector from 0 to N, where each element represents the index of a point on the circle.
- 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.
Goodmorning.Can some explain to me some steps of this program (downloaded from "Gershgorin Discs Plot Version by Mario Berge" I have highlighted them.
5 次查看(过去 30 天)
显示 更早的评论
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.
Thanks very much for your answers!
% 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
0 个评论
回答(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:
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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!