Fitting Cumulative Gaussian Function to data
17 views (last 30 days)
Show older comments
Hello ,
I am trying to fit the cumulative Gaussian Function to my data points, to find out the PSE. So far I used this function:
f = @(b,x) normcdf(x, b(1), b(2)); % Objective Function
NRCF = @(b) norm(y - f(b,x)); % Norm Residual Cost Function
B1 = fminsearch(NRCF, [-1; 5]); % Estimate Parameters
x = 0:5
y = [0.7 0.4 0.2 0.4 0.6 0.9]
I am getting: 1.91293286
However, the correct value should be: 1.81051. Does anyone have ideas? Is there a better way to fit the cumulative function to my data in Matlab?
Thank you for your help :)
Accepted Answer
the cyclist
on 19 Mar 2021
I get the same coefficient that you did, using the fitnlm function instead.
This seems to be a terrible model for your data, which is obvious when you plot them. (Always plot the data!) Why did you choose it?
f = @(b,x) normcdf(x, b(1), b(2));
x = 0:5;
y = [0.7 0.4 0.2 0.4 0.6 0.9];
mdl = fitnlm(x,y,f,[1 1])
xq = (-2 : 0.1 : 8)';
yq = predict(mdl,xq);
figure
hold on
hd = plot(x,y,'.');
set(hd,'MarkerSize',24)
hf = plot(xq,yq);
set(hf,'LineWidth',2)
ylim([0 1])
legend([hd,hf],{'data','fit'},'Location','SouthWest')
4 Comments
the cyclist
on 22 Mar 2021
I'm a little confused by your remark, "That is an interesting model you propose here."
The model is exactly the same as in your original question. I just used a different MATLAB function to find the best fit. There is no "extra" fitting parameter. Your code reports the two coefficients, just the same:
x = 0:5
y = [0.7 0.4 0.2 0.4 0.6 0.9];
f = @(b,x) normcdf(x, b(1), b(2)); % Objective Function
NRCF = @(b) norm(y - f(b,x)); % Norm Residual Cost Function
B1 = fminsearch(NRCF, [-1; 5]) % Estimate Parameters
And then, since this is a model that fits a model of the form f(x) = y, all I do in the last part is create a sequence of uniformly space x values, and find the "predicted" y values given by the fitted equation. You could have done that same with your output. Here is a tiny example:
xq = [-1 0 1];
yq = f(B1,xq)
I still don't understand why you are trying to fit this function to the data points, which are shaped nothing like a CDF.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!