MATLAB: Fitting data with custom equation

atandatafittingfunctionMATLAB

Hi,
I have some datas to fit with by the following equation : d*atan(b*(x+a))+c . I've done it by the " curve fitting tool" how I can incorporate this process in a matlab script ? I'd like to obtain the parameter a,b,c,d as an answer.
Thanks.
math.png

Best Answer

Hi,
you can use this code:
% Some data - replace it with yours (its from an earlier project)
x = [177600,961200, 2504000, 4997000, 8884000]';
y = [6.754, 24.416, 58.622, 107.980, 154.507]';
% Define Start points, fit-function and fit curve
x0 = [1 1 1 1];
fitfun = fittype( @(a,b,c,d,x) d*atan(b*(x+a))+c );
[fitted_curve,gof] = fit(x,y,fitfun,'StartPoint',x0)
% Save the coeffiecient values for a,b,c and d in a vector
coeffvals = coeffvalues(fitted_curve);
% Plot results
scatter(x, y, 'r+')
hold on
plot(x,fitted_curve(x))
hold off
Due to, that this code is taken from an older project and the values dont represent your function the result here is a very bad fit - but to show how it works with a custom function inside a script it should be good enough.
Best regards
Stephan