Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Code for make_A(k): def make_A(k): Create the matrix for the temperature problem on a k-by-k grid. Parameters: k: number of grid points in each dimension.
Code for make_A(k):
def make_A(k): """Create the matrix for the temperature problem on a k-by-k grid. Parameters: k: number of grid points in each dimension. Outputs: A: the sparse k**2-by-k**2 matrix representing the finite difference approximation to Poisson's equation. """ # First make a list with one triple (row, column, value) for each nonzero element of A triples = [] for i in range(k): for j in range(k): # what row of the matrix is grid point (i,j)? row = j + i * k # the diagonal element in this row triples.append((row, row, 4.0)) # connect to left grid neighbor if j > 0: triples.append((row, row - 1, -1.0)) # ... right neighbor if j 0: triples.append((row, row - k, -1.0)) # ... neighbor below if i 1. The temperature problem models our cabin in the woods in two dimensions, but most modern scientific simulations are done in three dimensions. Here you will create the matrix that corresponds to a 3-D version of the temperature problem. The "cabin" is now the unit cube. As before, we will discretize the interior by dividing it into k points in each dimension, but now there are k3 points in all rather than k2. The partial differential equation still leads to the approximation that the temperature at any given point is the average of the temperatures at the neighboring points, but now there are 6 neighbors, with 2 in each dimension. Using the routine make A (k) from Temperature.ipynb as a model, write a routine make A.3D (k) that returns the k-by-k3 matrix A for the 3D version of the temperature problem. This matrix expresses the fact that, in a 3D k-by-k-by-k grid, each interior point has a temperature that is the average of its 6 neighbors (left, right, up, down, in, out). The diagonal elements of A are all equal to 6, and the off-diagonal elements are either 0 or -1. Most of the rows of A have 7 nonzeros. Here below, for debugging, is the correct matrix for k 2. I converted it to dense for printing-you should also print it out as sparse, and indeed for k > 2 it's going to be too large to see what's going on in the dense matrix anyway [In:] k=2 A - make A 3D (k) print('k:', k) print('dimensions: ', A.shape) print('nonzeros:', A.size) #print as sparse matrix:'); print (A) print('A as dense matrix:'); print (A.todense )) [Out:] k: 2 dimensions: (8, 8) nonzeros: 32 A as dense matrix: 1. The temperature problem models our cabin in the woods in two dimensions, but most modern scientific simulations are done in three dimensions. Here you will create the matrix that corresponds to a 3-D version of the temperature problem. The "cabin" is now the unit cube. As before, we will discretize the interior by dividing it into k points in each dimension, but now there are k3 points in all rather than k2. The partial differential equation still leads to the approximation that the temperature at any given point is the average of the temperatures at the neighboring points, but now there are 6 neighbors, with 2 in each dimension. Using the routine make A (k) from Temperature.ipynb as a model, write a routine make A.3D (k) that returns the k-by-k3 matrix A for the 3D version of the temperature problem. This matrix expresses the fact that, in a 3D k-by-k-by-k grid, each interior point has a temperature that is the average of its 6 neighbors (left, right, up, down, in, out). The diagonal elements of A are all equal to 6, and the off-diagonal elements are either 0 or -1. Most of the rows of A have 7 nonzeros. Here below, for debugging, is the correct matrix for k 2. I converted it to dense for printing-you should also print it out as sparse, and indeed for k > 2 it's going to be too large to see what's going on in the dense matrix anyway [In:] k=2 A - make A 3D (k) print('k:', k) print('dimensions: ', A.shape) print('nonzeros:', A.size) #print as sparse matrix:'); print (A) print('A as dense matrix:'); print (A.todense )) [Out:] k: 2 dimensions: (8, 8) nonzeros: 32 A as dense matrix
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started