clc;
close all;
clear
R = [1 -.5 -.4; -.5 1 0; -.4 0 1];
Q = [1 0 -2; 0 1 0; -2 0 1];
cnt = [-1; -1; -1];
e = [1; 1; 1];
uRoots = roots([3353, 17080, 24623, 6420, - 3300])
uSing = [-1, - (10*181^(1/2))/59 - 20/59, (10*181^(1/2))/59 - 20/59]
[uR,uI] = ndgrid(linspace(-3,2,200),linspace(-0.8,0.8,200));
resMat = nan(size(uR));
for k = 1:numel(uR)
u = uR(k) + 1i*uI(k);
[r,v,x] = resfun1(u , R,Q,cnt,e);
resMat(k) = real(r);
end
figure(1); cla; hold on;
contourf(uR,uI,resMat,linspace(-1,1,35)*0.8,'LineStyle','none')
contour(uR,uI,resMat,[0,0],'EdgeColor','w','LineWidth',1)
colormap turbo
shading interp
colorbar
set(gca,'DataAspectRatio',[1 1 1])
plot(uSing,zeros(size(uSing)),'xw','LineWidth',1,'MarkerSize',6)
plot(uRoots,zeros(size(uRoots)),'ow','LineWidth',1,'MarkerSize',4)
cnt = [mean(uRoots(1:2)),mean(uRoots(3:4))]
rad = (uRoots([2,4])'-c)
plot(cnt,zeros(size(cnt)),'+k')
xlabel('real part of $u$','Interpreter','latex')
ylabel('imaginary part of $u$','Interpreter','latex')
title('residual $\left(1-x^T R x\right)$','Interpreter','latex')
function [r,v,x] = resfun1(u , R,Q,c,e)
Z = Q+Q'+2*u*R;
Zi = Z\eye(3);
v = -(1+e'*Zi*c)/(e'*Zi*e);
x = -Zi*(c+v*e);
r = 1 - x'*R*x;
end