CS 100 J Lecture 23 n Previous Lecture

  • Slides: 15
Download presentation
CS 100 J Lecture 23 n Previous Lecture – Mat. Lab and its implementation,

CS 100 J Lecture 23 n Previous Lecture – Mat. Lab and its implementation, continued. n This Lecture – Mat. Lab demonstration CS 100 Lecture 23 1

Mat. Lab n The notation j: k gives a row matrix consisting of the

Mat. Lab n The notation j: k gives a row matrix consisting of the integers from j through k. >> 1: 8 ans = 1 n 2 5 5 6 7 8 7 9 To get a vector of n linearly spaced points between lo and hi, use linspace(lo, hi, n) >> linspace(1, 3, 5) ans = 1. 0 1. 5 n 4 The notation j: b: k gives a row matrix consisting of the integers from j through k in steps of b. >> 1: 2: 10 ans = 1 3 n 3 2. 0 2. 5 3. 0 To transpose a matrix m, write m’ >> (1: 3)’ ans = 1 2 3 CS 100 Lecture 23 2

Mat. Lab Variables n Mat. Lab has variables and assignment: >> evens = 2.

Mat. Lab Variables n Mat. Lab has variables and assignment: >> evens = 2. *(1: 8) evens = 2 4 6 n 8 10 12 14 16 To suppress output, terminate line with a semicolon (not to be confused with the vertical concatenation operator) >> odds = evens - 1; >> n To see the value of variable v, just enter expression v: >> odds = 1 CS 100 3 5 7 9 Lecture 23 11 13 15 3

Mat. Lab Idiom n Mat. Lab has for-loops, conditional statements, subscripting, etc. , like

Mat. Lab Idiom n Mat. Lab has for-loops, conditional statements, subscripting, etc. , like Java. But the preferred programming idiom uses uniform operations on matrices and avoids explicit loops, if possible. n Essentially every operation and built in function takes a matrix as an argument. CS 100 Lecture 23 4

sum, prod, cumsum, cumprod n Function sum adds row elements, and the function prod

sum, prod, cumsum, cumprod n Function sum adds row elements, and the function prod multiplies row elements: >> sum(1: 1000) ans = 500500 >> prod(1: 5) ans = 120 n Function cumsum computes a row of the partial sums and cumprod computes a row of the partial products: >> cumprod(1: 7) ans = 1 2 6 >> cumsum(odds) ans = 1 4 9 CS 100 24 120 16 25 Lecture 23 720 36 5040 49 64 5

Compute Pi by Euler Formula n Leonard Euler (1707 -1783) derived the following infinite

Compute Pi by Euler Formula n Leonard Euler (1707 -1783) derived the following infinite sum expansion: 2 / 6 = 1/j 2 (for j from 1 to ) >> pi = sqrt( 6. * cumsum(1. / (1: 10). ^ 2)); >> plot(pi) n To define a function, select New/m-file and type definition: function e = euler(n) % Return a vector of approximations to pi. e = sqrt( 6. * cumsum(1. / (1: n). ^ 2)); n Select Save. As and save to a file with the same name as the function. n To invoke: >> pi = euler(100); >> plot(pi) CS 100 Lecture 23 6

Help System n Use on-line help system >> help function. . . description of

Help System n Use on-line help system >> help function. . . description of how to define functions. . . >> help euler Return a vector of approximations to pi. CS 100 Lecture 23 7

Compute Pi by Wallis Formula n John Wallis (1616 -1703) derived the following infinite

Compute Pi by Wallis Formula n John Wallis (1616 -1703) derived the following infinite sum expansion: (2 * 2) * (4 * 4) * (6 * 6) *. . . / 2 = -------------------(1 * 3) * (3 * 5) * (5 * 7) *. . . n Terms in Numerator evens. ^ 2 n Terms in Denominator 1 3 5 7 9. . . 3 5 7 9 11. . . ------------1*3 3*5 5*7 7*9 9*11. . . odds + 2 i. e. , odds. * (odds + 2) n Quotient prod( (evens. ^ 2). / (odds. * (odds+2)) ) n Successive approximations to Pi pi = 2. * cumprod( (evens. ^2). / (odds. * (odds+2)) ) CS 100 Lecture 23 8

Wallis Function n Function Definition function w = wallis(n) % compute successive approximations to

Wallis Function n Function Definition function w = wallis(n) % compute successive approximations to pi. evens = 2. * (1: n); odds = evens - 1; odds 2 = odds. * (odds + 2); w = 2. * cumprod( (evens. ^ 2). / odds 2 ); n Contrasting Wallis and Euler approximations >> plot(1: 100, euler(100), 1: 100, wallis(100)) CS 100 Lecture 23 9

Compute Pi by Throwing Darts n Throw random darts at a circle of radius

Compute Pi by Throwing Darts n Throw random darts at a circle of radius 1 inscribed in a 2 -by-2 square. n The fraction hitting the circle should be the ratio of the area of the circle to the area of the square: f= / 4 n This is called a Monte Carlo method y 1 1 CS 100 Lecture 23 x 10

Darts n The expression rand(h, w) computes an h-by-w matrix of random numbers between

Darts n The expression rand(h, w) computes an h-by-w matrix of random numbers between 0 and 1. >> x = rand(1, 10); >> y = rand(1, 10); n Let d 2 be the distance squared from the center of the circle. >> d 2 = (x. ^ 2) + (y. ^ 2); n Let in be a row of 0’s and 1’s signifying whether the dart is in (1) or not in (0) the circle. Note 1 is used for true and 0 for false. >> in = d 2 <= 1; n Let hits(i) be the number of darts in circle in i tries >> hits = cumsum(in); n Let f(i) be franction of darts in circle in i tries >> f = hits. / (1: 10); n Let pi be successive approximations to pi >> pi = 4. * f; CS 100 Lecture 23 11

Compute Pi by Throwing Needles n In 1777, Comte de Buffon published this method

Compute Pi by Throwing Needles n In 1777, Comte de Buffon published this method for computing : N needles of length 1 are thrown at random positions and random angles on a plate ruled by parallel lines distance 1 apart. The probability that a needle intersects one of the ruled lines is 2/. Thus, as N approaches infinity, the fraction of needles intersecting a ruled line approaches 2/. CS 100 Lecture 23 12

Subscripting n Subscripts start at 1, not zero. >> a = [1 2 3;

Subscripting n Subscripts start at 1, not zero. >> a = [1 2 3; 4 5 6; 7 8 9] ans = 1 2 3 4 5 6 7 8 9 >> a(2, 2) ans = 5 n A range of indices can be specified: >> a(1: 2, ans = 2 5 n 2: 3) 3 6 A colon indicates all subscripts in range: >> a(: , 2: 3) ans = 2 3 5 6 8 9 CS 100 Lecture 23 13

Control Structures: Conditionals if expression list-of-statements end if expression list-of-statements elseif expression list-of-statements. .

Control Structures: Conditionals if expression list-of-statements end if expression list-of-statements elseif expression list-of-statements. . . elseif expression list-of-statements else list-of-statements end CS 100 Lecture 23 14

Control Structures: Loops while expression list-of-statements end for variable = lo: hi list-of-statements end

Control Structures: Loops while expression list-of-statements end for variable = lo: hi list-of-statements end for variable = lo: by: hi list-of-statements end CS 100 Lecture 23 15