============================================================ Question 1: iteration, swap, function, logical array Given a one-dimensional numeric array, create two arrays sorted in ascending order: one contains all the values at or below the mean; the other contains the values above the mean. Write two functions: $selectsort$ accepts a numeric array as input, sorts it in ascending order in-place (i.e., without creating a new array), and returns the sorted array. You must use the Selection Sort method described below. $split$ accepts a numeric array as input, calls $selectsort$ to sort the array, and returns two arrays: one contains all the values at or below the mean; the other contains the values above the mean. Loops are not allowed in this function. Selection Sort (in ascending order): ------------------------------------ - Start with a numeric array of length N and assume that it is unsorted. - Select the smallest value and put it in the right place, now N-1 elements remain unsorted. - Select the second smallest value and put it in the right place, now N-2 elements remain unsorted. - Continue until all elements are sorted. Matlab reminders: ----------------- Function $mean(x)$ returns the mean of the values in (1-d) array $x$. Function $min(x)$ for a 1-d array $x$ can return both the minimum value and its position (first occurrence). E.g., >> [value,index]=min([4 3 5 2 2 5]) value = 2 index = 4 >> ============================================================ Question 2: Newton's method with a twist The Newton-Ralston method is a modified Newton's method for dealing with multiple roots. The central idea is to apply the Newton's method to a function of the function whose roots we wish determine--the second derivative is needed in addition to the first dervative. Write a function $newtonRalston$ to find the positive real roots of the function f(x) = x^4 - 8.6x^3 - 35.51x^2 + 464.4x - 998.46 using the Newton-Ralston method, described below. Newton-Ralston Method (for root finding): ----------------------------------------- Suppose we wish to find the roots of a function f(x). Define a new function u(x) = f(x)/f'(x) Eq. 1 Function u(x) has the same roots as f(x) does, since u(x) becomes zero everywhere that f(x) is zero. Apply Newton's method to u(x) instead of f(x): delta = x - x(0) = -u(x)/u'(x) Eq. 1 can be differentiated to obtain u'(x): u'(x) = 1 - (f(x)*f"(x))/(f'(x))^2 Algorithm for the Newton-Ralston method: 1. Pick an initial value x 2. Calculate f(x) 3. While tolerance is not met, iterate as follows: - compute f'(x), f"(x), u(x), u'(x) - compute the new value of x, denoted xn - compute the new function value f(xn) For this question, do not worry about the number of iterations. ============================================================ Question 3: Bisection method Write out the algorithm for the bisection method. Try to do this without looking at your project notes. ============================================================ 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? ============================================================