MATLAB Answers


Superimposing a user defined function on a histogram

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);


Do you have a question?

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.

I have everything done except for part d


No products are associated with this question.

0 Answers

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!