CS99 Fall 2002 Lab #10 (11/12/02) Due 11/15 at noon. Objective: In Lab10, you will practice writing Function M-files. Background: Suppose that A and B are two matrices, as follows: A = [ 1 2 3 ; 4 5 6 ] B = [ 2 3 4 ; 3 4 5 ] You can perform arithmetic on matrices. For instance, MATLAB has the following two operations: * matrix addition: A+B, if A and B have the same size * element-by-element multiplication: A.*B Note that A*B is different from A.*B Explanation of Matrix addition/multiplication: Let X,Y be two matrices of the same size. Let Z=X+Y. Then Z is the matrix of the same size as X and Y such that each of its entry is the summation of the corresponding entries of X and Y. e.g., the second row and third column of Z is the summation of the second row and the third column of X and the second row and the third column of Y. Similarly, W=X.*Y is the matrix of the same size as X and Y such that each of its entry is the product of the corresponding entries of X and Y. Example: For the given matrices A and B above, determine A+B and A.*B by hand. You can easily check your work with MATLAB, of course: Answer: +---------------+ +-----------+ | 1+2 2+3 3+4 | | 3 5 7 | A + B = | | = | | | 4+3 5+4 6+5 | | 7 9 11 | +---------------+ +-----------+ +---------------+ +------------+ | 1*2 2*3 3*4 | | 2 6 12 | A.*B = | | = | | | 4*3 5*4 6*5 | | 12 20 30 | +---------------+ +------------+ Functions: Suppose that you didn't know or couldn't use MATLAB syntax, as in many other languages. Write a script file lab9_sol.m to solve A+B and A.*B. In this script file, two function files add.m and mul.m are called to perform matrix addition and matrix element-by-element multiplication, respectively. lab9_sol.m: +-------------------+ | A=[1 2 3; 4 5 6] | | B=[2 3 4; 3 4 5] | | C=add(A,B) | | D=mul(A,B) | +-------------------+ You need to write add.m and mul.m that will be called by lab9_sol.m.