scientific computing with matlab 2. graphics for …dfan/unam_iim2012/m2graphics.pdfscientific...
TRANSCRIPT
![Page 1: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/1.jpg)
Scientific Computing with MATLAB2. Graphics for Research & Publication
Dra. K.-Y. Daisy FanDepartment of Computer ScienceCornell UniversityCornell UniversityIthaca, NY, USA
UNAM IIM 2012
![Page 2: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/2.jpg)
Monte Carlo Approximation of
Sq area N L L
Throw N darts
LL/2
Sq. area = N = L L
Circle area = Ninin
= L2/4
4 N / N = 4 Nin / N
3
![Page 3: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/3.jpg)
Monte Carlo with N darts on L-by-L board
N= ___; L= ___; hits= 0;for k = 1:Nfor k = 1:N
% Throw kth dartx = rand(1)*L – L/2;x = rand(1) L L/2;y = rand(1)*L – L/2;% Count it if it is in the circle% Count it if it is in the circleif sqrt(x^2+y^2) <= L/2
hits = hits + 1;;end
end
4
myPi = 4*hits/N;
![Page 4: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/4.jpg)
2-d array: matrix c
An array is a named collection of like data organized into
r
An array is a named collection of like data organized into rows and columns
A 2-d array is a table, called a matrixy , Two indices identify the position of a value in a matrix,
e.g.,gmat(r,c)
refers to component in row r, column c of matrix matp , Array index starts at 1 Rectangular: all rows have the same #of columns
5
Rectangular: all rows have the same #of columns
![Page 5: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/5.jpg)
Creating a matrix
Built-in functions: ones, zeros, rand E.g., zeros(2,3) gives a 2-by-3 matrix of 0sg , ( , ) g y
“Build” a matrix using square brackets, [ ], but the dimension must match up:p [x y] puts y to the right of x [x; y] puts y below x
4 0 3[ y] p y
[4 0 3; 5 1 9] creates the matrix [4 0 3; ones(1,3)] gives
4 0 3
5 1 9
4 0 3 [4 0 3; ones(3,1)] doesn’t work
4 0 3
1 1 1
6
![Page 6: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/6.jpg)
What will A be?
A= [0 0]A= [A’ ones(2,1)]A [0 0 0 0 A A]A= [0 0 0 0; A A]
7
![Page 7: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/7.jpg)
% Given an nr-by-nc matrix M.% What is A? for r= 1: nr
for c= 1: ncA(c,r)= M(r,c);
endend
9
![Page 8: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/8.jpg)
X-Y Plot
The basic command isplot(a b)plot(a,b)
which draws a line connecting the points ( (1) b(1)) ( (2) b(2)) ( ( ) b( )) (a(1),b(1)), (a(2),b(2)), … , (a(n),b(n)), where n is the length of vectors a and b.
Can add format specifications. E.g.,p gplot(a,b,’*r’)
draws red asterisks (*) at the data points instead of draws red asterisks (*) at the data points instead of using a line
11
![Page 9: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/9.jpg)
Color Options
White ‘w’Black ‘k’Black ‘k’Red ‘r’BlBlue ‘b’Green ‘g’Yellow ‘y’Magenta ‘m’gCyan ‘c’
12
![Page 10: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/10.jpg)
Making an x-y plot
a= [0 4 3 8]; % x-coordsb= [1 2 5 3]; % y-coordsb= [1 2 5 3]; % y-coordsplot(a, b, ‘-*’)
/ kx-values(a vector)
y-values(a vector)
Line/markerformat
6(a ecto )
4
5
2
3
0 2 4 6 8 100
1
![Page 11: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/11.jpg)
Making a figure with plot
figure % start a new graphics window
hold on % hold subsequent plot commands in current axeshold on % hold subsequent plot commands in current axes
plot(a,b)plot(c,d)
hold off % subsequent plot command refreshes axes--q p% erase previous items
close % close graphics windowclose % close graphics window
14
![Page 12: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/12.jpg)
Trajectory of a projectile – golf ball
Without air resistance (drag), the trajectory of a (drag), the trajectory of a golf ball in flight is a parabola. What is the parabola. What is the effect of drag?
Relevant equations for Relevant equations for simulating the golf ball in flight:flight:
15
![Page 13: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/13.jpg)
Trajectory of a projectile – golf ball
Without air resistance (drag), the trajectory of a (drag), the trajectory of a golf ball in flight is a parabola. What is the parabola. What is the effect of drag?
Relevant equations for Relevant equations for simulating the golf ball in flight:flight:
16
![Page 14: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/14.jpg)
Trajectory of a projectile – golf ball
Without air resistance (drag), the trajectory of a (drag), the trajectory of a golf ball in flight is a parabola. What is the parabola. What is the effect of drag?
Relevant equations for Relevant equations for simulating the golf ball in flight:flight:
17
![Page 15: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/15.jpg)
Simulate a golf ball in flight
Simulation begins at t = 0 and position (0,0) Simulation ends when the ball lands (y ≈ 0) or Simulation ends when the ball lands (y ≈ 0) or
when maximum simulation time is reachedPl h j Plot the trajectory
See the given file golfBall.m for the skeleton program, including given constant and parameters
18
![Page 16: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/16.jpg)
Simulate a golf ball in flight
Simulation begins at t = 0 and position (0,0) Simulation ends when the ball lands (y ≈ 0) or Simulation ends when the ball lands (y ≈ 0) or
when maximum simulation time is reachedPl h j Plot the trajectory
See the given file golfBall.m for the skeleton program, including given constant and parameters
19
![Page 17: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/17.jpg)
Simulate flight for different friction coefficient values
Modify golfBall to show the trajectories in one Modify golfBall to show the trajectories in one plot for different values of the friction coefficient:
k = 0, .02, … , .08
21
![Page 18: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/18.jpg)
Some format commands to use with plot
close all % close all graphics windowsaxis equal % same scaling for x, y axesaxis off % hide axesaxis off % hide axesaxis on % show axes
xlabel(‘text for labeling x-axis’)ylable(‘text for labeling y-axis’)title(‘text for plot title at top center’)( p p )
hold on % hold subsequent plot commands in current axeshold off % subsequent plot command refreshes axeshold off % subsequent plot command refreshes axes--
% erase previous items
22
![Page 19: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/19.jpg)
Draw a single line segment
a= 0; % x-coord of pt 1b= 1; % y-coord of pt 1b= 1; % y-coord of pt 1c= 5; % x-coord of pt 2d= 3; % y-coord of pt 2plot([a c], [b d], ‘-*’)
Line/markerformat
x-values(a vector)
y-values(a vector)
format
23
( )
![Page 20: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/20.jpg)
Making an x-y plot with multiple graphs (lines)My graphs
4 5
5
5.5
6My graphs
graph 1 namegraph 2 name
a= [0 4 5 8]; b= [1 2 5 3]; 3
3.5
4
4.5
y va
lues
[ ];f= [0 4 6 8 10];g= [2 2 6 4 3]; 1.5
2
2.5
plot(a,b,'-*',f,g,'c')legend('graph 1 name', 'graph 2 name')
0 1 2 3 4 5 6 7 8 9 101
x values
xlabel('x values')ylabel('y values')title('My graphs' 'Fontsize' 14)
25
title('My graphs', 'Fontsize',14)
See also plotComparison.m
![Page 21: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/21.jpg)
Simulate flight for different friction coefficient values
Modify golfBall to show the trajectories in one Modify golfBall to show the trajectories in one plot for different values of the friction coefficient:
k = 0, .02, … , .08
Note: Can create or lengthen a vector, e.g.,Note: Can create or lengthen a vector, e.g.,x = 3.1; % x is scalar 3.1x(4) = 2; % x is vector [3 1 0 0 2]x(4) = 2; % x is vector [3.1 0 0 2]
26
![Page 22: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/22.jpg)
Demonstrate commonly used2-d graphics
27
![Page 23: Scientific Computing with MATLAB 2. Graphics for …dfan/UNAM_IIM2012/M2graphics.pdfScientific Computing with MATLAB 2. Graphics for Research & Publication Dra. K.-Y. Daisy Fan Department](https://reader030.vdocuments.us/reader030/viewer/2022041110/5f1050bc7e708231d448806d/html5/thumbnails/23.jpg)
Files for tomorrow:
ll d / df /www.cs.cornell.edu/~dfan/UNAM_IIM2012/popMexicoByStates.xlsx
A JPEG photo file, color or grayscale
39