MATLAB: Fit specific function on scatter plot Matlab

curve fittingStatistics and Machine Learning Toolbox

So i have a scatterplot scatter(x,y) and I would like to find a best fit for a function of the type F(x)=e^(-(Bèta)*x) with x matching x and F(x) matching/approximating the corresponding y.
I would like to find the best fit and get the value of Bèta returned.
How do i do this?

Best Answer

If y = exp(-beta*x) then log(y) = -beta*x and beta = -log(y)/x . Your best fit in log space would then be approximately
beta = mean( -log(y) ./ x )
In linear space,
beta = lsqcurvefit(@(beta,x) exp(-beta*x), 3, x, y);
However, when I constructed artificial data by defining beta and using
c = 1;
y = exp(-beta*x + randn(size(x)) * c );
and then fit against that to recover beta, then I found that a closer beta to the one I defined could be calculated as
other_beta = lsqcurvefit(@(beta,x) beta*x, 3, x, -log(y));
As c gets reduced to (say) 1/10 then the two fittings become more similar.
With a noise model like
c = 1/15;
y = exp(-beta*x) + (2 * rand(size(x)) - 1) * c;
then the original beta version becomes a notably better fit. So which one to use would probably depend upon the model of noise / error that you have.
Related Question