Docsity

MATLAB: Plotting Functions of a Vector and Creating Graphs, Study notes of Computer Science

Partial preview of the text

Download MATLAB: Plotting Functions of a Vector and Creating Graphs and more Study notes Computer Science in PDF only on Docsity!

MA/CS 375

Spring 2009

Lecture1d

Introduction to MATLAB: Plotting

Ref: Appendix B

Building/plotting a function of a

vector

1) Create a vector x ,

with 50 equally

spaced entries from

0 to 1.

2) Create a vector f,

with entries of

cosine evaluated at

entries of 2pi x

3) Plot f as a function

of x , using +

symbols

Example

(Building and plotting two

functions of a vector)

1) Create a vector x , with 50
entries from 0.0 to 1.
2) Create a vector f, with
entries cos(2pi x ).
3) Create a vector g with entries
sin(2pi x )
4) Plot f as a function of x ,
using a blue line
5) On same graph plot g as a
function of x, using red +
symbols.
“hold on” instructs Matlab to keep the current graph window and its
contents open for another plot; “hold off” closes the figure for input.

Example

Adding a Title, Labeling Axes and

Legends

  • Give the graph a title

    title(„The very boring graphs');

  • Label the axis

    xlabel('x');

  • Label the y-axis

    ylabel('f,g');

  • Set up the legend box with descriptions of each

curve:

legend('f=cos(2pix)', 'g=sin(2pix)');

The labeling commands

Example

Defining Font Sizes/Types

subplot

  • If you wish to create a figure with two sub-figures you can use the subplot function: - subplot(1,2,1) specifies 1 row and 2 columns of plots and requests the first plot. - subplot(1,2,2) specifies 1 row and 2 columns of plots and requests the second plot. x = linspace(0,1,33); F = sin(pix); subplot(1,2,1), plot(x,F,'r+') xlabel('x'), ylabel('F(x)') G = cos(pix); subplot(1,2,2), plot(x,G,'b*') xlabel('x'), ylabel('G(x)')

subplot(1,2,1) subplot(1,2,2)

The Plot

A surface plot

A Contour Plot