mode point of a polynomial fit of histogram

조회 수: 9 (최근 30일)
Tamir Eisenstein
Tamir Eisenstein 2020년 1월 5일
답변: Meg Noah 2020년 1월 6일
Dear MATLAB experts,
I have a vector (1x18864) which I generated a histogram to, using the histogram(variable,nbins) function.
How can I fit an 8th order polynomial and find the mode point of the polynomial?
Thanks!

답변 (1개)

Meg Noah
Meg Noah 2020년 1월 6일
%% Question
% I have a vector (1x18864) which I generated a histogram to, using the
% histogram(variable,nbins) function.
% How can I fit an 8th order polynomial and find the mode point of the polynomial?
%% Solution
% run multiple times for random data to see if it is producing what you
% were looking for.
clc
clear all
close all
% making better fake data to illustrate
NX = 124;
NY = 153;
[ICOL,IROW] = meshgrid(1:NX,1:NY);
Z = abs(ifft2(ifftshift((hypot(IROW,ICOL)+1e-5).^(-2.1).*exp(2i*pi*rand(NY,NX)))));
Z = reshape(Z,1,NX*NY);
figure();
hdata = histogram(Z);
hold on;
y = hdata.Values;
x = hdata.BinEdges(1:end-1) + hdata.BinWidth/2;
P = polyfit(x,y,8);
yfit = polyval(P,x);
plot(x,y,'+b','displayname','histogram data');
hold on;
plot(x,yfit,':k','displayname','polynomial fit');
xlim = get(gca,'xlim');
ylim = get(gca,'ylim');
set(gca,'ylim',[0 ylim(2)]);
ylim = get(gca,'ylim');
% digital way to find the mode
idx = find(yfit == max(yfit));
plot([x(idx) x(idx)],[0 yfit(idx)],'r','displayname','mode');
legend('location','best');
% analog way is to solve the differential set to zero
% use the 'roots' function to find the zero points

카테고리

Help CenterFile Exchange에서 Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by