0% found this document useful (0 votes)
9 views12 pages

OPM Chapter 4 Graph

Uploaded by

kaouthermounir6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

OPM Chapter 4 Graph

Uploaded by

kaouthermounir6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1

Ministry of Higher Education and Scientific Research University


of Algiers 1
Faculty of Sciences, Department of Mathematics

1st year Mathematics

Programming tools (MATLAB)

Prepared by:

Dr KHETTABI Imen

Associate Professor B Department of Mathematics Faculty


of Sciences, University of Algiers.

Year: 2024-2025
2

Chapter 4: Graphing with Matlab


In MATLAB, plotting data is a fundamental way to visualize and analyze
information. MATLAB provides a variety of functions for creating different types
of plots, such as line plots, bar charts, histograms, and scatter plots. To create a
basic plot, you simply need to call the plot( ) function, providing the data you
want to display.

4.1 plot( )
The basic procedure for creating a 2D graph in MATLAB involves using a vector
of x-coordinates, 𝑥 = (𝑥1 , . . . , 𝑥𝑛 ), and a vector of y-coordinates, 𝑦 =
(𝑦1 , … , 𝑦𝑛 ).

The points (𝑥𝑖 , 𝑦𝑖 ) for i=1,...,n are then plotted and connected by straight lines.
To do this, x and y must be prepared in the same array format, either as row
arrays or column arrays of equal length.

For example: The vectors x = (1, 2, 3, 4, 5, 6, 7, 8) and y = (6, 3, 1, 8, 0, -1, 1)


produce the following Figure:
3
4
Note:
If it contains a single matrix as argument: it considers the values of each column
as the elements of the y axis, and the row number as the values of the x axis. So,
it will give several curves (one for each column).
For example:
>> M = [0, -2, 1; 2, 3, 4; -3, 3, 0; 1, 1, 4];
>> plot(M)

4.2 fplot( )
The fplot command allows you to plot the graph of a function over a given
interval. The syntax is :
fplot(′nomfunction ′, [xmin , xmax])

Where:
- nomfunction: is the name of a declared matlab function of the variable x,
i.e. the name of a user function.
- [xmin, xmax]: is the interval for which the graph of the function is plotted.
For example:
5

4.3 Adding titles, labels, legends to the graph and Grid

Commande Description
Set a title for the plot, displayed above
title('title')
the graph area

Display a label for the x-axis


xlabel('title_X') (horizontal axis)
6
Display a label for the y-axis
ylabel('title_Y') (vertical axis)

legend('title1', 'title2', ...) Set a legend for each curve in the graph
grid on Set grid in the graph
For example:

4.4 Defining Line Styles and Colors


A third parameter can be added to the plot function to specify the style of the
curve:
✓ The color of the curve (red, blue, black, white, yellow, green, etc.).
✓ The marker (representation of coordinates, points, triangles, squares, etc.).
✓ The line type (solid, dashed).

By using the general plot command:


plot(x, y,′ color marker style′)

The first character ‘colors’

Color MATLAB Color Symbol


Red r

White w

Black k
7
Blue b
Green g
Yellow y
Violet (Magenta) m
Cyan (Sky Blue) c

Note: To see all possibilities, type help plot.

The second character ‘marker’

Marker MATLAB Marker Symbol


Point .
Circle o

Cross 

Plus +
Stars *
Squares s
Diamonds d
Triangles (left and right) > <

The third character ‘style’

Style MATLAB Line style Symbol


Solid -
Dashed --

Dotted :

Dash-dot -.
For example:
8

4.5Multiple data sets in one plot


Method 1:
In MATLAB, it's very simple to plot multiple sets of data in a single graph.
We use the command ‘hold on’
For example:
9

Method 2:
We can display multiple sets of data in a single graph (i.e. in the same
figure) by calling them up in the same plot function.
For example:
10
We can also do the plotting in different plots using the ”figure()”
command.
For example:

4.6 Multiple plots in one Figure Window


The subplot command can be used to display a number of different
plots in a single Figure Window.
The subplot command takes three arguments that determine the
number and location of plots in the Figure. Typing ”subplot(m,n,p)”
partitions the figure window into an m-by-n matrix of small subplots
and selects the pth subplot for the current plot. The plots are numbered
along the first row of the figure window, then the second row, and so
on.
11

For example:

x= linspace(0,4*pi,100);
y1=sin(pi*x/4) ;
y2=cos(pi*x/4);
y3=5*sin(pi*(x-1))+1;
y4=5*cos(2*(x-pi/2))+0.25;
12
subplot(2,2,1);
plot(x, y1)
title('sin(pi*x/4)')
subplot(2,2,2);
plot(x, y2)
title('cos(pi*x/4)')
subplot(2,2,3); plot(x, y3)
title('5*sin(pi*(x-1))+1')
subplot(2,2,4);
plot(x, y4)
title('5*cos(2*(x-pi/2))+0.25')

4.6 Save Figure


A figure can be saved in several formats : in a format specific to
matlab with the .fig extension (for recent versions from Matlab).
To do this, click on the Save as command in the File menu of the
window graphic and enter a file name with the .fig extension in
the box that appears. Such a file can be viewed using the Open
command from the File menu.

You might also like