EXERCISE 3:

 

Building and using Pascal’s triangle:

 

Pascal’s triangle is used to obtain the coefficients for polynomial expansions: (x + y)^n

 

The triangle is:

 

1

1                 1

1          2          1

1          3          3          1

1          4          6          4          1

1          5          10        10        5          1

 

This means:

 

(x + y)^0 = 1

(x + y)^1 = 1x + 1y

(x + y)^2 = 1x^2 + 2xy + 1y^2

(x + y)^3 = 1x^3 + 3 x^2 y + 3 xy^2 + 1y^3

 

Note: each value is the addition of the values immediately above and above-left. The first value at (1,1) is 1. Any unfilled value is 0. Thus, the element at (2,1) = 1 (above) + 0(above-left) = 1 and the element at (2,2) = 0 (above) + 1 (above-left).

 

Question 1: Build the first 10 rows of Pascal’s triangle using for loops.

Hint: It is necessary to keep an extra columns of 0’s to the left of the triangle, so that the 1’s are at position (:,2). The above-left of position (row, col) is (row –1, col –1) and if col is 1, col – 1 is 0, which is not a valid index in MATLAB.

Hint 2: The first element of your pascal triangle is at (1,2) and it is equal to 1. The zeros are already there.

Hint 3: You 2 nested for loops, one for rows, one for columns.

Hint 4: After you finish, remove the extra column of 0’s

 

Question 2: Now that you have the triangle. Find the sum of each row and the sum of each column. What do they represent?

 

Question 3: (If you want the bonus marks): Remember Fibonacci’s numbers? They are also hidden in this triangle, can you find them? Hint, add diagonals.