============================================================ Question 4: 2d-array, nested for-loops Suppose someone has measured the elevations at a soccer field as shown in following picture. At all the crosses a measurement of the elevation was taken. The distances between the measurements in the x-direction (dx) and y-direction (dy) are 25m and 20m, respectively. The surveyer knew about Matlab and stored the measurements in a 5-by-6 matrix that he saved in a Matlab file called elevat.mat. +------+------+------+------+------+ | | | | | | | | | | | | +------+------+------+------+------+ | | | | | | | | | | | | Y +------+------+------+------+------+ ^ | | | | | | | | | | | | | | +------+------+------+------+------+ -----> X | | | | | | | | | | | | +------+------+------+------+------+ You are asked to write a Matlab script file to determine the gradients in the x and y directions at all interior points (3*4=12 points), i.e., all points excluding the points on the boundary. The gradients should be stored in two 3 by 4 matrices and written to files called grad_x.mat and grad_y.mat. This is how you determine the gradient for an interior point where i refers to the row and j to the column of matrix elevat, respectively: grad_x(i,j)=(elevat(i,j+1)-elevat(i,j-1))/(2*dx) grad_y(i,j)=(elevat(i+1,j)-elevat(i-1,j))/(2*dy) Which values of the matrix elevat did you not use for your calculations? ============================================================ Question 5: recursion Write functions to determine the following: a) N! (N factorial) b) the sum of N numbers in the Fibonnaci sequence: 1 2 3 5 8 13 ... c) the Nth number in the Fibonnaci sequence. E.g., the 5th number is 8. ============================================================ Question 6: more recursion Computer scientists refer to binary trees as a structure that can be illustrated pictorially by the following example: A / \ B C / D / \ X Y Here, A is the root of the tree and B and C are its children. C has no children and B has one child D, and so on. A, B, C, etc. are called nodes. In a binary tree, a node can have zero, one, or two children. A node that has no children is called a leaf. E.g., X is a leaf. In Matlab, a binary tree can be represented using cell-arrays. The example above can be represented as {'A', {'B', {'D',{'X', {}, {}}, {'Y', {}, {}}}, {}}, {'C', {}, {}}} The idea is that the binary tree with root A is given by {'A', left-subtree, right-subtree} where left-subtree in this case is a tree with root B and therefore it will be represented by {'B', ..., ...}. C here does not have any children, so we say C has empty left-subtree and empty right-subtree reprsented by {}. Write a function $fringe$ that returns an array of the leaves of a given tree. E.g., if the above array is the input to function $fringe$, the result should be {'X', 'Y', 'C'}. Strategy: recursion on the tree ============================================================