Asked by Chris
on 23 Nov 2012

Here is my attempt where the hist is bar(I,Y) and I want to superimpose Gaussian onto it

Z=[2 3 0 6 0 9 4 0]; N=sum(Z); M=sum(Z(6:8)); m=M; O=sqrt(N); Y=randn(10000,1)*O + m; x=mean(Y); s=std(Y); E=(abs(m-x)/m)*100; PE=(abs(O-s)/O)*100; Y=hist(Y,10000)/10000; Y=Y'; I=linspace(m-3*O,m+3*O,10000); bar(I,Y); hold on plot(I,Gaussian(x,m,O))

Here is the function

function [ gauss e u ] = Gaussian(x,m,O) %This is the Gaussian function % using variables x, m, and o d=1/sqrt(2*pi); e=2*O^2; u=(x-m)^2; gauss=(d*(O^(-1)))^(-u/e);

*No products are associated with this question.*

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 3 Comments

## Image Analyst (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54615-superimposing-a-user-defined-function-on-a-histogram#comment_112962

Do you have a question?

## Chris (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54615-superimposing-a-user-defined-function-on-a-histogram#comment_112964

Here is the full question: sorry its jumbled but was in a pdf

For this problem, define the following constants, based on your student number, to be used in generating the random numbers.

N = the sum of the nine digits of your student #.

M = the sum of the last three digits of your student #.

μ = M and 2 s = N

Use the Matlab function ‘randn’ to generate 10,000 Gaussian random variables with mean μ and variance 2 s .

(a) Estimate the sample mean () and sample standard deviation (s) from the generated random samples using the MATLAB functions mean and std, respectively. Compare these values with μ and s by calculating the corresponding percent error.

Note: Percent error: 100% μ x μ − × and 100% s s s − × .

(b) Plot a histogram of the samples (using the Matlab function 'hist' ). Plot the relative frequency by normalizing with the total number of samples. Use the MATLAB function linspace to specify your x-axis in the interval (μ − 3s ,μ + 3s ).

(c) Write a user-defined function called Gaussian that takes in x, μ and s , and returns the Gaussian function given by 2 2 1 ( ) ( ) exp . 2 2 X x f x − = − μ p s s

(d) Using the Matlab command ‘plot’ and your user-defined function Gaussian, superimpose your histogram with the Gaussian function.

## Chris (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54615-superimposing-a-user-defined-function-on-a-histogram#comment_112966

I have everything done except for part d