Professional Documents
Culture Documents
Exercise 2: X X X X F
Exercise 2: X X X X F
Exercise 2
Exercise 2 page 1
MATLAB beginner's course Bar-Ilan University Computer Center ©
7) Logical operations:
a) Read variables from glass.wav, and save elements 400 to 420 to a variable d.
b) Calculate an array z in which there are zeros where elements of d are negative and
ones otherwise.
c) Calculate an array s in which there is -1 where elements of d are negative and 1
otherwise.
d) Find out how to calculate the sign of a number, and calculate S, the signs of the
elements of d.
e) Verify by calculation, that s and S are identical.
20
1
8) Find out how to compute the sum of an array, and show that ∑2
n =1
n
≈1
1 n −1
Li = ∑ X n ( i −1) + j . L has elements approximately n times less than X.
n j =0
a) Read variables from guass3.mat, and calculate the number of elements in L. Use
help to find out how to use floor.
b) compute the local-average vector of 2 points: Li = (X2i-1 + X2i)/2 . You can drop up to
2 last elements of X.
c) Same as (b), with 3 points: Li = (X3i-2 + X3i-1 + X3i)/3 .
10) Simpson's integration formula of a discrete function of N points is given by:
h ⎛ N −1 N
⎞
F= ⎜ ∑ fi + ∑ fi ⎟ , where h is the spacing of the x values.
2 ⎝ i =1 i=2 ⎠
a) Construct a vector of equally-spaced x values.
b) Choose a simple function (eg. x2, sin(x) etc.) and compute the values fi at the points
xi.
c) Perform the integration according to Simpson's formula. You can use the function
sum.
d) Compute F at the first and last points of x, according to the analytic integral (eg.
x3/3, -cos(x) etc.) and compare the result to what you got in (c).
11) Simple statistics:
a) Read the file glass.wav. Save elements from data: x=elements 2000 to 4000
and y=elements 6000 to 8000.
b) Use the functions sum and length to calculate the means of x and y.
c) Compute the normalized covariance of x and y, which is given by:
∑ (x − x )( y − y )
i i
D= i
, where <x> and <y> are the means of x and y
∑ (x − x ) ( y − y )
2 2
i i
i
Exercise 2 page 2