mode point of a polynomial fit of histogram
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Meg Noah
on 6 Jan 2020
%% 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 Comments
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!