Using nlinfit to fit a Gaussian pdf to x,y paired data
8 ビュー (過去 30 日間)
古いコメントを表示
I am trying to use Matlab's nlinfit function to estimate the best fitting Gaussian pdf for x,y paired data. In this case, x is a range of 2D orientations and y is the probability of a "yes" response in a yes-no task.
I have borrowed the form for a Gaussian (@norm_funct) from relevant posts and I'd like to estimate the best fitting pdf to obtain its mean and standard deviation. At the moment, the fitted function appears to be incorrectly scaled and is not smooth (see plots)
The figure below, which was produced using Prism to fit 2 pdfs, is what I'd like to replicate in Matlab - many thanks
x = -30:5:30;
y = [0,0.20,0.05,0.15,0.65,0.85,0.88,0.80,0.55,0.20,0.05,0,0;];
% plot raw data
figure(1)
plot(x, y, ':rs');
axis([-35 35 0 1]);
% initial paramter guesses (based on plot)
initGuess(1) = max(y); % amplitude
initGuess(2) = 0; % mean centred on 0 degrees
initGuess(3) = 10; % SD in degrees
% equation for Gaussian distribution
norm_func = @(p,x) p(1) .* exp(-((x - p(2))/p(3)).^2);
% use nlinfit to fit Gaussian using Least Squares
[bestfit,resid]=nlinfit(y, x, norm_func, initGuess);
% plot function
xFine = linspace(-30,30,100);
figure(2)
plot(x, y, 'ro', x, norm_func(xFine, y), '-b');

1 件のコメント
Image Analyst
2013 年 11 月 28 日
編集済み: Image Analyst
2013 年 11 月 28 日
Should I delete your apparent duplicate http://www.mathworks.com/matlabcentral/answers/107961-using-nlinfit-to-fit-a-gaussian-pdf-to-x-y-paired-data? Or are they really different questions? Why don't we have just one thread so you don't have to look in two different places for answers?
回答 (1 件)
Matt J
2013 年 11 月 29 日
編集済み: Matt J
2013 年 11 月 29 日
Instead of
plot(x, y, 'ro', x, norm_func(xFine, y), '-b');
I think you want
plot(x, y, 'ro', xFine, norm_func(bestfit,xFine), '-b');
From this, I obtain the following (but from fminsearch, not nlinfit), and it looks fine to me,

3 件のコメント
Matt J
2013 年 11 月 29 日
編集済み: Matt J
2013 年 11 月 29 日
Doug,
Here's how I modified your original code to work with fminsearch. However, I wouldn't conclude that fminsearch is preferable to nlinfit. Fminsearch is, in fact, a rather ad hoc and less robust solver. The only reason I used it instead of nlinfit is because I don't have the Stats Toolbox.
x = -30:5:30;
y = [0,0.20,0.05,0.15,0.65,0.85,0.88,0.80,0.55,0.20,0.05,0,0;];
% plot raw data
figure(1)
plot(x, y, ':rs');
axis([-35 35 0 1]);
% initial paramter guesses (based on plot)
initGuess(1) = .9*max(y); % amplitude
initGuess(2) = 0; % mean centred on 0 degrees
initGuess(3) = 10; % SD in degrees
% equation for Gaussian distribution
norm_func = @(p,x) p(1) .* exp(-((x - p(2))/p(3)).^2);
% use nlinfit to fit Gaussian using Least Squares
[bestfit,resid]=fminsearch(@(p) norm(norm_func(p,x)-y), initGuess);
% plot function
xFine = linspace(-30,30,1000);
figure(2)
plot(x, y, 'ro', xFine, norm_func(bestfit,xFine), '-b');
参考
カテゴリ
Help Center および File Exchange で Get Started with Curve Fitting Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!