480 likes | 679 Views
Lecture (4). Plotting & Programming (1) Eng. Osama Talaat. Plotting. 0. t. 1. 2. 3. 4. 5. 5.18. s. -. 6.08. 24.97. 36. 54.98. 66.07. Plotting. 0. t. 1. 2. 3. 4. 5. 5.18. s. -. 6.08. 24.97. 36. 54.98. 66.07. Plotting : Line Colors. >> plot( t,s,'r ') Colors:
E N D
Lecture (4) Plotting & Programming (1) Eng. Osama Talaat
Plotting 0 t 1 2 3 4 5 5.18 s - 6.08 24.97 36 54.98 66.07
Plotting 0 t 1 2 3 4 5 5.18 s - 6.08 24.97 36 54.98 66.07
Plotting: Line Colors >> plot(t,s,'r') Colors: • 'r': red • 'g': green • 'y': yellow • 'b': blue • 'w': white • 'k': black
Plotting: Line Style >> plot(t,s,'--') • '-': Solid • '--': Dashed • ':': Dotted • '-.': Dash-Dot • '+': Plus signs • '*': Asterisks • 'x': Crosses • 's': Squares • 'd': Diamonds
Plotting: Line Style & Color • Red Dashed line: >> plot(t,s,'r--') >> plot(t,s,'--r')
Plotting Titles • X-Axis Title: >> xlabel('time')
Plotting Titles • Y-Axis Title: >> ylabel('Speed')
Plotting Titles • Graph Title: >> title('Speed Curve')
Plotting Grid >> grid
Plotting: Texts • Using Coordinates: >> text(0.6,-2,'Point(0.5,0)')
Plotting: Texts • Using Mouse: >> gtext('Point(0.5,0)')
Plotting: Axis Limits • X-Axis Limits: >> xlim([0.5 4])
Plotting: Axis Limits • Y-Axis Limits: >> ylim([-20 50])
Plotting: Figure window Click Here
Plotting in the same figure • Close all windows. • Plot sin(x) >> x=0:0.1:4*pi; >> plot(x,sin(x)) • Plot 2cos(x) >> plot(x,2*cos(x)) • ???
Plotting in the same figure >> x=0:0.1:4*pi; >> y1=sin(x); >> y2=2*cos(x); >> plot(x,y1,x,y2) >> plot(x,y1,'r',x,y2,'--b')
Plotting in the same figure >> plot(x,y1,'r') >> hold on >> plot(x,y2,'--b') >> hold off
Plotting in the same figure: Legend >> legend('sin(x)','2cos(x)')
Plotting in separate figures >> plot(x,y1,'r') >> figure >> plot(x,y2,'--b')
Test yourself !! >> close all • Insert each graph titles and grid ?? >> plot(x,y1,'r') >> xlabel('x') >> ylabel('y1') >> title('sin(x)') >> grid >> figure >> plot(x,y2,'--b') >> xlabel('x') >> ylabel('y2') >> title('2cos(x)') >> grid
Subplotting >> subplot(2,2,1)
Subplotting >> plot(x,sin(x))
Subplotting >> title('sin(x)'); grid
Subplotting >> subplot(2,2,2)
Subplotting >> plot(x,cos(x)); title('cos(x)'); grid
Subplotting >> subplot(2,2,3) >> plot(x,sin(2*x)) >> title('sin(2x)') >> grid >> subplot(2,2,4) >> plot(x,cos(2*x)) >> title('cos(2x)') >> grid
NB: Variable Plotting • 1 2 3 4 5 6 x = [2 3 4 12 15 14] >> x = [2 3 4 12 15 14]; >> plot(x)
Other Plotting Functions >> area(x,sin(x))
Other Plotting Functions >> stairs(x,sin(x))
Other Plotting Functions >> bar([2001:2006],[13 15 17 20 6 12])
Other Plotting Functions >> pie([15 23 57 5])
Other Plotting Functions >> pie3([15 23 57 5])
Other Plotting Functions • Plotting an equation >> ezplot('(x^2+y^2-1)^3-x^2*y^3=0') >> xlim([-1.5 1.5]) >> ylim([-1.5 1.5])
Polar Coordinates >> th=0:0.01:2*pi; >> r=sin(4*th); >> polar(th,r)
3D Plotting: Curve >> t=0:0.1:10*pi; plot3(sin(t),cos(t),t); grid
Test Yourself !! • Insert Z-Axis Label: >> zlabel('height')
3D Plotting: Surface >> [x,y]=meshgrid(-2:0.1:2,-2:0.1:2); >> z=x.*exp(-x.^2-y.^2); >> surf(x,y,z)
3D Plotting: Surface >> mesh(x,y,z)
Special Surfaces • Sphere >> sphere
Special Surfaces • Cylinder >> cylinder
Special Surfaces • MATLAB Logo >> logo
Introduction to Programming • Create New Program: • Ctrl + N • New button • File menu >> New >> script
x=[0:0.1:2*pi]; y=sin(x); plot(x,y) xlabel('x') ylabel('sin(x)') title('Sine Curve') grid !!')
Clean Start clear all; close all; clc; x=[0:0.1:2*pi]; y=sin(x); plot(x,y) xlabel('x') ylabel('sin(x)') title('Sine Curve') grid
GOOD LUCK To be continued in the next lecture …