# Partial differential equations

In engineering sciences, partial differential equations play an important and central role. For example, the temperature of a metal plate can be expressed as a partial differential equation if the temperature on the boundaries is known. This is called a boundary value problem. Usually, it is not easy to solve these problems. Analytical solutions exist only in very special cases. But there are some more or less ”good” numerical ways to solve boundary value problems. We now will look at one method which works with finite difference approximations for the derivatives of a function. For this approach, we do not look at an analytical function u(x) but we are only interested in the values of u at a finite set of discrete points xi : ui = u(xi). The distance between two adjacent points, xi and xi+1, is constant: h = xi+1 − xi (cf. Figure 1). The finite difference approximation of a first derivative of the function u(x) is Figure 1: u(x) at some discrete points xi ∂ u = 1 (−ui−1 +ui+1) (1) ∂x i 2h The second derivative is approximated by ∂2 1 ∂x2u = h2(ui−1 −2ui +ui+1) (2) i This approximation works with 2-dimensional functions u(x, y) as well. For simplicity we only work on square problems, i.e. (x, y) is element of [0, 1] × [0, 1]. Again, the area of the function is discretized in a similar way: xi+1 − xi = yi+1 − yi = h = n1 , for some integer n ≥ 2. We only look at the values of u(x,y) at the discrete points Pk = (xi,yj) : ui,j = u(Pk). With this discretization, we have a function ui,j as shown in Figure 2: On the boundary, u(xi,yj) is given by 4 known functions: u(xi, 0) = u(xi, 1) = u(0, yj ) = u(1, yj ) = b1(xi) b2(xi) b3(yj ) b4(yj ) (3) The points Pk cover cretization area, i.e. the They are numbered from left to right and from top to bottom like English text. What we now want to do is to solve the poisson- equation in the area [0, 1] × [0, 1]: ∂2 ∂2 ∂x2 u + ∂y2 u = f(x, y) (4) Figure 2 : Function ui,j in the discretization with the above boundary conditions. area the inner points of the dis- area without the boundary.

2/3 f(x,y) is a given 2-dimensional function. With equation (2) and the above discretization, the poisson-equation can be approximated at 1 (ui−1,j + ui,j−1 + ui+1,j + ui,j+1 − 4ui,j = fi,j, (5) h2 where fi,j is the function f(x,y), evaluated at the discrete points (xi,yj). points:  1010 000 1 −4 1♢u=0 1 0♢f (6a) 010 000 Formula (5) can be written in a more readable form, depending on the position of the discrete h2 A similar equation, which we will use as an example below, is:  1102 050 0 −4 0♢u=6 0 7♢f (6b) 304 080 h2 We call the 3×3 matrix on the left hand side v and the 3×3 matrix on the right hand side g. Now, equation (6b) can be formulated in every point of the discrete area of Figure 2:  1 h2 2 2 1 3 4  P : 1 (b (0)+2b (2h)−4P +3b (h)+4P )=5f(h,1)+6f(0,2h)+7f(2h,2h)+8f(h,h) P : 1 (b (h)+2b (1)−4P +3P +4b (h))=5f(2h,1)+6f(h,2h)+7f(1,2h)+8f(2h,h) 2 h2 2 2 2 3 4 P : h1 (b (2h)+2P −4P +3b (0)+4b (2h))=5f(h,2h)+6f(0,h)+7f(2h,h)+8f(h,0)  3232311  P : 1 (P +2b (2h)−4P +3b (h)+4b (1))=5f(2h,2h)+6f(h,h)+7f(1,h)+8f(2h,0) (7) By rearranging and adding the terms on each line, the linear equation system can be formulated as: az = b (8) where a is a 4 × 4 matrix and b is a vector with 4 elements. Vector z represents the unknown values of u(x, y) at the points P1, P2, P3 and P4. You are to write a program that creates the linear equation system (7) in the form (8) for any two matrices v and g (6). As input, the two matrices v and g and the functions b1,b2,b3,b4 , and f are given. Also, a parameter n is given as the number of discretization intervals. Thus, h = n1 . As the result, your program should calculate the matrix a and the vector b. For this more general case, there are (n − 1)2 inner points and a and b must be sized accordingly. Input The input file consists of m tests. The number m is given in the first line of the file. The first line of each test contains the number n which gives the number of discretizations intervals as defined above. You may assume that 2 ≤ n ≤ 30. Then the 3 × 3 matrices v and g follow. The following four lines contain the functions b1, b2, b3 and b4, each given as a vector of order n + 1, containing the values for 0, h, 2h, ..., 1. Finally, the function f is given as a n + 1 by n + 1 matrix. Like the vectors before, it contains the values for x, y = 0, h, 2h, . . . , 1. Each row contains from left to right the function values for increasing x values while each column contains from top to bottom the function values for decreasing y values. A vector occupies one line. Its values are given in ascending order, separated by a space. A n by n matrix occupies n lines. Its rows are given in ascending order as vectors, which occupy one line each. All values found in the input file are integer values. 4 h2 1 4 4 1 1 and (7) is a linear equation system for the values of u(x,y) at the points P1,P2,P3 and P4.

3/3 Output For each test found in the input file, your program should output the matrices a and b. Matrix a is a (n − 1)2 × (n − 1)2 matrix (the discretization area (cf. Figure 2) contains (n − 1)2 inner points, which are unknown). The vector b is of order (n − 1)2. They should be output in the same format as the vectors and matrices in the input file. Your output should only contain integer values. Note that the expression 1 yields an integer number and that all other calculations can also be done using integer h2 numbers. Sample Input 1 3 102 0 -4 0 304 050 607 080 3456 0123 3210 6543 1111 2222 3333 4444 Sample Output -36 0 0 36 0 -36 27 0 0 18 -36 0 9 0 0 -36 -35 -188 -189 -315