Question 1 --------------------------------------------------- function [lo,hi] = split(x) % Sort 1-d array X and reutrn 2 arrays: % LO contains all elements in X <= MEAN(X) % HI contains all elements in X > MEAN(X) % Sort array x = selectsort(x); % Define LO<=mean loloc = (x <= mean(x)); % locations lo = x(loloc); % values % Define HI~=LO hi = x(~loloc); %%%%%%%%%%%%%%%%%%%%%%%%%% function y = selectsort(y) % Sort and overwrite 1-d array Y in % ascending order len = length(y); for j = 1:len-1 % start of unsorted segment % Find min in unsorted segment [val,ind]=min(y(j:len)); ind = ind+j-1; % Swap min into correct position y([j ind]) = y([ind j]); end --------------------------------------------------- Question 2 --------------------------------------------------- function newtonRalston(x) % Find positive real root using Newton-Ralston % method given a starting point X. % Equation solved: % f(x)= x^4 - 8.6x^3 - 35.51x^2 + 464.4x - 998.46 % Parameters: % (use named constants--good programming style!) target = 0; % stopping value it = 0; % current no. of iterations maxit = 1000; % maximum no. of iterations eps = 0.001; % tolerance % Initial function value: f=x^4 - 8.6*x^3 - 35.51*x^2 + 464.4*x - 998.46; % Iterate for root: while ( abs(f-target)>eps & it=3 & cn>=3 for i=2:rn-1 for j=2:cn-1 grad_x(i-1,j-1)=(elevat(i,j+1)... -elevat(i,j-1))/(2*dx); grad_y(i-1,j-1)=(elevat(i+1,j)... -elevat(i-1,j))/(2*dy); end end end % save matrices in files save grad_x save grad_y --------------------------------------------------- Question 5 Part b calls the function from part c. Note that we defined the Fibonaci sequence in the question statement as 1 2 3 5 8 13 ... This is different from the usual definition 0 1 1 2 3 5 8 13 ... The solutions below match the question statement. If you would like more practice, write code for the usual definition of the Fibonaci sequence. --------------------------------------------------- function result = rfact(n) % Calculate N factorial (permutation, n!) using recursion, N>=0 if n<=1 result = 1; else result = n * rfact(n-1); end --------------------------------------------------- function [fn] = fibo(n) % Find the Nth Fibonacchi number, N>0 if n==1 fn=1; elseif n==2 fn=2; else fn=fibo(n-1)+fibo(n-2); end --------------------------------------------------- function [fnsum] = fibosum(n) % Find the sum of the first N Fibonacchi numbers. % fibo(n) is a function in the solution above. if n==1 fnsum=1; else fnsum=fibo(n)+fibosum(n-1); end --------------------------------------------------- Question 6 The recursion in this question is too difficult to be on Prelim 2. You should know how to use cell-arrays though. Look over the solution for your information, and for getting an idea of how/when recursion can be used. To understand the code below, first check out what a binary tree looks like as a cell-array. In Matlab, type in the tree: bintree={'A', {'B', {'D',{'X', {}, {}}, {'Y', {}, {}}}, {}}, {'C', {}, {}}} Then type celldisp(bintree) to see how each node is stored in the cell-array. --------------------------------------------------- function fringe_array = fringe(tree) % Search through a binary TREE recursively and save all leaves to FRINGE_ARRAY fringe_array = []; if isempty(left(tree)) & isempty(right(tree)) % No left or right subtree fringe_array = [root(tree)]; else % Search through subtrees recursively if ~isempty(left(tree)) fringe_array = fringe(left(tree)); end if ~isempty(right(tree)) fringe_array = [fringe_array fringe(right(tree))]; end end function root_of_tree = root(tree) root_of_tree = tree{1}; function left_subtree = left(tree) left_subtree = tree{2}; function right_subtree = right(tree) right_subtree = tree{3}; ---------------------------------------------------