MATLAB: Optimising the fit of a function with 2 variables

>2 variablesbest fitMATLABoptimisation

Hi,
I'm using a Gaussian derivative function of the form
A*(mu-x)*exp(-((x-mu)^2)/(2*S^2)))/S^2
to fit some existing discrete data and I want to vary A and S to minimse the RMS error of the fit.
Does anyone know of a function that would allow me to do this, and how I should use it?
I'd appreciate any help!
Thanks,
Earle

Best Answer

The function nlinfit() from the Statistics Toolbox will do this.
Here is a simple example of the use of the function:
% 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 + 2*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(1)
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','nonlinear fit')
Note that I am not actually using nonlinear fitting parameters here, but I hope the idea is clear enough.