



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Previous Lecture: Probability and random numbers 1-d array—vector
Today’s Lecture: More examples on vectors More MATLAB graphics Simulation
Announcement: P3 due 10/9 (Thurs) at 6pm P3: what is the result of rem(x,y) when x is negative?
February 26, 2009 Lecture 12 2
February 26, 2009 Lecture 12 3 February 26, 2009 Lecture 12 5
x-values (a vector)
y-values (a vector)
Line/marker format
February 26, 2009 Lecture 12 6
% Draw a rectangle with the lower-left
% corner at (a,b), width w, height h.
x= [a a+w a+w a a ]; % x data
y= [b b b+h b+h b ]; % y data
plot(x, y)
Fill in the missing vector values!
February 26, 2009 Lecture 12 11
February 26, 2009 Lecture 12 12
Assume some kind of granularity—discretize the color value range for red and blue Assume no contribution from green (set to 0)
Compute the color first; worry about drawing later Decide on granularity, say, ∆=.
February 26, 2009 Lecture 12 14
% All combinations of R and B
gran= 0.25; %granularity For all red values
Set color vector
February 26, 2009 Lecture 12 21
Things to consider/try on the color computation problem
February 26, 2009 Lecture 12 22
% given a<b, % given integer n> delta= (b-a)/n; for k= 1:n v(k)= a+k*delta; end
% given a<b, % given integer n> v= linspace(a,b,n);
February 26, 2009 Lecture 12 23
Loop through all the stars; each has equal likelihood of being bright or dark Repeat many times
% No. of stars and star radius N=10; r=.5; % Get mouse clicks, store coords In vectors x,y [x,y] = ginput(N); % Twinkle! for k= 1:50 % 50 rounds of twinkling
end
February 26, 2009 Lecture 12 45
−3 −3 −2 −1 0 1 2 3
−
−
0
1
2
3
Shrink (enlarge) the polygon so that the vertex furthest from the (0,0) is on the unit circle
February 26, 2009 Lecture 12 46
function [xNew,yNew] = Normalize(x,y) % Resize polygon defined by vectors x,y % such that distance of the vertex % furthest from origin is 1
d = max(sqrt(x.^2 + y.^2)); xNew = x/d; yNew = y/d;
Applied to a vector, max returns the largest value in the vector
Vectorized ops
February 26, 2009 Lecture 12 47
−1.5 −1 −0.5 0 0.5 1
−
−0.
−0.
−0.
−0.
0
1
Obtain a new polygon by connecting the midpoints of the edges
February 26, 2009 Lecture 12 49
function [xNew,yNew] = Smooth(x,y) % Smooth polygon defined by vectors x,y % by connecting the midpoints of % adjacent edges
n = length(x); xNew = zeros(n,1); yNew = zeros(n,1);
for i=1:n Compute the midpt of ith edge. Store in xNew(i) and yNew(i) end
February 26, 2009 Lecture 12 50
xNew(1) = (x(1)+x(2))/ yNew(1) = (y(1)+y(2))/
(x 4 ,y 4 )
(x 1 ,y 1 )
(x 3 ,y 3 )
(x 2 ,y 2 )
(x 5 ,y 5 )
February 26, 2009 Lecture 12 54
xNew(5) = (x(5)+x(1))/ yNew(5) = (y(5)+y(1))/
(x 4 ,y 4 )
(x 1 ,y 1 )
(x 3 ,y 3 )
(x 2 ,y 2 )
(x 5 ,y 5 )
February 26, 2009 Lecture 12 55
February 26, 2009 Lecture 12 58
February 26, 2009 Lecture 12 59
x= zeros(1,1); for k= 1: x= [x x]; end y= x(7);