<< Chapter < Page Chapter >> Page >
Plotting basics.

GraphingWithMATLAB

A picture is worth a thousand words, particularly visual representation of data in engineering is very useful. MATLAB has powerful graphics tools and there is a very helpful section devoted to graphics in MATLAB Help: Graphics. Students are encouraged to study that section; what follows is a brief summary of the main plotting features.

Two-dimensional plots

The plot Statement

Probably the most common method for creating a plot is by issuing plot(x, y) statement where function y is plotted against x.

Type in the following statement at the MATLAB prompt:

x=[-pi:.1:pi]; y=sin(x); plot(x,y);

After we executed the statement above, a plot named Figure1 is generated:

Plot
Graph of sin(x)

Having variables assigned in the Workspace, x and y=sin(x) in our case, we can also select x and y, and right click on the selected variables. This opens a menu from which we choose plot(x,y). See the figure below.

PlotFromWorkspace
Creating a plot from Workspace.

Annotating plots

Graphs without labels are incomplete and labeling elements such as plot title, labels for x and y axes, and legend should be included. Using up arrow, recall the statement above and add the annotation commands as shown below.

x=[-pi:.1:pi];y=sin(x);plot(x,y);title('Graph of y=sin(x)');xlabel('x');ylabel('sin(x)');grid on

Run the file and compare your result with the first one.

sinxLabels
Graph of sin(x) with Labels.

Type in the following at the MATLAB prompt and learn additional commands to annotate plots: help gtext help legendhelp zlabel

Superimposed plots

If you want to merge data from two graphs, rather than create a new graph from scratch, you can superimpose the two using a simple trick:

% This script generates sin(x) and cos(x) plot on the same graph % initialize variablesx=[-pi:.1:pi]; %create a row vector from -pi to +pi with .1 incrementsy0=sin(x); %calculate sine value for each x y1=cos(x); %calculate cosine value for each x% Plot sin(x) and cos(x) on the same graph plot(x,y0,x,y1);title('Graph of sin(x) and cos(x)'); %Title of graph xlabel('x'); %Label of x axisylabel('sin(x), cos(x)'); %Label of y axis legend('sin(x)','cos(x)'); %Insert legend in the same order as y0 and y1 calculatedgrid on %Graph grid is turned

sinxLabelsLegend
Graph of sin(x) and cos(x) in the same plot with labels and legend.

Multiple plots in a figure

Multiple plots in a single figure can be generated with subplot in the Command Window. However, this time we will use the built-in Plot Tools. Before we initialize that tool set, let us create the necessary variables using the following script:

% This script generates sin(x) and cos(x) variables clc %Clears command windowclear all %Clears the variable space close all %Closes all figuresX1=[-2*pi:.1:2*pi]; %Creates a row vector from -2*pi to 2*pi with .1 incrementsY1=sin(X1); %Calculates sine value for each x Y2=cos(X1); %Calculates cosine value for each xY3=Y1+Y2; %Calculates sin(x)+cos(x) Y4=Y1-Y2; %Calculates sin(x)-cos(x)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A brief introduction to engineering computation with matlab. OpenStax CNX. Nov 17, 2015 Download for free at http://legacy.cnx.org/content/col11371/1.11
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?

Ask