MATLAB Lesson 5 - Plotting

Special plots

MATLAB has a wide variety of plotting commands enabling you to also create polar plots, histograms and many more.

Polar plots

When information is given in polar coordinates, the command

polar(theta, r)

is used to make a plot of the angle theta in radians versus the radius r.

Plot the curve with r = 1 + cos(theta) for theta in [0, 2 pi].

Create a vector theta of 200 equally spaced angles on [0, 2 pi].

Create the corresponding vector r.

Plot the points, producing the figure below.

>>  theta = linspace(0, 2*pi, 200);
>>  r = 1 + cos(theta);
>>  polar(theta, r)


Polar plot

 

Histograms

A basic tool for looking at a (large) number of values is to create a histogram, which counts the number of values that occur in each sub-interval or bin. The MATLAB command

hist(y, m)

draws a histogram of the values in the vector y using m bins.
Create a vector of 1000 samples from the standard normal distribution using randn and create a histogram with 20 bins.

Create the vector of 1000 samples.

Create a histogram with 20 bins.

>>  y = randn(1,1000);
>>  hist(y, 20)

This plot also has had a title and grid added. How would you do that?

Histogram with 20 bins

 

Warnings

Self-test Exercise

Create a polar plot of the curve r = sin(3 theta).

Answer:
theta = linspace(-pi, pi, 200);
r = sin(3*theta);
polar(theta, r)
Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

Polar plots are created using the polar command.

Histograms are created using the hist command.