View
5
Download
0
Category
Preview:
Citation preview
Scientific Computing with MATLAB2. Graphics for Research & Publication
Dra. K.-Y. Daisy FanDepartment of Computer ScienceCornell UniversityCornell UniversityIthaca, NY, USA
UNAM IIM 2012
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
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;
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
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
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
% 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
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
Color Options
White ‘w’Black ‘k’Black ‘k’Red ‘r’BlBlue ‘b’Green ‘g’Yellow ‘y’Magenta ‘m’gCyan ‘c’
12
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
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
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
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
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
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
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
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
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
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
( )
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
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
Demonstrate commonly used2-d graphics
27
Files for tomorrow:
ll d / df /www.cs.cornell.edu/~dfan/UNAM_IIM2012/popMexicoByStates.xlsx
A JPEG photo file, color or grayscale
39
Recommended