% ExponentialCurveTestLS.m (L. Braile, Sept. 2002, revised Feb. 2006) % non-linear least squares by transformation to a % linear equation - Exponential curve test y = ae^(bx) x = [1 2 3 4 5 6 7 8 9]; % x data y = [200 100 50 25 12 6 3 1.5 0.75]; % y data % Take log of y npts = max(size(x)); % generate 20% (std) noise and add to data noise = randn(1,npts); % normally distributed random noise noise = 0.2.*y.*noise; % scale noise to 20% of data value y = y + noise; % add noise to y data logy = log(y); sigmay = ones(npts,1); mode = 0; [a,sigmaa,b,sigmab,r]=linfit(x,logy,sigmay,npts,mode) a = exp(a) % correct a to original form % Calculate values of y for the fitted line xt = 0.2:0.2:9.8; yt = a*exp(b*xt); % Plot data and fitted line on linear axes graph plot(x,y,'or','markersize',10,'linewidth',2) hold on plot(xt,yt,'-b','linewidth',2) xlabel('x data -- Exponential Curve -- linear plot','fontsize',16) ylabel('y data','fontsize',16) title('Least Squares Fit -- Exponential Curve (y = a*exp(bx))','fontsize',16) hold off figure % Plot data and fitted line on semilog axes graph semilogy(x,y,'or','markersize',10,'linewidth',2) hold on semilogy(xt,yt,'-b','linewidth',2) xlabel('x data -- Exponential Curve -- semilog plot','fontsize',16) ylabel('y data','fontsize',16) title('Least Squares Fit -- Exponential Curve (y = a*exp(bx))','fontsize',16) axis([1 10 0.1 1000]) hold off