2. Outline:
Matlab graphics and plotting
Plotting Process
Graph Components
Figure Tools
Arranging Graphs Within a Figure
Choosing a Type of Graph to Plot
Using of M-File
Sub-Plots (different diagrams)
3D-plot
2
3. Plot command
plot( ) Command creates a graphic from vectors, with
linear scales on two or three axes
plot3(X,Y,Z, 'option') %3-D plot.
plot(X1,Y1, . . . 'option') %2-D plot , one curve
plot(X,Y1, X, Y2, X, Y3, . . . 'option')
%2-D plot, Multi-curves to the same y-axis
Option is Line styles (Type, markers and color)
3
4. Plot command (line style)
Line style include
Line type,
Marker symbol, and
color
Example
>> plot(x,exp(-x),'g--s') 4
5. Class activity
Create an x-array samples with step 0.01.
Calculate sin x of the x-array
Calculate sin(x) of the x-array
>>x= 0 : 0.01:4*pi;
>>y=sin(x);
>>plot (x,y, ‘r -. *')
5
Plot the function sin(x) between 0 ≤ x ≤ 4π
8. Linspace command-01
linspace: This function generates a vector of uniformly
incremented values (i.e. Linearly spaced vector).
This function Syntax has the form:
linspace (start , end [ , number ] )
The default value if number not specified is 100
The increment is computed internally, having the value:
Example: linspace (0, π,11)
8
)
1
(
number
start
end
increment
1
.
0
)
1
11
(
0
increment
9. Linspace command-02
y = linspace(a,b) generates a row vector y
of 100 points linearly spaced between and
including a and b.
y = linspace(a,b,n) generates a row vector
y of n points linearly spaced between and
including a and b.
For n < 2, LINSPACE returns b.
(Ex.) >> linspace(0,4,2)
ans =
0 4 9
10. 10
>> linspace(1,5,4)
ans =
1.0000 2.3333 3.6667 5.0000
>> linspace(0,6,3)
ans =
0 3 6
>> linspace(3,9,4)
ans =
3 5 7 9
>> linspace(3,9,2)
ans =
3 9
11. Class activity
>>x=linspace(0 , 4*pi);
>>y=sin(x);
>>plot(x,y, ‘b :')
11
2- Calculate sin() of the x-array
Plot the y-array
1- Create an x-array of 100 samples with step 4π.
Plot the function sin(x) between 0 ≤ x ≤ 4π of 100
linearly equal spaced points
0 2 4 6 8 10 12 14
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
12. Class activity- M-file
% program indicating plot options
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,’--rs’,'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
12
15. Display Facilities - GUI
title(‘text strings’)
xlabel(‘text strings’)
ylabel(‘text strings’)
>>title( ' This is the sinus function' )
>>xlabel( ‘ x (secs) ')
>>ylabel( ‘ sin(x) ')
0 10 20 30 40 50 60 70 80 90 100
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
This is the sinus function
x (secs)
sin(x)
15
16. Matlab Graphics- Class activity
x = 0 : pi/100 : 2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2π ')
ylabel('Sine of x')
title('Plot of the Sine
Function')
16
17. Displaying text on diagram -01
To place places 'text‘ on the specific coordinates x
and y text(x,y,' text ')
17
Example
>> text(5,10, ' logarithmic function ' )
>> text (10,20, ' theta gama ' )
To place text with help of the mouse on a figure use
gtext(string) ≡ gtext( ' text ' )
18. Displaying text on diagram -02
t = 0:.01:2*pi;
plot (t,sin(t), '.- r')
xlabel(' theta in radians ' )
ylabel(' sin (theta) ' )
title ( ' plotting the area under a curve ' )
gtext ( ' linear function' )
grid
18
19. Displaying text on diagram -03
19
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
in radians
sin
(
theta)
plotting the area under a curve
linear function
21. Display Facilities (bar chart)
The form is bar(x_value, Y_value)
21
clf
x = 1:5;
y1 = [2 11 6 9 3];
bar(x,y1)
1 2 3 4 5
0
2
4
6
8
10
12
22. Display Facilities (bar vs. plot)
a bar chart.
22
clf
x = 1:5;
y1 = [2 11 6 9 3];
figure(1) % Put a bar chart in Figure 1
bar(x,y1)
figure(2) % Put plots on one plot with a legend
plot(x,y1, 'k') ,hold on
y2 = [4 5 8 6 2]; % better to written after y1
plot(x,y2, 'ro') ,grid on
legend('y1' , 'y2')
24. Adding additional plots to a
figure
24
•HOLD ON holds the current
plot
•HOLD OFF releases hold on
current plot
•HOLD toggles the hold state
x = 0 : 0.1 : 2*pi;
y = sin(x);
plot(x,y,' b ')
grid on;
hold on;
plot(x,exp(-x), ' r : * ');
25. Multiple Graphs (same
diagram)
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t + pi/2);
plot(t,y1,t,y2)
grid on
25
plot(X,Y1,X,Y2)
plots Y1 versus X and plots Y2 versus X with y-axis
labeling on the left for both curves.
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1