Matlab Matrices
Matlab Matrices
“Backslash”
operator
Ellipses is used in
Matlab to indicate
a continuation statement
Steel beam
E = 200 GPa
(200 e9 N/m2)
»c=ones(1,3)
c=
1 1 1
CPU Timea
Procedure Ratiob
(seconds)
Standard for-loop 5.29820 1.00
Array 1.39500 1.54
Pre allocation
Vectorization 0.90465 5.85
x=0:0.05:5;
y=exp(-x.^2);
semilogy(x,y); grid
10 0
10 -2
10 -4
10 -6
10 -8
10 -10
10 -12
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x=0:0.05:5;
y=exp(-x.^2);
loglog(x,y); grid
10 0
10 -1 -3
10 10 -2 10 -1 10 0
x = -2.9:0.2:2.9;
bar(x,exp(-x.*x));
grid
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-3 -2 -1 0 1 2 3
x=0:0.05:8;
stairs(x,sin(x.^2).*exp(-x));
grid
0.35
0.3
0.25
0.2
0.15
0.1
0.05
-0.05
-0.1
-0.15
0 1 2 3 4 5 6 7 8
x=-2:0.1:2;
y=erf(x);
e = rand(size(x))/2; errorbar(x,y,e); grid
1.5
0.5
-0.5
-1
-1.5
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
% Polar plot
t=0:.01:2*pi;
polar(t,sin(2*t).*cos(2*t));
90
0.5
120 60
0.375
150 0.25 30
0.125
180 0
210 330
240 300
270
x = 0:0.1:4;
y = sin(x.^2).*exp(-x);
stem(x,y); grid
0.35
0.3
0.25
0.2
0.15
0.1
0.05
-0.05
-0.1
-0.15
0 0.5 1 1.5 2 2.5 3 3.5 4
x=randn(1,1000);
hist(x);
grid
300
250
200
150
100
50
0
-4 -3 -2 -1 0 1 2 3 4
x=-2:0.1:2; y1=erf(x);
y2=erf(1.35.*x);
plotyy(x,y,x,y2);grid
0.5 1
0 0
-0.5 -1
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
B777
14%
55%
B767
-1
-2
-3
-4
-4 -3 -2 -1 0 1 2 3 4
-2
-4
-6
3
2
3
1 2
0 1
-1 0
-1
-2 -2
y -3 -3
x
t=-3:.1:3;
[x,y]=meshgrid(t,t);
z= 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2);
colormap(lines)
contour(x,y,z,15) % 15 contours are generated
grid
-1
-2
-3
-3 -2 -1 0 1 2 3
x = 0:0.05:8;
y = sin(x.^2).*exp(-x);
comet(x,y)
Tampa Bay
Atlantic Ocean
Aircraft Tracks
Miami Intl. Airport
Gulf of México
Longitude (deg.)
Plot containing 830 flight tracks arriving or departing Miami Airport
Each track has 325 data points
Source: FAA, plot by A. Trani (Air Transportation Systems Lab)
Longitude (deg.)
Plot containing 830 flight tracks arriving or departing Miami Airport
Each track has 325 data points
Source: FAA, plot by A. Trani (Air Transportation Systems Lab)
Virginia Polytechnic Institute and State University 44
Sample Use of Subplot Function
0.4
0.2
-0.2
0 0.5 1 1.5 2 2.5 3 3.5 4
0.5
-0.5
-1
0 0.5 1 1.5 2 2.5 3 3.5 4
35
30
25
-130 -120 -110 -100 -90 -80 -70 -60
Longitude (deg)
10
Traffic (acft)
0
1100 1120 1140 1160 1180 1200 1220 1240 1260 1280 1300
Reference Time (min)
The ‘zoom’
31
command is used to examine a smaller area
Latitude (deg)
30
29
28
27
-90 -88 -86 -84 -82 -80 -78 -76
Longitude (deg)
10
Traffic (acft)
0
1100 1120 1140 1160 1180 1200 1220 1240 1260 1280 1300
Reference Time (min)
Plot containing 104 flight tracks crossing an airspace sector in Florida
Source: FAA, plot by A. Trani (Air Transportation Systems Lab)
Retrieve the data file called: traffic flow data from our syllabus web site
plot3(density,speed,volume,'*')
3500
3000
2500
Volume (veh/hr)
2000
1500
1000 TextEnd
500
0
80
60 150
40 100
20 50
0 0
Density (veh/la-km) Speed (km/hr)
3000
2500
Volume (veh/hr)
2000
1500
1000
TextEnd
500
0
80
60
40
20 140
120
100
80
Density (veh/la-km) 60
0 40
20
0
Speed (km/hr)
10
-5
-10
50
40 50
30 40
20 30
20
10 10
0 0
z=peaks(25);
surf(z);
colormap(jet); ;
8
-2
-4
-6
-8
25
20 25
15 20
10 15
10
5 5
0 0
z=peaks(25);
surfl(z);
shading interp; colormap(jet);;
8
-2
-4
-6
-8
25
20 25
15 20
10 15
10
5 5
0 0
[x,y,z] = meshgrid(-2:.2:2,-2:.2:2,-2:.2:2);
v = x .* exp(-x.^2 - y.^2 - z.^2);
slice(v,[5 15 21],21,[1 10])
axis([0 21 0 21 0 21]);
colormap(jet)
20
15
10
0
20
15 20
15
10
10
5
5
0 0
3500
3000
2500
Volume (veh/hr)
2000
1500
1000
TextEnd
500
0
80
60 150
40 100
20 50
0 0
Density (veh/la-km) Speed (km/hr)