Lecture Slides on Vectors, MATLAB Graphics and Simulation | CS 1112, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-84c
koofers-user-84c 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 12 2009/2/26
Lecture slides 1
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
Example
Write a program fragment that calculates the
cumulative sums of a given vector v.
The cumulative sums should be stored in a
vector of the same length as v.
1, 3, 5, 0 v
1, 4, 9, 9 cumulative sums of v
February 26, 2009 Lecture 12 3 February 26, 2009 Lecture 12 5
Drawing a single line segment
a= 0; % x-coord of pt 1
b= 1; % y-coord of pt 1
c= 5; % x-coord of pt 2
d= 3; % y-coord of pt 2
plot([a c], [b d], ‘-*’)
x-values
(a vector) y-values
(a vector)
Line/marker
format
February 26, 2009 Lecture 12 6
Drawing a polygon (multiple line segments)
% 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
Color is a 3-vector, sometimes called the RGB
values
Any color is a mix of red, green, and blue
Example:
c= [0.4 0.6 0]
Each component is a real value in [0,1]
[0 0 0] is black
[1 1 1] is white
pf3
pf4
pf5

Partial preview of the text

Download Lecture Slides on Vectors, MATLAB Graphics and Simulation | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

„ 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

Example

„ Write a program fragment that calculates the

cumulative sums of a given vector v.

„ The cumulative sums should be stored in a

vector of the same length as v.

1, 3, 5, 0 v

1, 4, 9, 9 cumulative sums of v

February 26, 2009 Lecture 12 3 February 26, 2009 Lecture 12 5

Drawing a single line segment

a= 0; % x-coord of pt 1

b= 1; % y-coord of pt 1

c= 5; % x-coord of pt 2

d= 3; % y-coord of pt 2

plot([a c], [b d], ‘-*’)

x-values (a vector)

y-values (a vector)

Line/marker format

February 26, 2009 Lecture 12 6

Drawing a polygon (multiple line segments)

% 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

Color is a 3-vector, sometimes called the RGB

values

„ Any color is a mix of red, green, and blue

„ Example:

c= [0.4 0.6 0]

„ Each component is a real value in [0,1]

„ [0 0 0] is black

„ [1 1 1] is white

February 26, 2009 Lecture 12 12

Let’s compute colors!

Show “all combinations” of red and blue

„ Assume some kind of granularity—discretize the color value range for red and blue „ Assume no contribution from green (set to 0)

Program development:

„ 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

„ The granularity was the programmer’s choice

„ Choosing how to display the colors was a design

problem!

„ What if you want compute “all combinations” of

the R, G, and B values? How would the program

change?

„ Another design problem: how to show all color

combinations of the 3 -vector on a 2 -dimensional

plot?

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

Yes

No

Do these two fragments produce the same vector?

A

B

% given a<b, % given integer n> v= linspace(a,b,n);

February 26, 2009 Lecture 12 23

Simulate twinkling stars

„ Get 10 user mouse clicks as locations of 10

stars—our constellation

„ Simulate twinkling

„ 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

Before

After

Second operation: normalize

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

Third operation: smooth

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

for i=1:n

xNew(i) = (x(i) + x(i+1))/2;

yNew(i) = (y(i) + y(i+1))/2;

end

Will result in a subscript

out of bounds error when i is n.

Smooth

February 26, 2009 Lecture 12 58

Proposed Simulation

Create a polygon with randomly located vertices.

Repeat:

Centralize

Normalize

Smooth

February 26, 2009 Lecture 12 59

x= zeros(1,1); for k= 1: x= [x x]; end y= x(7);

Yes

No

No, but there’s

some other error

Is there a subscript out of bounds error?

A

B

C