mode point of a polynomial fit of histogram
4 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Polynomials についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!