% nlinfit example (simplified)
% Adapted from:
% Tim Placek - placetd@auburn.edu (CHEN 2610 - Fall 2011)
clear
clc
clf
format compact

% load data
load('Exp3.mat')

% the data 
TSE1 = TSE(1:20);   % x = 2:16; 
%Pdet1 = Pdet(1:20); % y = [6.42,8.2,9.58,9.5,9.7,10,9.93,9.99,10.49,10.59,10.6,10.8,10.6,10.9,10.76];
MOS1 = mean(Ann,2);
MOS = MOS1(1:20);

% the proposed functionality (fh is a handle to the function)
% fh = @(b,x) 1./(b(1)+b(2)./x);
% p(e) = 1 - 2^(E/Et)^k
fh = @(b,TSE1) 100./(1 + exp(-((TSE1 - b(1))./b(2))));

% guess values for parameters (beta0)
b0 = [3.99,0.29];

% plot the raw data
plot(TSE1,MOS,'s','markersize',5,'color',[0,0,0]);  
hold on

% determine best fit values for coefficient (bhat)
bhat = nlinfit(TSE1,MOS,fh,b0);

% plot the fit
xf = linspace(TSE1(1), TSE1(length(TSE1)));
plot(xf,fh(bhat,xf),'linewidth',1,'color',[1,0,0]);      
legend('original data','fit data','location','Best') % the result 
bhat(1)
bhat(2)