function test_arnoldi()
clear all
close all
colors = ['k', 'r', 'g', 'b', 'c', 'm', 'k'];
symbol = ['^', 'o', '*', 's', 'x', '+', 'd'];
N = 100;
h = zeros(N,N);
L = zeros(N,1);
A = randn(N,N);
nmax = N;
b = ones(N,1);
h(1,1) = norm(b);
Q(:,1) = b/h(1,1);
figure
hold on
for n = 1:nmax
    v = spec_mul(A,Q(:,n));
    for j = 1:n
        h(j,n) = conj(Q(:,j)')*v;
        v = v-h(j,n)*Q(:,j);
    end
    h(n+1,n) = norm(v);
    if (h(n+1,n) == 0)
        sprintf('0 pivot found')
    end
    Q(:,n+1) = v/h(n+1,n);
    if (mod(n,10) == 0)
       L(1:n) = eig(h(1:n,1:n));
       k = mod(n,7)+1;
       plot(real(L(1:30)),imag(L(1:30)),[colors(k),symbol(k)]);
    end
end
L(1:N) = eig(A);
plot(real(L(1:40)),imag(L(1:40)),[colors(2),symbol(2)]);

function y = spec_mul(A,x)
y = A*x;