Solve MATLAB Question: Plot Normal Distribution on Histogram

  • Thread starter coffeem
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses how to plot a normal distribution on top of a histogram using 100 random values. The attempt at a solution involves changing the plot function and adjusting the x and y values. The conversation also mentions the need for suitable scaling and how to check for accuracy.
  • #1
coffeem
91
0

Homework Statement



Plot a suitably scaled normal distribution on top of a histogram made with 100 random values.


The Attempt at a Solution



x = -2,9:0.1:2.9;

y = randn(100,1);

nd = exp(-x.^2/2)/sqrt(2*pi);

hist(y,x);

hold on;

plot(x,nd);

plot(-x,nd);

holf off;

This is not working. What am I doing wrong? Also how do I ensure that it is suitably scaled?

Thanks
 
Physics news on Phys.org
  • #2
Hey
I think you have to change "plot(y,x)" to "plot(x,y)". Remove also "hold off;" (written as "holf off" but I suppose it is an type error). I don't understand why you first plot nd as a function of x and then as a function of -x. As nd is an even function you only need to plot it as a function of x.
 
  • #3
Umm i'll try and explain my thoughts then.

I plotted fn as both a function and a function of -x so that I got your average bell shaped curve. for this I need to go back to -3 as well as +3 on the x-axis. When I tried changing it to just x it did not work sadly.

Also I need to plat a histogram so changing it to plot would not work. the only way I can get the historgram to work is by plotting (y,x). thanks
 
  • #4
I have not got this far:

%%
x = 0:0.1:3;

nd = 10.*exp(-x.^2/2)/sqrt(2*pi);

plot(x,nd);

hold on;

plot(-x,nd);
%%
x = -2.9:0.1:2.9;

y = randn(100,1);

hist (y,x);

hold off

This will give me what I want. So all I need to do is suitably scale it. How do I do this? Is there some way I can tell how well it matches?

Thanks
 

What is a normal distribution?

A normal distribution, also known as a Gaussian distribution, is a statistical distribution that is often used to model real-world data. It is characterized by a bell-shaped curve and is symmetrical around the mean value.

How can I plot a normal distribution on a histogram in MATLAB?

To plot a normal distribution on a histogram in MATLAB, you can use the normplot function. This function takes in a vector of data and plots a histogram with a curve representing the normal distribution overlaid on top.

Can I change the parameters of the normal distribution on the histogram?

Yes, you can change the parameters of the normal distribution on the histogram by specifying the mean and standard deviation of the distribution when using the normplot function. You can also adjust the number of bins in the histogram to better visualize the distribution.

What is the purpose of plotting a normal distribution on a histogram?

Plotting a normal distribution on a histogram can help to visually analyze the distribution of a dataset and determine if it follows a normal distribution. It can also be used to compare different datasets and see how they differ in terms of their distribution.

Are there any other ways to plot a normal distribution in MATLAB?

Yes, there are other ways to plot a normal distribution in MATLAB besides using the normplot function. You can also use the normpdf function to plot the probability density function of a normal distribution or the normcdf function to plot the cumulative distribution function. Additionally, you can use the histogram function to plot a histogram and then overlay a normal distribution curve using the normpdf function.

Similar threads

  • Introductory Physics Homework Help
Replies
1
Views
993
  • Calculus and Beyond Homework Help
Replies
10
Views
981
  • High Energy, Nuclear, Particle Physics
Replies
9
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
868
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top