mode point of a polynomial fit of histogram

4 ビュー (過去 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 Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by