Lec6 PDF
Lec6 PDF
Lec6 PDF
(CSC209)
Lecture (6)
Introduction to basic Plot
Dr.Omar Almutairi
Basic Plot
Creating simple plots
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
2
Basic Plot
x = 0:1:10;
y = x.^2 - 10.*x + 15;
plot(x,y);
15
10
-5
-10
0 1 2 3 4 5 6 7 8 9 10
3
Print, Save and Exporting a plot
15
10
-5
-10
0 1 2 3 4 5 6 7 8 9 10
4
Adding title, axis label and
Examples annotations
>> x = 0:1:10;
y = x.^2 - 10.*x + 15;
plot(x,y);
title ('Plot of y = x.^2 - 10.*x + 15');
Plot of y = x. 2 - 10.*x + 15
ylabel ('Good'); 10
grid on; 5
Good
-5
-10 5
0 1 2 3 4 5 6 7 8 9 10
very good
Saving and print figure options
6
Saving and print figure options
close all
x = [2 4 7 2 4 5 2 5 1 4];
y = 0.5*cos(x);
plot(x,y)
xlabel('xlabel')
ylabel('ylabel')
title('Saving and print figure options')
saveas(gcf,'Barchart','tiff') Saving and print figure options
0.4
a=figure(1) 0.3
saveas(a,‘Omar.png') 0.2
0.1
ylabel
0
-0.1
-0.2
-0.3
-0.4
1 2 3 4 5 7
6 7
xlabel
Multiple Plots
>> x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
2
plot(x,y1,x,y2); 1.5
0.5
-0.5
-1
-1.5
-2
0 1 2 3 4 5 6 7
8
Line Color, Line Style, Marker Style, and
Legends
x=0:pi/100:2*pi; y1=2*cos(x);y2 = cos(x);
y3 = 0.5*cos(x);
plot(x,y1,'--',x,y2,'-',x,y3,':')
xlabel('0 \leq x \leq 2\pi')
ylabel('Cosine functions')
legend('y1','y2','y3')
title('Typical example of multiple plots')
axis([0 2*pi -3 3])
9
Resistor (Ohms) vs Power (w)
volts = 120;
rs = 50;
rl = 1:1:100;
amps = volts ./ ( rs +
rl );
pl = (amps .^ 2) .* rl;
plot(rl,pl);
title('Plot of power
versus load
resistance');
xlabel('Load resistance
(ohms)');
ylabel('Power (watts)');
grid on;
10
Table of Plot Colors, Marker Styles, and Line
Styles
11
Table of Plot Colors, Marker Styles,
and Line Styles
close all
x = -pi:pi/10:pi;
y1 = tan(sin(x))-
sin(tan(x));y2=cos(sin(x))-cos(tan(x));
plot(x,y1,'--
ro','LineWidth',2,'MarkerEdgeColor','k',...
'MarkerFaceColor','g','MarkerSize',10)
hold on
plot(x,y2,'--
rs','LineWidth',2,'MarkerEdgeColor','r',...
'MarkerFaceColor','b','MarkerSize',10)
xlabel('x lable');ylabel('y 3
Plot Colors, Marker Styles,and Line Styles
y1
label');legend('y1','y2'); 2
y2
y label
0
-1
-2
-3
12
-4 -3 -2 -1 0 1 2 3 4
x lable
In class Assignment
1. Plot the relation between y & x. suppose x=0 : 0.1 : 2*pi
14