%laboratorio 13.01.23

%autovalori Gershgorin
close all
clear all
warning off

A=[4 -1 1 0 0;1 3 -1 0 0; 0 1 1 0 0; 0 0 0 2 1; 0 0 0 1 8];

v=eig(A)

f1=figure
hold on

f2=figure
hold on

RR=polyshape([0], [0]);
CC=polyshape([0], [0]);

t=linspace(0,2*pi,100);
t=t(1:end-1);

for i=1:length(v)
    r(i)=sum(abs(A(i, [1:i-1,i+1:end])));
    R(i)=polyshape(r(i)*cos(t)+A(i,i),r(i)*sin(t));
    figure(f1)
    subplot(2,1,1)
    hold on
    plot(R(i))
    text (A(i,i),0,strcat('R_',num2str(i)'));

    c(i)=sum(abs(A([1:i-1,i+1:end],i)));
    C(i)=polyshape(c(i)*cos(t)+A(i,i),c(i)*sin(t));
    figure(f2)
    subplot(2,1,1)
    hold on
    plot(C(i))
    text (A(i,i),0,strcat('C_',num2str(i)'));
    
    RR=union(RR,R(i));
    CC=union(CC,C(i));

end

figure(f1)
subplot(2,1,1)
title('Cerchi per righe')
subplot(2,1,2)
hold on
plot(complex(v),'*k')
plot(RR)
title('Unione')
figure(f2)
subplot(2,1,1)
title('Cerchi per colonne')
subplot(2,1,2)
hold on
plot(CC)
plot(complex(v),'*k')
title('Unione')

figure(f1)
set(findobj(gcf,'type','axes'),'DataAspectRatio',[1 1 1])
set( findall(0, '-property', 'FontSize'), 'FontSize', 20);
set( findall(0, '-property', 'FontName'), 'FontName', 'times');
figure(f2)
set(findobj(gcf,'type','axes'),'DataAspectRatio',[1 1 1])
set( findall(0, '-property', 'FontSize'), 'FontSize', 20);
set( findall(0, '-property', 'FontName'), 'FontName', 'times');

f3=figure;
hold on
U=intersect(CC,RR);
plot(U)
plot(complex(v),'*k')
title('Intersezione')

set(findobj(gcf,'type','axes'),'DataAspectRatio',[1 1 1])
set( findall(0, '-property', 'FontSize'), 'FontSize', 20);
set( findall(0, '-property', 'FontName'), 'FontName', 'times');
