matlab-oh-no
matlab-oh-no

Reputation: 51

Simple curve fitting

I have 2 vectors

x = [ -11.1821  -9.8248  -8.4675  -7.1101  -5.7528 -4.3955  -3.0382  -1.6808  -0.3235  1.0338  2.3912  3.7485 ]

y = [ 0   0   0   0   0   0   7   1   0   0   0   0 ]

which are histogram data.

I tried interp1 and spline methods to draw a curve instead on connecting the points with lines, but the first one connects points by lines and the second one goes to negative values, which make no sense in a histogram.

Is there any simple way how I can overlay the data with a curve ? I don't have the curve fitting toolbox.

EDIT: I cannot use histfit even though it's histogram data. The bin count has to be created manually.

Upvotes: 1

Views: 228

Answers (1)

matlab-oh-no
matlab-oh-no

Reputation: 51

Figured it out.

When fitting a curve to histogram data, you can avoid interpolating into negative values (as spline does) by utilizing either the 'pchip' or 'cubic' option.

xi = interp1(1:n,x,linspace(1,n,10*n));
yi = interp1(x,y,xi,'pchip');
plot(xi,yi)

Hope this helps someone out there.

Upvotes: 1

Related Questions