Solve MATLAB Question: Plot Normal Distribution on Histogram

  • Thread starter Thread starter coffeem
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Homework Help Overview

The discussion revolves around plotting a normal distribution on top of a histogram created from 100 random values using MATLAB. Participants are exploring how to correctly scale the normal distribution to match the histogram.

Discussion Character

  • Exploratory, Mathematical reasoning, Assumption checking

Approaches and Questions Raised

  • Participants are attempting to plot a histogram of random values and overlay a normal distribution. Questions arise regarding the correct scaling of the normal distribution and the proper use of MATLAB functions for plotting.

Discussion Status

Some participants are providing suggestions for correcting code errors and clarifying the plotting process. There is an ongoing exploration of how to ensure the normal distribution is suitably scaled to fit the histogram, with no explicit consensus reached on the best approach yet.

Contextual Notes

Participants are working within the constraints of MATLAB syntax and are questioning the assumptions behind their plotting methods. There is mention of needing to ensure the normal distribution matches the histogram visually and statistically.

coffeem
Messages
91
Reaction score
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
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.
 
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
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K