Search code examples
matlabcurve-fitting

Vectors must be the same length error in Curve Fitting in Matlab


I'm having problems in curve fitting my randomized data for the function

enter image description here

Here is my code

N = 100;
mu = 5; stdev = 2;
x = mu+stdev*randn(N,1);
bin=mu-6*stdev:0.5:mu+6*stdev;
f=hist(x,bin);
plot(bin,f,'bo'); hold on;

x_ = x(1):0.1:x(end); 
y_ = (1./sqrt(8.*pi)).*exp(-((x_-mu).^2)./8); 
plot(x_,y_,'b-'); hold on;

It seems like I'm having vector size problems since it is giving me the error

Error using plot
    Vectors must be the same length.

Note that I simplified y_ since mu and the standard deviation is known.

Plot:

enter image description here


Solution

  • Well first of all some adjustments to your question:

    • You are not trying to do curve fitting. What you are trying to do (in my opinion) is to overlay a probability density function on an histogram obtained by taking random points from the same distribution (A normal distribution with parameters (mu,sigma)). These two curve should indeed overlay, as they represent the same thing, only one is analytical and the other one is obtained numerically.

    • As seen in the hist documentation, hist is not recommended and you should use histogram instead

    First step: Generating your random data

    Knowing the distribution is the Normal distribution, we can use MATLAB's random function to do that :

    N = 150;
    rng('default') % For reproducibility
    mu = 5;
    sigma = 2;
    r = random('Normal',mu,sigma,N,1);
    

    Second step: Plot the histogram

    Because we don't just want a count of the elements in each bin, but a feel of the probability density function, we can use the 'Normalization' 'pdf' arguments

    Nbins = 25;
    f=histogram(r,Nbins,'Normalization','pdf');
    hold on
    

    Here I'd rather specify a number of bins than specifying the bins themselves, because you never know in advance how far from the mean your data is going to be.

    Histogram plot

    Last step: overlay the probability density function over the histogram

    The histogram being already consistent with a probability density function, it is sufficient to just overlay the density function:

    x_ = linspace(min(r),max(r),100);
    y_ = (1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
    plot(x_,y_,'b-');
    

    With N = 150

    N = 150

    With N = 1500

    N = 1500

    With N = 150.000 and Nbins = 50

    Last one

    If for some obscure reason you want to use old hist() function

    The old hist() function can't handle normalization, so you'll have to do it by hand, by normalizing your density function to fit your histogram:

    N = 1500;
    % rng('default') % For reproducibility
    mu = 5;
    sigma = 2;
    r = random('Normal',mu,sigma,1,N);
    Nbins = 50;
    [~,centers]=hist(r,Nbins);
    hist(r,Nbins); hold on
    
    % Width of bins
    Widths = diff(centers);
    
    x_ = linspace(min(r),max(r),100);
    y_ = N*mean(Widths)*(1./sqrt(2*sigma^2*pi)).*exp(-((x_-mu).^2)./(2*sigma^2)); 
    plot(x_,y_,'r-');
    

    BadHist