You are on page 1of 3

Homework Problems Week 3 440:127 Fall 2011 S. J.

J. Orfanidis This homework set is due online at Sakai no later than 5:00 AM on Monday, October 3, 2011. Please submit the HTML le together with any supporting PNG les generated by MATLABs publish command from your script M-le (alternatively, you may copy-paste from the HTML le into Word, or submit a single MHT le). Late submissions will not be accepted. Please upload your les to Sakai under Assignments for week-3, not to the Drop Box. Please do the following problems: 1. Problems 4.1, 4.6, and 4.11. In particular, for Problem 4.6, use linspace to generate a length-6 vector of pressure values P in the interval [0, 100] kPa. Use meshgrid to generate a 26 array of heights h, expressed in millimeters (note that in metric units you must replace 100 kPa by 100,000 Pa). 2. Consider the matrix: 2 A = 8 5

2 5 6

7 4 1

3 5 8

Using appropriate MATLAB functions do the following, each in a single operation: a. Determine the minima of the columns and their locations. Then, determine the minima of the rows and their locations. Then, repeat for the maxima of the columns and rows. b. Compute the column means and the row means. Compute the column sums and the row sums. Compute the column products and the row products. c. Apply the functions cumsum, cumprod, and diff column-wise, and then, row-wise. d. Sort A column-wise in decreasing order. Then, sort it row-wise in increasing order. e. Replace by zeros the 22 submatrix dened at the intersection of the second and third columns and rows of A. f. Flip the matrix A upside-down. Then, ip it left-right. Determine the matrix B whose rows and columns are the reverse of those of A. Rotate the matrix A clock-wise by 90 deg. Rotate by 90 deg followed by ipping upside down, and compare the result with the transpose of A. g. Replace the main diagonal (i.e., the A11 , A22 , . . . , matrix elements) by zeros. Replace all elements of A by zeros but for the upper-left 22 submatrix. h. Swap the second and fourth columns of A. Swap the rst and third rows of A. i. Rearrange A into a 26 matrix whose elements in each column are taken column-wise from those of A. Similarly, reshape A into a 43 matrix and compare it with the transpose of A. Then, reshape A into a 62 matrix. Finally, reshape A into a 223 three-dimensional array. j. Using the function repmat, replicate A three times horizontally. Then, replicate it two times vertically. Then, replicate it three times horizontally and two times vertically. Re-do these replications without the use of repmat, just by appropriately concatenating A with itself. k. What do the commands, A==5, and, q = nd(A==5), do? With one command, replace all the entries of A that are equal to 5 by the value of 50. Then, with another single command restore their values back to 5. [ Not fully covered yet, but see class notes: week01.pdf, week03.pdf (p.42). ] l. Square every entry of A. Then, take the square root of the result. Compute the matrix, B = log(2*A+3), then, recover A from B. Repeat with, B = log2(2*A+3), and with, B = exp(2*A+3). 3. Consider the Taylor series for the sine function, and its (N+1)-term approximation:

sin(x)=

(1)n x2n+1 , (2n + 1)! n=0


1

S(x, N)=

(1)n x2n+1 (2n + 1)! n=0

a. For N = 30 and the column vector x = [1; 2; 5; 4; 10], use the function ndgrid (or, meshgrid) to provide a fully vectorized computation of S(x, N) as a column vector, and show how to print the formatted table of values:
x sin(x) S(x,N) --------------------------------1.00 0.841471 0.841471 2.00 0.909297 0.909297 -5.00 0.958924 0.958924 4.00 -0.756802 -0.756802 10.00 -0.544021 -0.544021

b. Consider the trigonometric series expansion and the corresponding N-term approximation of the periodic signal f (t)= t(1 t) dened over its basic period 0 t 1:

f (t)= t(1 t)=

n=1

sin2 (nt)

n2

S(t, N)=

N n=1

sin2 (nt)

n2

0t1

Using the function linspace, dene t to be a column vector of 101 equally-spaced values in the interval [0, 1], and choose N = 20. Using the function ndgrid (or, meshgrid) provide a fully vectorized computation of S(t, N) as a column vector. On the same graph, plot f (t) and S(t, N) versus t. Repeat with N = 40. c. For the case N = 40 of part (b), plot f (t) and S(t, N) over three periods spanning the time interval 1 t 2. Note that while S(t, N) is manifestly periodic in t, you would need to extend f (t) periodically over these periods (i.e., its values over the basic period [0, 1] are repeated.) 4. Two formulas for calculating are: 9801 (4n)! (1103 + 26390 n) = (n!)4 (396)4n 2 2 n=0

=2 3

(1)n (2n + 1) 3n n=0

The rst one is due to Ramanujan and converges extraordinarily fast. Truncate both series to a maximum number of 10 terms. Dene the vector n = 0 : 9 and using the built-in functions cumsum and factorial create a 103 matrix of the approximation values, as shown below, where the nth entries are computed by using the terms 0 : n. Your computations must be completely vectorized. Moreover, use appropriate MATLAB commands, e.g., the function fprintf, to print your results as in the table below, where the very last row prints itself.

i.e.,

http://en.wikipedia.org/wiki/Pi

vectorized in both variables x and n.

http://en.wikipedia.org/wiki/Ramanujan

n f(n) g(n) ----------------------------------------------0 3.1415927300133055 3.4641016151377544 1 3.1415926535897940 3.0792014356780038 2 3.1415926535897931 3.1561814715699539 3 3.1415926535897931 3.1378528915956805 4 3.1415926535897931 3.1426047456630846 5 3.1415926535897931 3.1413087854628832 6 3.1415926535897931 3.1416743126988376 7 3.1415926535897931 3.1415687159417840 8 3.1415926535897931 3.1415997738115058 9 3.1415926535897931 3.1415905109380797 ----------------------------------------------Inf 3.1415926535897931 5. The distance traveled by a uniformly-accelerated object in time t, with initial velocity v and acceleration a, is given by: 1 x = vt + a t2 (1) 2 We wish to compute x for a range of t values and for three different sets of v and a, dened as follows in MATLAB notation: t = 0:10; v = [10, -10, 20]; a = [2, 4, -6]; Using the meshgrid function, dene appropriate 103 meshgrid matrices V, A, T that correspond to the vectors v, a, t, and calculate the corresponding 103 matrix of distances X that implements Eq. (1), vectorized in all three variables v, a, t, and whose three columns represent the x distances for the three cases of v, a. Moreover, use fprintf to display your answers in a table (to be completed by your program) as shown below, in which the last three columns are the entries of the matrix X: --------------------------------v = 10 v = -10 v = 20 t a = 2 a = 4 a = -6 --------------------------------0 0.00 0.00 0.00 1 11.00 -8.00 17.00 2 24.00 -12.00 28.00 ... 10 .... 200.00 .... 100.00 .... -100.00