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

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# how can I create a plot with box the density function of a normal distribution and a log normal distribution

Asked by Locks on 25 Mar 2013

I would like to have both:

a) a plot with the density function of a normal distributed random variable and the densitiy function of a log-normal distributed random variabel

b) a plot with the densitiy function of a normal distributed random variable and the density function of a t-distributed random variable for several degrees of freedom

## Products

No products are associated with this question.

Answer by Tom Lane on 27 Mar 2013

You could simply use the normpdf, lognpdf, and tpdf function to compute these densities, and plot the results together.

If you need to fit the distributions to data, here's an example that might get you started:

```x = wblrnd(10,2,1000,1);
[heights,centers] = hist(x,40);
area = (centers(2)-centers(1))*sum(heights);
bar(centers,heights/area,'hist') % normalize histogram to integrate to 1
p1 = fitdist(x,'normal');        % fit three distributions
p2 = fitdist(x,'lognormal');
p3 = fitdist(x,'tlocationscale');
xx = linspace(0,30);
line(xx,pdf(p1,xx),'color','r')  % plot three densities
line(xx,pdf(p2,xx),'color','g')
line(xx,pdf(p3,xx),'color','m')
```

Locks on 29 Mar 2013

Thanks!

I have changed to code to:

```x = wblrnd(10,2,1000,1);
[heights,centers] = hist(x,40);
area = (centers(2)-centers(1))*sum(heights);
bar(centers,heights/area,'hist') % normalize histogram to integrate to 1
p1 = fitdist(x,'normal');        % fit three distributions
p2 = fitdist(x,'lognormal');
```
```xx = linspace(0,30);
line(xx,pdf(p1,xx),'color','r')  % plot three densities
line(xx,pdf(p2,xx),'color','g')
```

Because I only need the normal and the log normal distribution. Is there a way I can plot only those tho curves without the blue bars?

Tom Lane on 1 Apr 2013

You can omit lines 2-4 (hist/area/bar) if you don't want the densities superimposed on a histogram.

Locks on 1 Apr 2013

thanks!