% Experiment with Gauss-Newton
x=[0;1;2;3];
y=[2;.7;.3;.1];
p=[1;0]; % Starting point
%p=[5;5]; % Starting point that doesn't converge
Fc = p(1)*exp(p(2).*x)-y;
i=0;
disp( ' i p residual');
disp(sprintf(' %1d [%2.4f,%2.4f] %1.8f',i,p(1),p(2),norm(Fc,2)^2));

for i=1:5
Jc=[exp(p(2).*x) x.*p(1).*exp(p(2).*x)];
[Q,R]=qr(Jc);
c=-Q'*Fc;
sc=R(1:2,:)\c(1:2);
p=p+sc;
Fc = p(1)*exp(p(2).*x)-y;
disp(sprintf(' %1d [%2.4f,%2.4f] %1.8f',i,p(1),p(2),norm(Fc,2)^2));
end
xplot=0:.2:3;
plot(x,y,'*',xplot,p(1)*exp(p(2).*xplot),'-');
legend('Data points','Model');
title('Nonlinear LS');