%%
clear

t = (1:10)';
A0 = [ 100; 3 ];
s = 1;

X = [ ones(size(t)) t ];

% Y0 = X*A0;
% FUNCAO = @(A)(A(1) + A(2)*t);

FUNCAO = @(A)( A(1) * exp(-t/A(2)) );
Y0 = FUNCAO(A0);
s = sqrt( Y0 );

Ac = A0;

Ntestes = 10000;
for qT = 1:Ntestes
    Y = Y0 + s.*randn(size(Y0));
%    Y = round(Y);
    Y( Y<0 ) = 0;
    si = sqrt(Y) + 1*(Y==0);
    [ A(:,qT), sA, Q2(qT), NGL, VA, F(:,qT) ] = mmq_naolin( FUNCAO, Y, si, Ac );
    si2 = sqrt( F(:,qT) );
    [ A2(:,qT), sA2, Q22(qT), NGL2, VA2, F2(:,qT) ] = mmq_naolin( FUNCAO, Y, si2, Ac );
end

% figure
% errorbar( t, Y, s*ones(size(Y)), 'or' )
iT = (1:Ntestes );
figure
subplot( 3,2,1 )
plot( iT, A(1,:), '.' )
title( sprintf( 'Am_1=%.3f \\pm %.3f', mean(A(1,:)), std(A(1,:))/sqrt(Ntestes) ), 'fontsize', 16 )
subplot( 3,2,2 )
plot( iT, A(2,:), '.' )
title( sprintf( 'Am_2=%.3f \\pm %.3f', mean(A(2,:)), std(A(2,:))/sqrt(Ntestes) ), 'fontsize', 16 )
subplot( 3,2,3 )
plot( iT, A2(1,:), '.' )
title( sprintf( 'A2m_1=%.3f \\pm %.3f', mean(A2(1,:)), std(A2(1,:))/sqrt(Ntestes) ), 'fontsize', 16 )
subplot( 3,2,4 )
plot( iT, A2(2,:), '.' )
title( sprintf( 'A2m_2=%.3f \\pm %.3f', mean(A2(2,:)), std(A2(2,:))/sqrt(Ntestes) ), 'fontsize', 16 )
subplot( 3,2,5 )
plot( iT, Q2(:), '.' )
subplot( 3,2,6 )
errorbar( t, Y, s, '.'), hold on, plot( t, F(:,end), '-r' )
