% Test2 for solving the H-equation
% via L-Broyden's method

clear; close all

c = 1 - 1e-4; L = 4;
N = input('(return for default) N = ');
if isempty(N), N = 10000; end
fprintf('L-Broyden: c = %g, N = %i  L = %i\n\n',c,N,L)

x0 = ones(N,1);
tol = 5e-12; 
maxiter = 100;
prt = 1;

if exist('yzLBroyden','file')
    t0 = tic;
    [x,nrmF,iter] = yzLBroyden(@yzHeqn,x0,tol,maxiter,L,prt,c,N);
    fprintf('yzLBroyden using L = %i: mean(x) = %.8f\n',L,mean(x))
    fprintf('nrmF %g, iter %i, time %g\n\n',nrmF(end),iter,toc(t0))
    h = semilogy(1:iter,nrmF,'-o'); 
    set(h,'linewidth',2); grid on;
    title('Convergence History'); 
    xlabel('Iter'); ylabel('F-norm');
    legend('yzLBroyden: L=4');
end

if exist('myLBroyden','file')
    t0 = tic;
    [x,nrmF,iter] = myLBroyden(@myHeqn,x0,tol,maxiter,L+1,prt,c,N);
    fprintf('myLBroyden using L = %i: mean(x) = %.8f\n',L,mean(x))
    fprintf('nrmF %g, iter %i, time %g\n\n',nrmF(end),iter,toc(t0))
    hold on; 
    h = semilogy(1:iter,nrmF,'-o'); 
    set(h,'linewidth',2); grid on;
    title('Convergence History'); 
    xlabel('Iter'); ylabel('F-norm');
    hold off, legend('yzLBroyden: L=4','myLBroyden: L=5');
end
