Monday, January 23, 2006 Matrix mult. is not commutative: A*B usually not = B*A Associativity: A*(B*C) = (A*B)*C Diagonal matrix: o Diagonal = set of elements for which row index = column index. o Diagonal matrix = a matrix that is nonzero only on its diagonal. o Diagonal matrix does not have to be square in this course! Dot Product: v•w = v(1)*w(1) + . . . + v(N)*w(N) o Also known as the scalar product. 1. Manufacturing Materials. The Acme Widget Company makes three products out of a variety of components. The Deluxe Widget comprises three gears, one crank, and two springs. The Standard model is made from one gear, two cranks, and three springs. A Value model is constructed from four cranks and two springs. The production manager is reviewing the production planned for the week and discovers that the plant will produce 2000 Deluxe, 4000 Standard, and 2000 Value widgets. How many gears, cranks, and springs will be required? a. First make (on paper) the appropriate table and the appropriate vector. b. Make the corresponding Matlab matrix and vector. Wednesday, January 25, 2006 2. Here is a fictitious set of three ships: The Neptune has 100 POSH staterooms, 300 First-class staterooms, and 400 Excursion staterooms. The Jewel has 150 POSH, 200 1stClass, and 500 Excursion. The Venture has 115, 186, and 398. A 6-day cruise for POSH costs 1,500 per person, for First-Level costs 850 per person, and for Excursion costs 590 per person. Each room has two persons. How much money will each ship bring in on one 6-day cruise if it is full? POSH 1st-Class Excurs 3000 POSH Nept 100 300 400 1700 1st-Class Jewel 150 200 500 1180 Excurs. Vent 115 186 398 o Matrix multiplication: Z=X*Y “Inner matrix dimensions” of X and Y must be equal. Z has outer matrix dimensions of X and Y. end Z (m, n) X (m, k )Y (k , n) k 1 o Array division: Z = X./Y or X.\Y X, Y, and Z have same shape Z (m, n) X (m, n) / Y (m, n) or Z (m, n) X (m, n) \ Y (m, n) o Only addition and array multiplication are commutative. Only addition, subtraction, array multiplication, and matrix multiplication are associative. o Array exponentiation: (also called “array power”) Z=X.^Y X, Y, and Z have same shape Z (m, n) X (m, n)Y ( m,n ) o Matrix exponentiation: Z=X^p (we don’t cover p^X) Case 1: p is a positive integer: Z = X*X*X...X (p of them) Case 2: p is -1: X^(-1) = inv(X) = inverseX Case 2: p is any other negative integer. Left for homework Case 3: p is not an integer: we don’t cover this case 1. parentheses 2. exponentiation 3. multiplication and division 4. addition and subtraction Special (wrong) numbering for in-class exercise only: 1. parentheses 2. addition and subtraction 3. multiplication and division 4. exponentiation 6*3+2, 6*3*2, 2*3^2+1 = (2*3)^(2+1) Friday, January 27, 2006 o Polymorphism = dependence of a function’s behavior on type or number of argument(s) o Polymorphism according to type of input argument(s) o Polymorphism according to number of input argument(s) o Polymorphism according to the number of output arguments o Variety 1: You decide whether to put everything into one object or divide elements into separate objects. o Variety 2: Returned elements are always divided into separate objects and you decide how many objects you want returned: In 1625, Peter Minuet, representing the Dutch government, paid an Indian tribe known as the Canarsee Delawares $24.00 for Manhattan Island. Today that same island is estimated to be worth somewhere around $760 billion. How bad a deal was it? Well, we can answer that question with Matlab. It happens by a lucky coincidence that 1625 was also the year that the New York Stock Exchange came into being. Suppose the Indians had invested their money in a diversified stock portfolio of stock-mutual funds, growth stocks, and blue chips, and gotten an average 7% annual rate of return? How much money would they have in 2006? Assume that interested is compounded annually. The formula is principal = original*(1 + annual rate)^years. Linear algebra in Matlab o Problems of the form A*x = b are problems in linear algebra, sometimes also called matrix algebra. In mathematics texts, the form is usually written this way: Ax=b (no star and not bold). Monday, January 30, 2006 Linear algebra in Matlab o Problems of the form A*x = b are problems in linear algebra, sometimes also called matrix algebra. In mathematics texts, the form is usually written this way: Ax=b (no star and not bold). Suppose we have this set of linear algebraic equations: 3x – 4y = 5 6x – 10y = 2 >> x = A\b x = 7 4 >> A*x ans = 5 2 1. A*x = b % (think this, but don’t do it in Matlab) 2. A\(A*x) = A\b % (think this, but don’t do it in Matlab) 3. (A\A)*x = A\b % (think this, but don’t do it in Matlab) 4. I*x = A\b % (think this, but don’t do it in Matlab) 5. x = A\b % (do this in Matlab) Suppose we have this set of linear algebraic equations: 2 x- 1 3 y = - 3x + 2 y= Re-arrange the equations so that both x and y are on the same side. 2 x+ y = - 1 Ans: 3 + 3x + y = 2 To solve a set of simultaneous equations into Matlab: - Re-arrange so that all the unknowns are on the left sides of all the equations. Set A = to a 2D matrix whose elements are laid out as are the coefficients on the left side of the equations. Set b = column vector of the knowns that appear on the right x = A\b puts the answers in the column vector x Wednesday, February 1, 2006 Manufacturing Materials Problem. Related to the earlier problem on manufacturing materials used to make widgets. In that problem the production manager is reviewing the production planned for the week and discovers that the plant will produce 2000 Deluxe, 4000 Standard, and 2000 Value widgets, and he needed to determine how many gears, cranks, and springs would be required. W1 gears cranks Production springs W2 W3 This time, however, the manager has received a report on inventory from the warehouse manager, which tells him how many parts were used last week, and he needs to calculate from that how many widgets were made. How does he do that? Widget 1 uses 3, 1, 2 of parts 1, 2, and 3 Widget 2 uses 1, 2, 3 of parts 1, 2, and 3 Widget 3 uses 0, 4, 2 of parts 1, 2, and 3 The numbers of parts used are as follows: # part 1 used = 10,000 # part 2 used = 18,000 # part 3 used = 20,000 2000, 4000, 2000 materials = T*products is the Matlab statement of the manufacturing-materials relationship product is a column vector of the amounts of items produced materials is a column vector of the amounts of materials consumed Each column of T corresponds to one type of product Manufacturing Materials. Try it again but this time change the first element of the materials to 9000. Manufacturing Materials. Try it again, but this time change the first element of parts to 500. 1. Manufacturing Materials Problem. Suppose we are making 3 items out of 3 materials: Item-1 requires 1 unit of material-1, 5 units of material-2, 4 units of material-3 Item-2 requires 2 units of material-1, 3 units of material-2, 7 units of material-3 Item-3 requires 3 units of material-1, 7 units of material-2, 8 units of material-3. a. We need to manufacture 23 of item-1, 63 of item-2, and 19 of item-3. How much of each material is required? b. How many items of each type can be made from 211 units of material1, 519 units of material-2, and 676 units of material-3? c. How many items of each type can be made from 210, 520, 675 units of materials 1, 2, and 3, respectively? Students must use their personalized cover sheets from now on (received from Hamm by email) A new folder called “graders list”, will appear on Blackboard Monday at 8am giving the name of the grader for each question on your homework. You spend the weekend trying to understand why you lost points, and then email or talk with your grader to get an explanation. Note that they cannot change a grade. If you feel that an error has been made, fill out an appeal. 2. Consider these manufacturing problems, all of which are based on the linear algebra equation Ax=b. Not a word problem! x1 + 2x2 + 3x3 = b1 5x1 + 3x2 + 7x3 = b2 4x1 + 7x2 + 8x3 = b3. Find b. x = [23; 63; 19] 1. Another problem. Same x but different equations x1 + 2x2 + 3x3 = b1 5x1 + 3x2 + 7x3 = b2 4x1 + 8x3 = b3 7x2 Find b. Equations again x1 + 2x2 = b1 3x3 5x1 + 3x2 + 7x3 = b2 4x1 + 8x3 = b3 7x2 Now b = [200; 400; 700] is given and x is unknown. Find x and check it by constructing an “error vector” e = A*x-b. Rule of thumb: If error is less than 1e6*eps, then the error is due to round-off: An exact answer exists, but it is not representable in your computer and requires an infinitely large computer and infinitely long to calculate it! eps is the smallest number that, when added to 1 gives more than 1, but half of it gives 1. Linear algebra in Matlab continued o Sometimes the number of equations (materials) is greater than the number of unknowns (items to be produced). o This means that in Ax=b there are more rows in A and b than elements in x. o Note that matrix multiplication of A times x is still possible because the number of columns in A still equals the number of rows in x. Monday, February 06, 2006 1. Manufacturing materials. Now supposed that there are four types of materials and three types of items. The compositions given in Error! Reference source not found. above are the same, viz, Product-1 requires 1 unit, 5 units, 4 units of the respective materials 1, 2, and 3. Product-2 requires 2 units, 3 units, 7 units of the respective materials 1, 2, and 3. Product-3 requires 3 units, 7 units, 8 units of the respective materials 1, 2, and 3. But, in addition, a new material is required for the production of each product. There are 9, 4, and 5 units of material-4 required for products, 1, 2, and 3, respectively. We need to manufacture 25 of item-1, 80 of item-2, and 5 of item-3. What amount of each material is needed? 2. Same A, but we are given b = [200; 400; 700; 570], and we are to find x and check it by computing the error vector, e = A*x - b. 3. Use the same A and same b, but try to solve the problem this way: x = inv(A)*b. 4. Same A as before, but this time b = [200; 400; 700; 550] (i.e., b(4) is smaller by 20). Find x and check it by calculating e = A*x - b. From our error check we find that e’ = [2.234479.4, -0.480837.4, -0.593975.4, 0.282845.4]. At least one of these elements is larger in magnitude than 1e6*eps (in fact, all of them are), so, it does not check out! Matlab carries out matrix division, x = A\b, if A and b have the same number of rows, regardless of whether there is a solution to Ax = b or not! For the case in which there is no solution x to Ax = b, asking Matlab to do this: x=A\b produces the x that comes as close as possible to solving it. “As close as possible” means that the sum of the squares of the elements of the error vector, e = A*x-b is as small as possible. In other words, ei2 is smaller than for any other x. o Ill-conditioned matrices o Let’s study these two equations: 3x – 4y = 5 3.1 x – 4.1 y= 5.3 February 10, 2006: “ill-conditioned” because small errors in b can produce much larger errors in x. 4x + 3y = 40 Software Engineering = discipline devoted to studying how to organize and solve complex problems by designing and writing computer programs Elementary problems = problems that are so simple that a direct solution is easily seen. (i.e., no special software engineering methods are needed.) Decomposition = breaking down a complex problem into a set of simpler subproblems Stepwise refinement = the repeated application of decomposition to each of a set of progressively simpler subproblems Top-down design = stepwise refinement continued to the point at which all subproblems are elementary, followed by the solution of the elementary problems. o A Five-step process for Top-down design using Matlab: 1. State the problem Black-box design 2. Define the inputs and outputs 3. Design the algorithm a. Apply decomposition b. Perform stepwise refinement to the elementary level 4. Write the algorithm in Matlab (with the Editor Window) 5. Test the program (in the Command Window) o If the program fails a test, go back to either 4 (easier) or 3 (harder). Pseudocode – a mixture of English and Matlab code. Example: 1. State the Problem: Describe a number that is typed in. 2. Define the inputs and outputs: Inputs: A number Outputs: Description of that number 3. Algorithm a. Decompose into subproblems Get number from user Decide its category Print the description b. Stepwise Refinement (only one step needed for this problem) Get number from user - prompt user for number and read it in Decide category - less than 10? - equal to 7? - equal to 13? - greater then or equal to 10? Print message Friday, February 17, 2006 Give the value of this expression in Matlab: 2*(3 == (1+2)) + (1>0) How many lines does this program print: 1, 3, 4, or 12? for ii = 1:3 fprintf('%d: ',ii) for jj = 1:4 fprintf('%d ',jj) end fprintf('\n') end while conditional statements end Compare: if conditional statements end The conditional is an expressions that plays the same role that it does in a branching statement. If the real part of every element of the expression evaluates to true (= nonzero), then the statements are executed. After the statements are evaluated, control returns to the beginning of the loop again and the conditional is again evaluated. When the conditional evaluates to false (zero), the loop ends. Exercise: for loop that sums 1 + 2 … + 5 Monday, February 20, 2006 o BREAK terminates the execution of FOR and WHILE loops. % print first string of positives for ii = 1:length(x) if x(ii) <= 0, break, end; fprintf(‘%f\n’, x(ii); end Wednesday, February 22, 2006 Class exercise: Write a Matlab script (m-file) that calculates this sum 2n æ1 ö S = å çç ÷ ÷ ÷ ç è ø p n= 0 100 but have it stop if the change in the sum is less than .001%. This is a bit hard. Friday, February 24, 2006 o Local means visible only within one function Monday, February 27, 2006 o This function will have one input argument and one output argument o If the function were called y=safe_inverse(x) then the function should return y = 1/x unless x = 0, in which case y = 0. Class exercise: Suppose we have the following script, called foo.m, defined: jj = 0; for ii = 1:length(x) if x(ii) > 0 jj = jj + 1; y(jj) = x(ii); end end What would be the value of y if we did this in the command window: >> y = 77; >> x = [2 -3 1]; >> foo; First line of M-file must contain the word “function” Name of M-file should be the same as function name, but Matlab uses the name of the M-file as the name of the function! (It is like a required comment: Matlab requires it but then ignores it!) Formal parameters (called “dummy arguments” in Matlab): Input arguments are always in parentheses. Output precedes an equal sign. All variables in a function, including the formal parameters, are local variables. Thus, they are unavailable outside the function. (Exceptions are possible by declaring a variable to be “global”. We do not allow the use of global variables in CS 103, unless specifically permitted.) The local character of variables implements the concept of information hiding. Information hiding is important to the successful writing of programs with multiple programmers. Class exercise: Write a function called big that takes one vector as an input argument and returns 1 if the sum of its elements is greater than 1000 and 0 otherwise. You may use Matlab's built-in function sum inside your function. Here are examples of how it is used: >> big([1 10 100 9900]) ans = 1 >> big([1 10 100 9]) ans = 0 Wednesday, March 01, 2006 Class exercise: Write a function called bigger that takes two input arguments: The first one is a vector and the second one is a scalar. It returns 1 if the sum of the elements of the vector is greater than the scalar and 0 otherwise. You may use Matlab's built-in function sum inside your function. Here are examples of how it is used >> bigger([1 10 100 9900], 1e6) ans = 0 >> bigger([1 10 100 99], 4) ans = 1 Example of nargin : function a = below(hw1,hw2,test1,threshold) if nargin < 4 threshold = 73; end hw_avg = (hw1+hw2)/2; weights = [55, 45]; grades = [hw_avg,test1]; weighted_avg = weights*grades'/sum(weights); if weighted_avg < threshold a = true; else b = false; end Class exercise on nargin: Change bigger so that it compares the sum to 1000, when there is no second actual argument given (i.e, if no second argument is given, the function behaves as if a second argument equal to 1000 was given). Solution: function ans = bigger(x,t) if nargin < 2 t = 1000; end if sum(x) > t ans = 1; else ans = 0; end Example of nargin AND nargout: function [yp,ym] = increment(x,add_on) % [yp,ym] = increment(x,add_on) makes yp = x+add_on % If there is no 2nd input argument, yp = x+1; % (Sort of like C/C++/Java's ++ and += operators.) % If there is a 2nd output argument, ym = x-addon, % or, there is no 2nd input argument, ym = x-1; if nargin < 2 add_on = 1; end yp = x + add_on; if nargout > 1 ym = x - add_on; end Class exercise on loops: Write a function called odd_factorial that calculates the "oddfactorial" of its argument. Assume that it will be called with an integer. Use it to calculate 1*3*5*7*…*55. (Hint: This is like calculating the sum, 1+3+5+7+…+55, but this time you multiply the accumulator by the loop index each time through instead of adding, AND you initialize the accumulator to 1, instead of 0.) Answer (see below) Class exercise on robust functions: Modify odd_factorial so that it gives an error if the input is not an integer. Use the built-in function, error to make an error. It prints out in red any string that you give it as an argument, e.g., error(‘Input must be an integer’). function myprod = odd_factorial(N) if N - fix(N) ~= 0 error('Sorry. Input must be an integer.'); end myprod = 1; for n = 1:2:N myprod = myprod*n; end Friday, March 03, 2006 Predicates. A predicate is defined as a function that returns 0 to indicate falseness and nonzero to indicate truth. Writing polymorphic functions by accommodating varying numbers of actual arguments-o A function may be called with fewer input arguments or output arguments than specified in the function definition (but not more than are specified). o Argument counters: 1. nargin = number of actual input arguments 2. nargout = number of output arguments requested Matlab will not allow a call with too many arguments—either input or output. function rand_plot(N,style,pause_on,figure_no) % Plot random points in 2D if nargin < 1 N = 100; end if nargin < 2 style = 'k*'; end if nargin < 3 pause_on = 0; end if nargin < 4 figure_no = 1; end figure(figure_no); axis([0,1,0,1]); axis square; hold on; for ii = 1:N x = rand; y = rand; plot(x,y,style); if pause_on pause(5/N) end end hold off; SPRING BREAK! Monday, March 13, 2006 o Subfunctions o Primary function is the first function in a file. All others are either subfunctions or nested functions subfunctions function x body of x function suby1 body of suby1 function suby2 body of suby2 nested functions function x body of x function nestedy1 body of nestedy1 end function nestedy2 body of nestedy2 end body of x end o Can have nested-in-nested-in-nested-in… Can have both nested and sub, but if so, then there must be an end at the end of every function in the file. Predicates. A predicate is defined as a function that returns 0 to indicate falseness and nonzero to indicate truth. function answer = is_scalar(x) [rows,cols] = size(x); if rows == 1 && cols == 1 answer = 1; else answer = 0; end Persistent Variables o Variables that keep their value between calls of the same function function a = accumlate(x) persistent storage if isempty(storage) storage = x; else storage = storage + x; end a = storage; feval allows you to send a function to a function! function plot_function(function_name, vect) % Example call plot_function(@sqrt,[1:100]) y = feval(function_name, vect) Wednesday, March 15, 2006 Global Variables o Variables that are visible to two or more functions one or more functions and the command window. o Syntax: global a b This is another declaration statement (persistent was the first), which is information about the type, lifetime, or scope of an object. o Global variables lead to trouble They are visible everywhere and can be changed without warning. (Functions, such as deck above, are visible everywhere too (by means of the path, but they cannot be changed without warning.) It is difficult to follow the progress of a program during execution if global variables are involved. For these reasons, students in CS 103 (and 101) are not allowed to use global variables! Data structures The combination of the first set of functions and their global variable is an example of an implementation of a data structure. The combination of the second set of functions and their persistent variable is an example of an implementation of the same data structure. A data structure is a user-defined data type A data type, or simply, type, is defined as a set values and a set of operations that can be performed on those values. o In this case the set of values is all scalars (a better implementation would enforce that in put_card) and the set of operations is {put_card, take_card} o It is possible, as we have shown here, to have more than one implementation of the same data type. o This data structure is an example of a stack. Set of values varies from application, but set of operations = {add object, remove object} Last in is first out: LIFO o This new data structure is an example of a queue. Set of values varies from application, but set of operations = {add object, remove object} First in is first out: FIFO Friday, March 17, 2006 (91 present) 1. Write a predicate called is_real that takes one argument and determines whether or not all of its elements are real. Assume that the argument is a vector. Make it efficient. 2. Same as above, but argument may be a 2D array. Answer: function a = is_real (x) a = true; [M,N] = size(x); for ii = 1:M for jj = 1:N if imag(x(ii,jj)) ~= 0 a = false; break; end end if a == false; break; end end In-class question: function a = f(b,c) a = b + c; and these commands executed in the command window: >> a = 1; b = 2; c = 3; >> b = f(a,c);fprintf(‘%d, %d, %d\n’, a, b, c); What will be printed? A) 1 2 3 B) 4 2 3 C) 4 4 3 D) 1 4 3 Answer = is D under here In-class question function a = g(b,c,d) if nargin < 2 c = 2; end if nargin < 3 d = 3; end a = b + c + d; >> a = 1; b = 2; c = 3; d = 4; >> b = g(a,c); fprintf(‘%d, %d, %d\n’, a, b, c); What will be printed? A) Error, wrong number of input arguments B) 9 2 3 C) 1 7 3 D) 1 4 3 Answer Answeris=under C here Monday, March 20, 2006 Class Exercise: Write a function called one_or_two that takes as input one scalar and then, if the input equals 1, it returns “One”, else if it equals 2, it returns “Two”, else it returns “Not one or two”. Use an if-elseif statement. Put the value to be returned in single quotes. function s = one_or_two(n) if n == 1 s = 'One'; elseif n == 2 s = 'Two'; else s = 'Not one or two'; end function s = one_or_two(n) switch n case 1 s = 'One'; case 2 s = 'Two'; otherwise s = 'Not one or two'; end function s = one_or_two(n) if nargin == 0 n = 0; end switch n case 1 s = 'One'; case 2 s = 'Two'; otherwise s = 'Not one or two'; end Class Exercise: Write a predicate called same_sum that checks a matrix to determine whether each of its columns and each of its rows has the same sum. function a = same_sum(A) a = true; [M,N] = size(A); total = sum(A(1,:)); for ii = 2:M if sum(A(ii,:)) ~= total a = false; return; end end for jj = 1:N if sum(A(:,jj)) ~= total a = false; return; end end Wednesday, March 22, 2006 Test Day Friday, March 24, 2006 Mathworks seminar at Vanderbilt, Wed. Mar 29 www.mathworks.com/seminars/vanderbilt or call 978-659-6104 data type, or simply, type, is defined as a set values and a set of operations that can be performed on those values. o All elements of a given array must be of the same type. o The type of an element of an array is an elementary type. o Both variables (e.g., x, y) and values (e.g., 7, [1 2 3]) have types. o Specifying the type of a Matlab object: number of dimensions (2 or more in Matlab) size of each dimension elementary type The class function Mixed-mode operations o A mixed-mode operation is one whose operands are not all of the same type. o Arithmetic operations: Only some mixing allowed. See items 1-7 on page 7 of Ch. 8. o Result is of “narrower” type. Monday, March 27, 2006 (89 present) There is a function that will pad the ends with spaces to get rid of the raggedness: >> A = strvcat(long,short) Arithmetic on strings: Legal, but only with other strings or with singles or doubles In latter case result is a single or double (Matlab widens in this case!) Class exercise: Write a function called fun_code that adds n to each character and returns the result as a string. Class exercise: Use fun_code in a for loop to discover the number that must be added to the string ‘[srwqoz’, in order to get an English word. (NOTE that the first character is a left bracket). HINT: The number is less than 20. Relational Operators accept strings as operands o Lexicographical order: sa <= sb if and only if sa(1) < sb(1) or sa(1) == sb(1) && sa(2:end) <= sb(2:end) String functions o See Table 3 in Chapter 8 Structs o Heterogeneous structures whose indices are user-chosen names Wednesday, March 29, 2006 o Each element of a struct is called a field o The index is the field name o The value of the field can be of any type, even another struct Dynamic field naming function s = create_struct_dynamic % S = CREATE_STRUCT_DYNAMIC Create a structure from field names % and values that are input by the user while 1 field_name = input('Enter a field name (zero to quit): '); if field_name == 0 break; end field_value = input('Enter value for this field: '); s.(field_name) = field_value; end Struct functions o Table 4 of Ch 8 Cell arrays Activation Records o An activation record is simply an area of memory in which all information is stored that is needed during the execution of a function, or during the execution of a command in the command window. Activation-record stack—the area of memory that hold activation records of the Command Window and all functions that have been called but have not yet returned. The activation records are placed on the stack in the order in which they are called and are removed in the order in which the functions return. Friday, March 31, 2006 Static versus dynamic allocation o Dynamic allocation is allocation during the running of a program o Static allocation is allocation before the running of a program If an activation record is allocated for every function before the program starts, the allocation scheme is static. If the activation records are allocated only as needed during the running of the program, the allocation scheme is dynamic. Before tax is called: While tax is running: While high is running: AR for high AR for tax AR for tax AR for command window AR for command window AR for command window After high has returned: After tax has returned: AR for tax AR for command window AR for command window In-Class Question The function F is called from the Command Window. F calls G, G calls H, H returns, G calls J, J calls H, H calls K, K returns, and H is still active. The variables of the Command Window are stored in an activation record. How many activation records are on the stack? In-class question: The function A is called from the Command Window. A calls B, B calls C, C calls D, D returns, and C is still active. Assume there is one activation record on the stack for the Command Window. What is the current number of activation records on the stack? Recursion Recursion is the use of a recursive definition or a recursive function o A recursive definition defines a concept in terms of the concept itself. There must be a base case and a recursive case (there can be more than one of each) Base case is a case the does not involve self reference Recursive case is a case that does involve self reference The self reference must a “smaller” version of the definition. o A recursive function calls itself either directly or indirectly Direct recursion: f calls f—known as a recursive call. Indirect recursion: f calls g, which calls, h,…, which calls f. o A recursive definition can be implemented by a recursive function The base and recursive cases that appear in the definition always appear in the function that implements it. In-class Question: The function f is called from the Command Window, f calls itself recursively, f calls itself again recursively, f calls itself a third time, f returns, and f is still active. Assume there is one record on the stack for the Command Window. How many activation records are there on the stack now? Monday, April 03, 2006 o Class exercise: Consider this definition: 0 , for n = 0 s(n) = s(n-1) + n, otherwise Write a recursive function that implements it. Infinite recursion = a sequence of recursive calls that never reaches a base case. o Class exercise: Write a function that implements this function: 13, for n 20 h n 2 h n 1 n 2 , otherwise More than one recursive call in a single function o Class exercise: Write a function that implements this function: 0, if n 0 w n w n 1 +3, if 0 n 10 2 w n 2 , otherwise o There are two possible cases: Case 1: One call takes place per activation, but there are two or more choices of arguments. Both calls must have “simpler” arguments, but they differ from each other. We will see this case when we look at searching algorithms. Case 2: Two or more calls per activation. Both calls must have “simpler” arguments. We will see this case when we look at sorting algorithms. Example from Ch. 9—Fibonacci series: 1, for N 1 F N 1, for N 2 F N 2 F N 1 , otherwise Wednesday, April 05, 2006 Look at the News. There is an article about downloading the Symbolic Math Toolbox for free. Please do that some time this week! Do it sooner, rather than later, so that if you have trouble, you can get it resolved before you need it! N F(N) 1 1 Fibonacci sequence starts like this: 2 1 3 2 4 3 5 5 6 8 7 13 8 21 Rule: Any two successive numbers can be added to get the next one. Here’s how your thinking process might go, if you were given this rule and asked to come up with a recursive function: Thought 1: F(N) + F(N+1) F(N+2) Thought 2: F(N+2) = F(N) + F(N+1) Thought 3: F(N) = F(N-2) + F(N-1) Thought 4: The “Recursive Leap of Faith” Searching o We will study two types: Sequential search (sometimes called “linear search”) Binary Sequential search is the only choice for unsorted data. Binary is the better choice for sorted data. o See sequential-search algorithm in Chapter from textbook o See binary_search_recursive from textbook. Monday, April 10, 2006 In-class question: When using the binary search to find the target value 7 in the list, [10, 36, 45, 96, 100, 112, 112, 190, 200, 365], which numbers are compared with the target. Give the numbers, in order, not their indices.: A: All the numbers B: 10 C: 100, 36, 10 D: 100, 45, 36, 10 ANSWER = C Sorting Wednesday, April 12, 2006 o We will study three types of sorting algorithms: Selection sort (Ch. 10.2.1 of textbook, Figure 3) Quicksort (Ch. 10.2.2 of textbook, Figure 4) Merge sort (Ch. 10.2.3 of textbook, Figure 5) o Selection sort is easy to understand and is naturally iterative. o Quicksort is harder to understand and is naturally recursive. o Merge sort is harder to understand and is naturally recursive. o Selection sort is slow for large lists but good for small lists (say, < 50) o Quicksort is slow for some large lists but good for small lists (say, <50) o Merge sort is fast for large lists (say, >= 50) In-class Question: When using the selection sort on the list [112,365,45,112,36,190,10], what does the list look like after the second swap has taken place? Monday, April 17, 2006 Algorithmic complexity o The behavior of an algorithm as its input gets larger is called its algorithmic complexity o The Order of an algorithm is the most important measure of its complexity Order = the asymptotic form of t(N) Order is expressed by giving the fastest growing term in t(N). The constant multiplier is omitted The term is surrounded by O(…). Class Example: Sequential search, t(N) = a + b*N. What is the order of sequential search? Answers: o Sequential search is O(N). o Order of sequential search is N. o Sequential search is order N. Class Example: Selection sort, t(N) = a + bN+c N2. What is the order of the selection-sort algorithm? t(N) approaches c N2 as N gets larger: o i.e., cN2/(a+bN +cN2) 1 Answers: o Selection sort is O(N2) o Order of selection sort is N2 o Selection sort is order N2 Class Example: Binary search, t(N) = a + b*ceil(log2 (N+1)). What is the order of binary search? Make is as simple as possible. Hints: The ratio of x to ceil(x) 1, as x infinity. The ratio of log2(x) to log2(x+c) 1, as x infinity. log(x) = d*log2(x), where d = log(2) o Dominance If the ratio of one term to a second term goes to infinity with N, then the first term dominates the second. Summary of method for determining the order of a typical t(N) (1) Simplify t(N) by removing ceil or floor. For example, ceil(log(N+1)) becomes log(N+1). (2) Simplify by removing constant added to N. For example, log(N+c) becomes log(N). Thus, ceil(log(N+1)) = logN. (3) Drop all but the dominant term (4) Simplify the dominant term by eliminating any multiplying factor: e.g., 2NN Symbolic Mathematics = the manipulation of symbols according to mathematical rules. A Matlab variable is either symbolic or non-symbolic syms x tiger causes the variables x and tiger to be symbolic Wednesday, April 19, 2006 Maple also provides variable-precision arithmetic with vpa(…): Assignment of symbolic expressions produce symbolic variables: Maple can simplify expressions It replaces functions by arithmetic expressions if possible. It finds expressions that use fewer symbols We can tell Maple to try hard to simplify by using simple Sometimes we want a less simple version. We get that with expand: Maple can solve equations with solve. Maple can solve simultaneous equations with solve. Maple can substitute values for some symbols to with subs. Maple can do calculus: diff(e) takes the derivative of e. diff(e,n,x) takes the nth derivative of e with respect to x int(e,x) takes the indefinite integral of e with respect to x. int(e,x,a,b) takes the integral of e with respect to x from a to b. To get help on the symbolic functions, diff or int,type help sym/diff.m or sym/int.m Friday, April 21, 2006 (last day ) Evaluation of Teaching Assistants Description of Test 3 Format is similar to Tests 1 and 2: Short-answer questions on concepts Questions on what algorithms (as opposed to Matlab statements) will do Problems in which you write code in Matlab Teammate evaluation Coverage: Non-programming questions/problems: 1. Activation record stack 2. GUI—short-answer only 3. Searching 4. Sorting 5. Order notation—short-answer only Programming questions/problems: 6. Implement a recursive function whose definition is given 7. Two options—you pick one: o Loops o Symbolic Mathematics Teammate evaluation: You can evaluate only those teammates whose names you know! (for both projects) Review of material See test_3_review.doc under Other Links In-class examples: Activation record stack: What is the maximum number of activation records at any given time here: o A is called from the command line, A calls B, B calls C, C calls D, D returns, C returns, B returns, A returns. ANS: 5 o A is called from the command line, A calls B, B calls C, C calls C, C returns, C returns, B returns, A returns. ANS: 5 o What if C called itself 10 more times before it began returning in the previous example? ANS: 15 Implementation of a given recursive function: o Write a recursive function that implements this recursive definition: f(n) = 4 if n <=0 f(n) = 100 – f(n-10), if n > 100 f(n) = n + f(n-1) otherwise Loops o Write a for loop that carries out z = x./y, but sets z(n) to 1 if y(n) is zero. Check for equal lengths before entering the loop. Search o Binary search Given LIST = [10 20 44 44 56 67 80 81 82 83 90 94 99] Given Target = 4, or 81, or 100, list the indices of the numbers that are compared with the target, in order. Sort o Selection sort Given a = [4 6 3 2 –1 4 3 5 6 6 8], give the list after the first, second, and third swaps. Note that a number is never swapped with itself. o Merge sort Given a = [4 6 3 2 –1 4 3 5 6 6 8], give the two lists that are merged at the final step. ANS: [-1 2 3 4 6] and [3 4 5 6 6 8] Order notation Give the Order using “Big-O” notation of the following functions: 16 2N 3N 2 ANS: O ( N 2 ) N log N 100 N ANS: O ( N log N ) 16 6.3N 3 N ANS: O( N ) 2N N 3 ANS: O (2 N ) Symbolic mathematics o Write code to solve this problem: A rocket goes up vertically at 100 m/s. Its height y in meters = 100*t 12 gt 2 , where t is the time in seconds after the launch and g = 9.8m/s2. Consider a point on the ground 25 meters from the launch point. The distance of the rocket from that point as a function of the height of the rocket is d 252 y 2 . Give the symbolic mathematical commands in Matlab that will give (a) the value of that distance and (b) the rate at which that distance is increasing 100 seconds after the launch? ANS: >> syms t >> g = 9.8; >> y = 100*t-(1/2)*g*t^2; >> d = sqrt(25^2+y^2); >> subs(d,{t},100) ans = 3.9000e+004 >> subs(diff(d),{t},100) ans = 879.9998 (Check the answers at 0 seconds. They should be 25 m and 0 m/sec.) >> subs(d,{t},0) ans = 25 >> subs(diff(d),{t},0) ans = 0 o Write code that will add these two polynomials and simplify the result: ax 2 bx c and ex3 fx 2 g ANS: >> syms a b c x e f g >> p1 = a*x^2+b*x+c; >> p2 = e*x^3+f*x^2+g; >> p3 = p1 + p2 p3 = a*x^2+b*x+c+e*x^3+f*x^2+g >> p_simple = simple(p3) p_simple = e*x^3+(a+f)*x^2+b*x+g+c o Write code to calculate p – q and factor the result, where p 3x 4 8 x3 31x 2 28 and q 3x3 5x . ANS: >> p1 = 3*x^4-8*x^3+31*x^2-21*x+28 >> p2 = 3*x^3+5*x >> factor(p1-p2) ans = (3*x^2-2*x+4)*(x^2-3*x+7) END OF LECTURES