OPM Chapter 4 Graph
OPM Chapter 4 Graph
Prepared by:
Dr KHETTABI Imen
Year: 2024-2025
2
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.
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
Commande Description
Set a title for the plot, displayed above
title('title')
the graph area
legend('title1', 'title2', ...) Set a legend for each curve in the graph
grid on Set grid in the graph
For example:
White w
Black k
7
Blue b
Green g
Yellow y
Violet (Magenta) m
Cyan (Sky Blue) c
Cross
Plus +
Stars *
Squares s
Diamonds d
Triangles (left and right) > <
Dotted :
Dash-dot -.
For example:
8
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:
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')