MATLAB: How to fit data to a specific logarithmic formula

curve fittingfunctionlogarithmic

I am trying to fit a set of data to the function V(I) = a*[I + ln(1 + (I / b))] – c but am having some difficulty. MATLAB's 'cftool' was of no help…I also tried using 'lsqnonlin' but was getting some pretty funky results (the fit was not anywhere close to the data).
Anyone know of any possible solutions?
Thank you.
Also, here's the code I was trying to use for the lsqnonlin in case you'd like to view it:
newk0=[1 10 ];
newlb=[0 0.2];
newub=[100 300 ];
for i=1:10
diode2 = @(newk) (k(2)*(ydata(:,i)+log(1+(ydata(:,i)/k(1)))) - xdata);
newk(:,i)=lsqnonlin(diode2, newk0, newlb, newub, options);
end
where xdata is Voltage and ydata is current. There are 10 sets of data (for different temperatures) which is the reason for the 'for' loop.

Best Answer

If you have the Statistics and Machine Learning Toolbox, then you could use nlinfit.

Here is a simple example of its use:

rng 'default'
% Here is an example of using nlinfit(). For simplicity, none of
% of the fitted parameters are actually nonlinear!
% Define the data to be fit
x=(0:1:10)'; % Explanatory variable
y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect)
y = y + 20*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x,y,f,[1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')