WEEK #3, Lecture 1: Sparse Systems, MATLAB Graphics Visualization of Matrices Good visuals anchor any presentation. MATLAB has a wide variety of ways to display data and calculation results that can be powerful tools in presenting information. Recall our heat distribution example from last week:
2 The heat-balance equations: T 1 : 20+T 2 +T 6 3 T 1 = 0 T 2 : T 1 +20+T 3 +T 7 4 T 2 = 0 T 3 : T 2 +20+T 4 +T 8 4 T 3 = 0 T 4 : T 3 +20+T 5 +T 9 4 T 4 = 0 T 5 : T 4 +20+120+T 10 4 T 5 = 0 T 6 : T 1 +T 7 +T 11 3 T 6 = 0 T 7 :. T 6 +T 2 +T 8 +T 12 4 T 6 = 0
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 3 Matrix form 3 1 0 0 0 1 0... 1 4 1 0 0 0 1 0... 0 1 4 1 0 0 0 1 0... 0 0 1 4 1 0 0 0 1... 0 0 0 1 4 0 0 0 0... 1 0 0 0 0 3 1 0 0... 0 1 0 0 0 1 4 1 0... 0 0 1 0 0 0 1 4 1.... T 1 T 2 T 3 T 4 T 5 T 6 T 7 T 8 T 9 T 10 T 11. = 20 20 20 20 (20+120) 0 0 0 0 120 0.
4 Solving and Interpreting Exercise: Download W3 1.m, which creates and solves the plate temperature equations for a 5 5 grid. Explain what each of the final steps does. T = A \ B; Tm = reshape(t, N, N) pcolor(tm) colorbar
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 5 Explain why we only see a 4 4 grid on the screen, even though Tm is a 5 5 matrix. Explain what else is wrong with the temperature plot displayed by pcolor, relative to the temperature problem.
6 Refining the plot These commands get us close to the image we want. However, we still want to have the x and y axes scaled so that our plate has the correct dimensions, 1 cm 1 cm, and get the vertical ordering of the data correct. To accomplish this, we ll need to introduce a new command, meshgrid.
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 7 Adding coordinates with meshgrid Exercise: Use the following commands to plot the grid of temperatures. [X, Y] = meshgrid(linspace(0, 1, 5), linspace(0, 1, 5)) pcolor(x, Y, Tm) colorbar What changes in the pcolor plot?
8 How meshgrid works Here are the X and Y matrices created by meshgrid, along with the original Tm temperature matrix. X = 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 0 0.2500 0.5000 0.7500 1.0000 Y = 0 0 0 0 0 0.2500 0.2500 0.2500 0.2500 0.2500 0.5000 0.5000 0.5000 0.5000 0.5000 0.7500 0.7500 0.7500 0.7500 0.7500 1.0000 1.0000 1.0000 1.0000 1.0000 Tm = 38.1938 39.7321 43.5407 51.7942 70.7147 54.8493 57.1940 62.6366 72.9213 91.0645 69.1600 71.5580 76.8905 86.1899 100.6219 81.0729 82.9873 87.1774 94.3261 105.2330 91.0713 92.1411 94.5056 98.7040 105.9843
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 9 How does the pcolor(x, Y, Tm) command use all this information to generate the plot?
10 With the help of meshgrid, our pcolor plot is now correctly scaled along the x and y axes. Inverting the y ordering Finally, we want to make sure that the top-left corner in our matrix shows up in the top-left corner of our graph. From the meshgrid output, what is the y coordinate assigned to the first row of Y? Exercise: What vector does the command linspace(1, 0, 5) generate? Exercise: Using that new linspace command for the y points in meshgrid, what do the first row and column of Y look like?
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 11 Final Display Code [X, Y] = meshgrid(linspace(0, 1, N), linspace(1, 0, N)) pcolor(x, Y, Tm) colorbar Other plotting options: surf(x, Y, Tm) colorbar shading flat shading interp contour(x, Y, Tm, 0:10:100)
12 Speed of computation Now that we have successfully graphed a simple 5 5 grid of temperatures, we consider the problem of generating and displaying the equilibrium temperatures using a finer grid. Exercise: Start with the script W3 2.m. Modify it so it generates a 20 20 grid and run it. Note the approximate run time. Exercise: Incrementally increase N by 20 and re-run the script. When does it start getting slow enough to be annoying?
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 13 Matrix Characteristics Unfortunately, the value of N that slows MATLAB to a crawl does not result in a particularly detailed temperature grid. Since the accuracy of our calculation depends on how small each sub-cell in the plate is, we would really like to use a finer grid. Let s investigate further. Exercise: Type clear to clear MATLAB s memory. Then run the script with N = 60. Type spy(a) to see where the non-zero elements are in A. What do you notice? If there are roughly 5 entries per row in A, what fraction of its values are non-zero? How much space have we wasted storing the number 0 over and over again?
14 Common Matrix Structure Both trusses and heat distribution equation matrices are based on connectivity properties. Trusses: eachbeam(columnofvalues)appearsinatmost2joints,or4equations (horizontal and vertical force equations, for 2 joints). Heat: each row models temperature at a grid point; non-zero coefficients only for neighbouring grid points. In both cases, the number of non-zero elements in the A matrix will be very small relative to the numberof zeros. In these cases, if we stored the matrix in full, the vast majority of the values we store would be largely redundant zero values.
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 15 Sparse Matrices Matrices with relatively few non-zero elements are called sparse matrices There are two important implications of using sparse matrices in computer programs. Memory: Much less memory is needed to store sparse matrices. Instead of storing every entry in the matrix, just the non-zero elements are stored, along with their coordinates in the matrix. Run Time: Algorithms like Gaussian elimination can be coded to accept sparse matrices as input. How would knowing a matrix was sparse save time during row operations?
16 Notethatseeinglotsofzerosinamatrixmakesitconceptually sparse, butonlyby explicitly creating and storing the matrix in sparse format would it provide any practical advantage. Here is our original, conceptually sparse matrix, in standard form 3 1 0 0 0 1 0... 1 4 1 0 0 0 1 0... 0 1 4 1 0 0 0 1 0... 0 0 1 4 1 0 0 0 1... 0 0 0 1 4 0 0 0 0... 1 0 0 0 1 3 1 0 0... 0 1 0 0 0 1 4 1 0... 0 0 1 0 0 0 1 4 1.... This format requires storing n 2 numbers for an n n matrix. Almost all of these numbers are zeros.
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 17 The same matrix can also be stored in an explicit sparse format: A(1,1) = -3; A(1,2) = 1; A(1,5) = 1; A(2,1) = 1; A(2,2) = -4; A(2,3) = 1;. By implication, any element that isn t explicitly listed must be a zero. For each element, sparse storage is a little more expensive, because we store both the location and value, but if 90% of the values in A are zeros, we will still save more space overall by not having to store the zero values.
18 Empirical Comparison Exercise: Set N = 80 in W3 2.m. This makes A a 6400 6400 matrix, with roughly 40 million elements. Using regular format, solving takes seconds Exercise: Change the command A = zeros(nsq, Nsq) to A = sparse(nsq, Nsq) Using sparse format, solving takes seconds.
Week 3 Sparse Matrices, Inverses, Non-Linear Equations 19 We now try an even larger case. Using a 250 250 plate grid (62,500 10 5 temperature values, or a matrix A with 10 10 values). Exercise: What happens if we try to create this A matrix using regular format, with A = zeros(nsq, Nsq)? Exercise: If we use the sparse matrix format instead, can the 62,500 62,500 A matrix be built successfully? If so, how long does it take to solve the equations and generate the plot? In fact, using the sparse approach, most computer time in the script is spent creating the A matrix, not solving the final equations!
20 Sparse Summary If a problem leads naturally to a sparse matrix, it can be a huge advantage. Storage of sparse matrices is compact, so larger matrices can be stored. Specially designed algorithms can save orders of magnitude of time. Unfortunately, you can t choose sparseness: problem types either lead to sparse matrices, or they don t. Also, even if a system is conceptually sparse, you need to make sure that your software takes advantage of the sparse structure of the equations/matrices. e.g. in MATLAB, creating the matrix with the sparse command, rather than zeros.