%
%  trust-region solution for non-convex quadratic (easy case)
%
%  Nick Gould

lambda_min = -8.2;
lambda_max = 6;
psi_min = -0.2;
psi_max = 2.5;

tick = 0.05 ;
lambda_mint = -8.3 ;

inc = .005;

H = [-1 3 5];
g = [1 1 1];
lambda_crit = 1 ;
lambda_fade = lambda_min:inc:lambda_crit;
lambda = lambda_crit:inc:lambda_max;

hold off

s_fade = g(1) ./ ( H(1) + lambda_fade ) ;
psi_fade = s_fade.^2 ;
s_fade = g(2) ./ ( H(2) + lambda_fade ) ;
psi_fade = psi_fade + s_fade.^2 ;
s_fade = g(3) ./ ( H(3) + lambda_fade ) ;
psi_fade = psi_fade + s_fade.^2 ;

plot( lambda_fade, psi_fade, '--b' );
axis( [ lambda_mint lambda_max psi_min psi_max ] );

hold on

s = g(1) ./ ( H(1) + lambda ) ;
psi = s.^2 ;
s = g(2) ./ ( H(2) + lambda ) ;
psi = psi + s.^2 ;
s = g(3) ./ ( H(3) + lambda ) ;
psi = psi + s.^2 ;

plot( lambda, psi, '-r', 'LineWidth', 2 );

crit = [ lambda_crit lambda_crit ] ;
vertical = [ psi_min psi_max ] ;
plot( crit, vertical, ':k' );

axis off

plot( [ lambda_min lambda_max ], [ 0 0 ], '-k' );
plot( [ lambda_min lambda_min ], [ 0 psi_max ], '-k' );
plot( [ -8 -8 ], [ -tick 0 ], '-k' );
plot( [ -7 -7 ], [ -tick 0 ], '-k' );
plot( [ -6 -6 ], [ -tick 0 ], '-k' );
plot( [ -5 -5 ], [ -tick 0 ], '-k' );
plot( [ -4 -4 ], [ -tick 0 ], '-k' );
plot( [ -3 -3 ], [ -tick 0 ], '-k' );
plot( [ -2 -2 ], [ -tick 0 ], '-k' );
plot( [ -1 -1 ], [ -tick 0 ], '-k' );
plot( [  0  0 ], [ -tick 0 ], '-k' );
plot( [  1  1 ], [ -tick 0 ], '-k' );
plot( [  2  2 ], [ -tick 0 ], '-k' );
plot( [  3  3 ], [ -tick 0 ], '-k' );
plot( [  4  4 ], [ -tick 0 ], '-k' );
plot( [  5  5 ], [ -tick 0 ], '-k' );

plot( [ lambda_mint lambda_min ], [ 0 0 ], '-k' );
plot( [ lambda_mint lambda_min ], [ 0.5 0.5 ], '-k' );
plot( [ lambda_mint lambda_min ], [ 1 1 ], '-k' );
plot( [ lambda_mint lambda_min ], [ 1.5 1.5 ], '-k' );
plot( [ lambda_mint lambda_min ], [ 2 2 ], '-k' );
%plot( [ lambda_mint lambda_min ], [ 2.5 2.5 ], '-k' );

hold off

print -depsc eps/tr2

