Home > Net >  Vectors must be the same length error in Curve Fitting in Matlab
Vectors must be the same length error in Curve Fitting in Matlab

Time:06-02

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

CodePudding user response:

Well first of all some adjustments to your question:

  • You are not trying to do 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

  • Related