Вы находитесь на странице: 1из 3

# CS 16 S12 Homework 7: Solving the Heat (diffusion) equation in 2-D using Gauss-Seidel Iteration Softcopy Due in BB: 3/28

by 8am Caution: Next weeks homework will build on this one, so make sure you complete this! Problem: Write a Matlab program to solve the 2-D Laplace equation for steady-state temperature distribution across a square metal plate, given fixed temperatures at the edges of the plate, using Gauss-Seidel iteration. Background: The steady-state temperature T in a 2-D x-y plate, with prescribed temperatures at the four boundaries, is described by a partial differential equation known as the Laplacian of T: 2 2 T T 0 . In other words, once the temperature gradient is established across the plane 2 x y2 (the first derivative of T with respect to a change in position in the x and y directions describes this gradient), it reaches steady-state and doesnt change (i.e., the second derivative of T with respect to a change in position in the x and y directions is zero). If you know the boundary temperatures, you can solve this partial differential equation to determine the steady state temperature at any x-y coordinate interior to the domain. Spatial Discretization: First, we discretize the continuous 2-D plane by representing it with a number of nodes (a 2-D grid), associating a discrete temperature with each node in the grid (e.g., see sample 1010 grid below). We will store this grid as a matrix of temperatures called T. T

TRADEOFF: Obviously, the finer we discretize the grid, the better the resolution we will have on the temperature distribution, but the more the computational resources (both space and time) it will take to compute them.

GENERAL ITERATIVE UPDATING SCHEME: Since the boundary values are specified (yellow shaded portions), we will only update the interior portion of this grid (unshaded portion). We will do this update iteratively, as follows: for all internal locations, update the value of the element by averaging the values of its 4 nearest neighbors (above, below, left, right), as illustrated below. avg When all internal values have been updated, one iteration will have been completed. Continue iterating until convergence is achieved, according to the convergence criterion described below, (or until some maximum number of iterations has been hit). Convergence criterion: We will use the root mean square error (RMSE), which is simply the square root of the sum (over all interior elements) of the squared differences between the value at the previous iteration and the value at the current iteration. Convergence is said to have occurred when the RMSE is less than some small specified value epsilon (e.g., 1e-4, 1e-6, 1e-9, etc., depending on the number of significant digits of accuracy that you want). For practical reasons, we will also specify some maximum number of iterations, so your iterative loop will terminate when convergence is achieved or when the maximum number of iterations is hit, whichever comes first. We will explore two different iterative updated schemes; this week you will implement GaussSeidel iteration (uses explicit for-loops to step through the elements of the matrix). Next week youll implement Jacobi iteration (using vectorization to do whole-matrix updates without explicit for-loops), and make some comparisons between the two methods. GAUSS-SEIDEL ITERATION: Write a function to do Gauss-Seidel iteration to estimate the distribution of temperatures across the plate, as follows. Update the interior points of the matrix in the order going from top to bottom, left to right. The updates for all internal elements are computed by averaging the values of its 4 nearest neighbors (above, below, left, right; note that the values from the elements above and to the left that are used in the average are FROM THE CURRENT ITERATION, whereas those below and to the right are the values from the PREVIOUS iteration. This means that you must explicitly loop through all interior elements to do the updates, and you must calculate the running sum of squared differences of the interior elements as you go, so that you can complete the computation of the RMSE at the end of each iteration. PLATE PROGRAM: Write a function that takes the following inputs (as input parameters) and produces the following outputs. (Test youre your program thoroughly, but only hand in output from one representative run.) INPUTS: For simplicity, we will assume that each of the four boundary temperatures (for the top, bottom, left, and right boundaries) are constant values; these four values will be input to the program, along with optional input parameters specifying the grid resolution (i.e., number of

elements in one side of a square grid), tolerance, and maximum number of iterations (set reasonable default values for these). In addition, the name of the function to do the actual iterative updating must be passed into the function (this week, that will be your Gauss-Seidel function; next week youll also use the Jacobi function as an alternative). The plate is initialized by putting the specified boundary values in the edge elements of the matrix (yellow shaded portions what should you do at the corners?). The interior portion of the grid (unshaded portion) is also initialized to some value (what value should the interior elements be initialized to, in order to minimize the number of iterations needed for convergence?). For good modularity, do this actual grid initialization in a separate function. After the grid has been initialized, your program must call the function you passed in to do the actual computation of the temperature values, and output the results, as follows. OUTPUTS: Your program must create a 3D surface plot of the final temperature distribution, appropriately labeled with info specifying the iteration method used, the final rmse, the number of iterations, etc.; Note that if T is a 2D matrix, the command to surface plot is simply surf(T). You can manually rotate your plot to see what the 3D surface looks like. Pick a good viewing angle (that best illustrates the shape of the surface) to print out for the hardcopy. HAND IN: Hardcopy of all source code and sample output for one call using Gauss-Seidel iteration, using the test values of 100, 80, 20, 50 C for the top, bottom, left, and right boundaries, respectively, a 5050 grid, a tolerance of 10-4 , and a 5000 iteration maximum. Hand in softcopy of all source code through blackboard.