Search code examples
matlabplotconfidence-interval

How do I plot confidence intervals in MATLAB?


I want to plot some confidence interval graphs in MATLAB but I don't have any idea at all how to do it. I have the data in a .xls file.

Can someone give me a hint, or does anyone know commands for plotting CIs?


Solution

  • I'm not sure what you meant by confidence intervals graph, but this is an example of how to plot a two-sided 95% CI of a normal distribution:

    alpha = 0.05;          % significance level
    mu = 10;               % mean
    sigma = 2;             % std
    cutoff1 = norminv(alpha, mu, sigma);
    cutoff2 = norminv(1-alpha, mu, sigma);
    x = [linspace(mu-4*sigma,cutoff1), ...
        linspace(cutoff1,cutoff2), ...
        linspace(cutoff2,mu+4*sigma)];
    y = normpdf(x, mu, sigma);
    plot(x,y)
    
    xlo = [x(x<=cutoff1) cutoff1];
    ylo = [y(x<=cutoff1) 0];
    patch(xlo, ylo, 'b')
    
    xhi = [cutoff2 x(x>=cutoff2)];
    yhi = [0 y(x>=cutoff2)];
    patch(xhi, yhi, 'b')
    

    plot