%
% This is a matlab script to fit the diode data.
% Page 33 in manual for the equation that we are fitting
%
% The only fitting parameter is Isub0
% function to.  Make sure to import these into MatLab before running this
function [vfit,ifit, params] = MJRfitd(va,i,T)
 
% Initialize the coefficients of the function
X0=[1];
 
% Set an options file for LSQNONLIN to use the
% medium-scale algorithm
options = optimset('Largescale','off');
 
% Calculate the new coefficients using LSQNONLIN
[params,renorm]=lsqnonlin(@MJRcomp,X0,[],[],options,va,i,T);
 
%define the fitted function
Z= params(1);
% Equation of fit
% I = I0 * (exp(q*Va/(Kb*T)) - 1)
% I0 is the fit parameter
%  http://en.wikipedia.org/wiki/Elementary_charge
q = 1.6*(10^-19); % 1.602176565(35)×10?19 C[1]
% http://en.wikipedia.org/wiki/Boltzmann_constant
% Kb = 8.617*(10^-5); %8.617?3324(78)×10?5	eV?K?1
Kb = 1.3806503* (10^-23);
Inew = Z*( exp((q*va)/(Kb*T)) -1);

%plot the data along with the fit function
vfit=va;
ifit = Inew;
figure;
plot(va, i, 'b*')
hold
plot(va, Inew,'r+')

end
