Filter löschen
Filter löschen

mode point of a polynomial fit of histogram

4 Ansichten (letzte 30 Tage)
Tamir Eisenstein
Tamir Eisenstein am 5 Jan. 2020
Beantwortet: Meg Noah am 6 Jan. 2020
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!

Antworten (1)

Meg Noah
Meg Noah am 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

Kategorien

Mehr zu Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by