Optimising the fit of a function with 2 variables

4 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2012-3-27
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.

更多回答(1 个)

Frederic Moisy
Frederic Moisy 2012-5-14
You can also use the Ezyfit toolbox, which is free: http://www.mathworks.com/matlabcentral/fileexchange/10176
One installed, you can perform your fit like this:
f = ezfit(x,y,'A*(mu-x)*exp(-((x-mu)^2)/(2*S^2)))/S^2');
See also the example here:

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by