Question: Math 98, Homework 5 Part 1: Recursive MatMul Your task is to create three functions for multiplying two nn matrices, having the following forms: 1.function
Math 98, Homework 5 Part 1: Recursive MatMul Your task is to create three functions for multiplying two nn matrices, having the following forms: 1.function C = defaultMatMul(A,B) 2.function C = myMatMul(A,B) 3.function C = recMatMul(A,B) The first just usesC = A*B, using Matlabs default matrix multiplication algorithm. The second uses three nested loops and the formula Cij=nk=1AikBkj, and therefore uses only scalar multiplication as a primitive. The third is a recursive algorithm, with base case when n= 1 (just c=ab), and uses the recursion[C11C12C21C22]=[A11A12A21A22][B11B12B21B22]=[A11B11+A12B21A11B12+A12B22A21B11+A22B21A21B12+A22B22]. The function will therefore involve calling eight recursive calls, each on a problem half the size of the original: C11 = recMatMul(A11,B11) + recMatMul(A12, B21) . . . . . . C22 = recMatMul(A21,B12) + recMatMul(A22, B22) You may assume for the purposes of your recursive algorithm that n is a positive integer of the form 2m. Thus you can divide the matrices neatly in half at every step. Part 2: PlottingUse code along the lines of tic; CODE HERE; timeElapsed = toc (check the documentation on tic and toc) to record the time required for matrix multiplication by each algorithm. UseA = randn(n,n); B = randn(n,n) to make random nn matrices and time how long it takes each of your algorithms to computeC=AB for n= 32,64,128, . . . ,4096(or 8192, if your computer can handle it). Make a plot with one line for each method. If you want to get clearer results you may optionally run each trial multiple times and report the average
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
