Areg Hayrapetian (#53) Problem Set #6 18.085 3.3: 6, 8, 10, 16, 27 6) v1 = (y 2 , x2 ) is not a gradient of any function because the curl of v1 can be shown to be 0. ∇ × v1 = ∂(y 2 ) ∂y ∇× ∂(x2 ) ∂x = 2y − 2x 6= 0 for all x, y. However, v2 2 ) ∂(2xy) v2 = ∂(y = 2y − 2y = 0 for all x, y. ∂y − ∂x − = (y 2 , 2xy) is a gradient of the function u(x, y) because ∂u = y2 ∂x u(x, y) = xy 2 + f1 (y) ∂u = 2xy ∂y u(x, y) = xy 2 + f2 (x) So, f1 (y) = f2 (x) = C, but the constant C can be arbitrary set. By letting C = 0, we find u(x, y) = xy 2 . 8) w = ∇u with u(x, y) = x2 in the square S = −1 < x, y < 1. w = ∇u = (2x, 0), and ∇ · w = 2. ZZ Z Divergence Theorem: ∇ · (∇u) dx dy = n · ∇u ds S C ZZ Z 1 Z 1 ∇ · (∇u) dx dy = Z Z 1 n · ∇u ds = Z 1 ex · (2, 0) dy + −1 Z 1 C = 2 dx dy = 8 −1 S −1 Z ey · (2x, 0) dx + −1 Z Z 1 (−ex ) · (−2, 0) dy + −1 (−ey ) · (2x, 0) dx −1 1 2 dy + 0 + −1 1 2 dy + 0 = 4 + 4 = 8 −1 10) v = (2xy, x2 − y 2 ) = ∇u. ∂u ∂s = = 2xy ∂y ∂x u(x, y) = x2 y + f1 (y) s(x, y) = xy 2 + f3 (x) − ∂s ∂u = = x2 − y 2 ∂x ∂y 1 u(x, y) = x2 y − y 3 + f2 (x) 3 1 s(x, y) = xy 2 − x3 + f4 (y) 3 Equating the two equations for u(x, y) and s(x, y), and letting arbitrary constants be 0, we find f1 (y) = − 31 y 3 , 1 1 f2 (x) = 0, f3 (x) = − 13 x3 , and f4 (x) = 0. So, u(x, y) = x2 y − y 3 and s(x, y) = xy 2 − x3 . The 3 3 equipotentials (u(x, y) = c) and streamlines (s(x, y) = c) are shown in the figure below for flow into a 30◦ √ wedge. To verify that flow does not pass through the upper boundary, we can show that v ·n = 0 for y = x/ 3. √ √ √ 2 ◦ ◦ 2 On the upper boundary, n = (− sin(30 ), cos(30 )) = (−1/2, 3/2), and v = (2x(x/ 3), x − (x/ 3) ) = √ x2 (2/ 3, 2/3). √ 1 2 3 22 v · n = − x2 √ + x 2 2 3 3 2 2 x x = −√ + √ = 0 X 3 3 11/3/10 Page 1 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 ° Equipotentials and Streamlines for Flow into a 30 Wedge 3 Streamline Curves Equipotential Curves Boundary 2.5 y 2 1.5 1 0.5 0 0 0.5 1 1.5 2 2.5 x 3 3.5 4 4.5 5 16) v(x, y, z) is a rotation field given by the cross product of axis A = (a1 , a2 , a3 ) with R = (x, y, z), v = A × R = (v1 , v2 , v3 ) = (a2 z − a3 y, a3 x − a1 z, a1 y − a2 x). ∂v3 ∂v2 ∂v1 ∂v3 ∂v2 ∂v1 ∇×v = − , − , − ∂y ∂z ∂z ∂x ∂x ∂y = (a1 + a1 , a2 + a2 , a3 + a3 ) = 2(a1 , a2 , a3 ) = 2A ∂v1 ∂v2 ∂v3 + + =0+0+0=0 ∂x ∂y ∂z v cannot be a gradient field, because if it were, it would contradict with the curl calculated above: ∇ × v = ∇ × (∇u) = 0 6= 2A, using the vector identity that the curl of a gradient is 0. However, v is a vorticity field because the flow is solenoidal (∇ · v = 0). The stream function S = (s1 (x, y, z), s2 (x, y, z), s3 (x, y, z)) can be calculated. We can try the following equations and check to see if they satisfy ∇ × S = v: ∇·v = s1 = a2 xy + a3 xz s2 = a1 xy + a3 yz s3 = a1 xz + a2 yz ∂s3 ∂s2 − = a2 z − a3 y ∂y ∂z ∂s1 ∂s3 − = a3 x − a1 z ∂z ∂x ∂s2 ∂s1 − = a1 y − a2 x ∂x ∂y X X X So, S = (a2 xy + a3 xz, a1 xy + a3 yz, a1 xz + a2 yz) . If a1 and a2 were selected to be 0, the rotation axis A would equal (0, 0, a3 ), and the rotation would only occur in the x − y plane. Substituting a1 = a2 = 0 into the equation for v, it can be seen that the z-component of the velocity would disappear, and so v would be a plane field. 27) We would like to show that M 2 is a diagonal matrix equal to −∇2 I, where ∇2 = ∂ 2/∂x2 + ∂ 2/∂y2 + ∂ 2/∂z2 and M is given by 0 −∂/∂z ∂/∂y ∂/∂x −1 ∂/∂z C A curl grad 0 −∂/∂x ∂/∂y M= = = ∂/∂z −∂/∂y ∂/∂x −div 0 0 AT 0 −∂/∂x −∂/∂y −∂/∂z 0 11/3/10 Page 2 of 10 Areg Hayrapetian (#53) Problem Set #6 −1 −1 −1 C A C −1 A (C C + AAT ) C −1 A M2 = = AT 0 AT 0 AT C −1 0 ∂ ∂ ∂ ∂ 0 − /∂z ∂/∂y ∂/∂x 0 − /∂z /∂y /∂x ∂ ∂/∂z 0 −∂/∂x ∂/∂y 0 −∂/∂x ∂/∂y /∂z = ∂ ∂/∂z ∂ ∂ −∂/∂y ∂/∂x − /∂y /∂x 0 0 /∂z −∂/∂x −∂/∂y −∂/∂z 0 −∂/∂x −∂/∂y −∂/∂z 0 2 2 2 2 ∂ ∂ ∂ ∂ ∂2 ∂2 −∇2 − ∂x∂y − ∂x∂z − ∂y∂z + ∂y∂z ∂x∂y ∂x∂z 2 2 2 ∂ ∂2 ∂ ∂2 ∂ ∂2 − ∂x∂y −∇2 − − ∂x∂y ∂y∂z ∂y∂z ∂x∂z ∂x∂z = −∇2 I 2 = ∂2 ∂2 ∂2 ∂2 ∂2 ∂ 2 −∇ − ∂x∂y + ∂x∂y ∂x∂z − ∂x∂z ∂y∂z − ∂y∂z ∂2 ∂2 ∂2 ∂2 ∂2 ∂2 2 − ∂y∂z + ∂y∂z − − + −∇ ∂x∂z ∂x∂z ∂x∂y ∂x∂y 18.085 From the above calculation, we can see three useful identities. First, the 3 × 1 vector C −1 A equals 0, which means that the curl of a gradient is 0. Second, the 1 × 3 vector AT C −1 equals 0, which means that the divergence of a curl is 0. Finally, the 3 × 3 matrix (C −1 C −1 + AAT ), which is the curl of a curl added with the gradient of a divergence, is equal to −∇2 I. 3.4: 2, 4, 5, 6, 17, and Extra Problem 2) We want to solve Poisson’s equation uxx + uyy = 4 with the boundary condition u = 0 on the circle x2 +y 2 = 1. Trying the function u(x, y) = x2 +y 2 −1, we find that it satisfies Poisson’s equation, uxx +uyy = 2 + 2 = 4, and it satisfies the boundary condition, on the circle u = 1 − 1 = 0. So, u(x, y) = x2 + y 2 − 1 . 4) u = r cos θ + r−1 cos θ solves Laplace’s equation ∂2u ∂r 2 + 1 ∂u r ∂r ∂2u 2 = 3 cos θ ∂r2 r 1 ∂u = cos θ − 2 cos θ ∂r r + 1 ∂2u r 2 ∂θ 2 = 0, which is verified below. ∂2u 1 = −r cos θ − cos θ ∂θ2 r ∂ 2 u 1 ∂u 1 ∂2u 2 1 1 1 1 + + = 3 cos θ + cos θ − 3 cos θ − cos θ − 3 cos θ = 0 2 2 2 ∂r r ∂r r ∂θ r r r r r u can be expressed in terms of x and y through the change in coordinates r2 = x2 + y 2 and r cos θ = x. u = r cos θ + r−1 cos θ = r cos θ + r cos θ x = x+ 2 = u(x, y) r2 x + y2 Then, v = (ux , uy ) is y 2 − x2 ∂u 1(x2 + y 2 ) − x(2x) =1+ = 1 + ∂x (x2 + y 2 )2 (x2 + y 2 )2 ∂u 0 − x(2y) 2xy uy = = 2 =− 2 ∂y (x + y 2 )2 (x + y 2 )2 ux = v= 2xy (y 2 − x2 ) 1+ 2 , − (x + y 2 )2 (x2 + y 2 )2 This is the velocity field for flow past a unit circle x2 + y 2 = 1. Therefore, the flow into or out of the circle should be 0, or v p · n = 0 on the circle. This can be verified by first finding the normal vector on the unit circle, n = (x, y)/ x2 + y 2 , and taking the dot product v · n given that x2 + y 2 = 1 since it is evaluated on the circle: 1 x(y 2 − x2 ) 2xy 2 v·n= p x+ 2 − 2 = x + x(y 2 − x2 ) − 2xy 2 (x + y 2 )2 (x + y 2 )2 x2 + y 2 = x + xy 2 − x3 − 2xy 2 = x − x3 − xy 2 = x − x(x2 + y 2 ) = x − x = 0 11/3/10 Page 3 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 5) u = ln(r) and U = ln(r2 ) both satisfy Laplace’s equation. In fact, U = ln(r2 ) = 2 ln(r) = 2u. So, it is enough to show that u satisfies Laplace’s equation, because then U automatically will as well. This is shown below. ∂ 1 1 ∂ 2 u 1 ∂u 1 ∂2u 1 1 1 + = + + +0=− 2 + 2 =0 ∂r2 r ∂r r2 ∂θ2 ∂r r r r r r u and U would have been obtained by selecting the complex functions f (z) = ln(z) = u + is and F (z) = ln(z 2 ) = U + iS where z = reiθ . f (z) = u + is = (ln(r)) + i(θ) and F (z) = U + iS = (ln(r2 )) + i(2θ) . The Cauchy-Riemann equations require that ∂U/∂x = ∂S/∂y and ∂U/∂y = −∂S/∂x. This is verified below. U (x, y) = ln(x2 + y 2 ) S(x, y) = 2 tan−1 y x ∂U 2x = 2 ∂x (x + y 2 ) y 2 −1 1 ∂S =2 1+ ∂y x x 2x = 2 (x + y 2 ) ∂U 2y = 2 ∂y (x + y 2 ) y 2 −1 y ∂S − 2 =2 1+ ∂x x x 2y =− 2 (x + y 2 ) 6) For each r, the points Z, defined below, lie on an ellipse. Z≡ 1 1 (z + z −1 ) = (reiθ + r−1 e−iθ ) = 2 2 r + r−1 2 cos θ + i r − r−1 2 4X 2 4Y 2 + = cos2 θ + sin2 θ = 1 −1 2 (r + r ) (r − r−1 )2 sin θ = X + iY (Equation of Ellipse) We would like to see why the circles |z| = R and |z| = R−1 produce the same ellipse when mapped to Z. For |z| = R, r = R in the expression z = reiθ , and so we must substitute r = R into the general equation of the ellipse above to get the equation of the ellipse corresponding to this particular circle: With r = R: 4X 2 4Y 2 + =1 −1 2 (r + r ) (r − r−1 )2 −−−−−→ 4X 2 4Y 2 + =1 −1 2 (R + R ) (R − R−1 )2 For |z| = R−1 , r = R−1 in the expression z = reiθ , and so we must substitute r = R−1 into the general equation of the ellipse above to get the equation of the ellipse corresponding to this particular circle: With r = R−1 : 4X 2 4Y 2 + =1 −1 2 (r + r ) (r − r−1 )2 −−−−−→ −−−−−→ 4X 2 4Y 2 + −1 =1 2 + R) (R − R)2 4X 2 4Y 2 + =1 −1 2 (R + R ) (R − R−1 )2 (R−1 The resulting equations for the ellipses for both cases are identical, therefore the circles |z| = R and |z| = R−1 both produce the same ellipse when mapped to Z. 17) We want to verify that uk (x, y) = sin(πkx) sinh(πky)/ sinh(πk) for k = 1, 2, ... solves Laplace’s equation and satisfies the boundary conditions on a unit square u(x, 1) = sin(πkx) and u(x, 0) = u(0, y) = u(1, y) = 0. First, we check if uk (x, y) satisfies Laplace’s equation: ∂ 2 uk sinh(πky) = −π 2 k 2 sin(πkx) 2 ∂x sinh(πk) ∂ 2 uk sin(πkx) 2 2 = π k sinh(πkx) 2 ∂y sinh(πk) ∂ 2 uk ∂ 2 uk π2 k2 + = [− sin(πkx) sinh(πky) + sin(πkx) sinh(πkx)] = 0 ∂x2 ∂y 2 sinh(πk) 11/3/10 X Page 4 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 Then, we check if uk (x, y) satisfies the boundary conditions: sin(0) sinh(πky) =0X sinh(πk) sin(πkx) sinh(0) uk (x, 0) = =0X sinh(πk) uk (0, y) = sin(πk) sinh(πky) =0X sinh(πk) sin(πkx)sinh(πk) uk (x, 1) = = sin(πkx) sinh(πk) uk (1, y) = X Extra Problem) Solutions to Laplace’s Equation ∇2 Ψ = 0 come in pairs ui and si . Four of these pairs are listed below for both 2D Cartesian coordinates and 2D Polar coordinates. 2 2 In 2D Cartesian coordinates, ui (x, y) and si (x, y) both satisfying ∂∂xΨ2 + ∂∂yΨ2 = 0: u1 (x, y) = x s1 (x, y) = y 2 u2 (x, y) = x − y 3 2 u3 (x, y) = x − 3xy s2 (x, y) = 2xy 2 s3 (x, y) = 3x2 y − y 3 u4 (x, y) = x4 − 6x2 y 2 + y 4 In 2D Polar coordinates, ui (r, θ) and si (r, θ) both satisfying 11/3/10 s4 (x, y) = 4x3 y − 4xy 3 ∂2Ψ ∂r 2 + 1 ∂Ψ r ∂r + 1 ∂2Ψ r 2 ∂θ 2 = 0: u1 (r, θ) = r cos(θ) s1 (r, θ) = r sin(θ) u2 (r, θ) = r2 cos(2θ) s2 (r, θ) = r2 sin(2θ) u3 (r, θ) = r3 cos(3θ) s3 (r, θ) = r3 sin(3θ) u4 (r, θ) = r4 cos(4θ) s4 (r, θ) = r4 sin(4θ) Page 5 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 MATLAB Assignment 2 2 We would like to numerically solve Laplace’s equation ∂∂xu2 + ∂∂yu2 = 0 with the boundary conditions u(1, y) = y, u(x, 1) = x, (w · n)|x=0 = −y, and (w · n)|y=0 = −x. Taking w to be w = ∇u = (∂u/∂x, ∂u/∂y), and n to be the outward facing normal, the last two boundary conditions can be rewritten in a simpler form. At x = 0, the normal vector is n = −ex , and the boundary condition can be written as ∂u ∂x x=0 = y. At y = 0, the normal vector is n = −ey , and the boundary condition can be written as ∂u ∂y y=0 = x. The MATLAB function laplace was written to numerically solve Laplace’s equation on an N by N 2D grid, bounded by 0 < x < 1 and 0 < y < 1, using arbitrarily specified boundary conditions. The code is displayed in Listing 1, and includes comments describing how to use the function. We will use this function 1 , and the boundary conditions with a grid size of N = 10, meaning a spacing between nodes of h = N1+1 = 11 2 described in the above paragraph. The code generates two matrices K (N × N 2 ) and F (N 2 × 1) which are used as part of the matrix equation KU = F that solves for U containing the values of u(x, y) at the nodes. The N 2 × 1 vector U is ordered to go across the columns of a particular row first, before moving on to the next row. The first component of the vector is the value for u(h, h) and the last component of the vector is the value for u(N h, N h). Although the right hand side of the Laplace equation is 0, the non-zero vector F on the right hand side of this matrix equation is a result of the imposed boundary conditions, which change the matrix K and cause terms to be moved to the right hand side. To get the matrices K and F we call the laplace function in MATLAB as follows: >> [K, F] = laplace(10, 1, @(y) y, 0, @(y) y, 1, @(x) x, 0, @(x) x); We are interested in learning more about the matrix K. First, we can check if it is symmetric. We can use the MATLAB command max(max(abs(A))) to see what the component in matrix A with the maximum magnitude is. If the component with maximum magnitude in a matrix is 0, then that means the whole matrix contains only zeros. >> max(max(abs(K - K’))) ans = 0 Since, K − K T = 0, K must be symmetric. Now, we would like to know whether K is positive definite. A quick way to check this would be to find the pivots of K and see what their signs are. The command below LU factorizes K and then grabs the diagonal of the upper triangular matrix U which contains the pivots, and then checks to see if there is any pivot that is non-negative. >> [L,U] = lu(K); d = diag(U); >> find(d >= 0, length(d)) ans = Empty matrix: 0-by-1 The answer is the empty matrix, which means that there are no non-negative pivots. This means that all the pivots are negative, and thus the matrix K is actually negative definite. The main diagonal of the matrix K is printed below: >> diag(K) ans = (-2, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -4, -4, -4, -4, -4, -4, -4, -4, -4 ) 11/3/10 Page 6 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 K is banded around the main diagonal, and we would like to find the bandwidth w, which is defined as the minimum w such that Kij = 0 if |i − j| > w. We can find w using the simple script below. >> Kmod = K - diag(diag(K)); >> for i = 1:50 if max(max(abs(Kmod))) == 0 break; end Kmod = Kmod - diag(diag(K,i),i); Kmod = Kmod - diag(diag(K,-i),-i); end >> w = i w = 11 The script starts from the main diagonal and begins zeroing out the diagonals of the matrix K as it expands outward from the main diagonal. At each iteration it checks to see if the matrix is equal to the zero matrix yet. As soon as the matrix is 0, it breaks out of the loop and its index i is equal to the bandwidth w. The script calculated that the bandwidth of the matrix K is w = 11. We now want to find the largest and smallest eigenvalues of K. Note that since K is negative definite, the smallest eigenvalue will be a negative number with the largest magnitude, and the largest eigenvalue will be a negative number with the smallest magnitude. The eigenvalues can be found by using the MATLAB command eig to do an eigen decomposition of K, and then extracting the largest and smallest eigenvalues from the diagonal eigenvalue matrix. >> [V, E] = eig(K); >> min(diag(E)) ans = -7.8223 >> max(diag(E)) ans = -0.0447 So, the minimum eigenvalue of K is equal to -7.8223, and the maximum eigenvalue of K is -0.0447. We can now take the inverse of the K matrix and print out the first column v of K −1 . >> Kinv = inv(K); >> v = Kinv(:,1) v = (-1.75563, -1.25563, -0.95319, -0.74342, -0.58399, -0.45499, -0.34567, -0.24948, -1.05807, -0.86051, -0.69309, -0.55356, -0.43531, -0.33255, -0.24082, -0.15664, -0.73769, -0.61487, -0.50185, -0.40013, -0.30840, -0.22461, -0.14661, -0.07238, -0.52686, -0.43884, -0.35496, -0.27633, -0.20260, -0.13281, -0.06573, -0.58399, -0.37169, -0.30454, -0.23934, -0.17666, -0.11632, -0.05771, -0.45499, -0.43531, -0.25218, -0.19982, -0.14838, -0.09810, -0.04878, -0.34567, -0.33255, -0.30840, -0.15939, -0.11895, -0.07891, -0.03932, -0.24948, -0.24082, -0.22461, -0.20260, -0.08912, -0.05928, -0.02959, -0.16194, -0.15664, -0.14661, -0.13281, -0.11632, -0.03951, -0.01974, -0.07971, -0.07718, -0.07238, -0.06573, -0.05771, -0.04878, -0.00987 ) -0.16194, -0.07718, -0.74342, -0.55356, -0.40013, -0.27633, -0.17666, -0.09810, -0.03932, -0.07971, -0.95319, -0.69309, -0.50185, -0.35496, -0.23934, -0.14838, -0.07891, -0.02959, -1.25563, -0.86051, -0.61487, -0.43884, -0.30454, -0.19982, -0.11895, -0.05928, -0.01974, The ratio of the first component to the last component, which is calculated using v(1)/v(100), is about 177.8556. 11/3/10 Page 7 of 10 Areg Hayrapetian (#53) Problem Set #6 18.085 Finally, we can solve the matrix equation KU = F and print out the solution vector U . >> U = K\F U = (0.00826, 0.01653, 0.02479, 0.03306, 0.04132, 0.04959, 0.05785, 0.06612, 0.07438, 0.08264, 0.04959, 0.06612, 0.08264, 0.09917, 0.11570, 0.13223, 0.14876, 0.16529, 0.02479, 0.04959, 0.12397, 0.14876, 0.17355, 0.19835, 0.22314, 0.24793, 0.03306, 0.06612, 0.09917, 0.13223, 0.23140, 0.26446, 0.29752, 0.33058, 0.04132, 0.08264, 0.12397, 0.16529, 0.20661, 0.24793, 0.37190, 0.41322, 0.04959, 0.09917, 0.14876, 0.19835, 0.24793, 0.29752, 0.34711, 0.39669, 0.05785, 0.11570, 0.17355, 0.23140, 0.28926, 0.34711, 0.40496, 0.46281, 0.52066, 0.57851, 0.19835, 0.26446, 0.33058, 0.39669, 0.46281, 0.52893, 0.59504, 0.66116, 0.07438, 0.14876, 0.37190, 0.44628, 0.52066, 0.59504, 0.66942, 0.74380, 0.08264, 0.16529, 0.24793, 0.33058, 0.57851, 0.66116, 0.74380, 0.82645 ) 0.01653, 0.07438, 0.16529, 0.28926, 0.44628, 0.06612, 0.22314, 0.41322, 0.03306, 0.09917, 0.19835, 0.33058, 0.49587, 0.13223, 0.29752, 0.49587, The ratio of the last component of U , the value of the numerical solution at u(10h, 10h), to the first component of U , the value of the numerical solution at u(h, h), can be calculated using U(100)/U(1), and it is equal to 100. Listing 1: MATLAB function that creates the matrices required to numerically solve 2D Laplace’s Equation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 function [ K, F ] = l a p l a c e ( N, B x0 type , B x0 func , . . . B x1 type , B x1 func , . . . B y0 type , B y0 func , . . . B y1 type , B y 1 f u n c ) %LAPLACE G e n e r a t e s m a t r i c e s K and F t h a t w i l l s o l v e Laplace ’ s E q u a t i o n % [ K, F ] = l a p l a c e ( N, B x 0 t y p e , B x 0 f u n c , . . . % x=0 boundary % B x 1 t y p e , B x 1 f u n c , . . . % x=1 boundary % B y 0 t y p e , B y 0 f u nc , . . . % y=0 boundary % B y1 type , B y1 func ) % y=1 boundary % g e n e r a t e s t h e m a t r i c e s K and F , t h a t s o l v e 2D Laplace ’ s E q u a t i o n w i t h % m a t r i x e q u a t i o n KU=F , where U would c o n t a i n t h e v a l u e s o f u a t a l l t h e % nodes i n t h e 2D g r i d . K i s an Nˆ2 by Nˆ2 m a t r i x and F and U a r e b o t h % Nˆ2 by 1 v e c t o r s . The e l e m e n t s i n U a r e o r d e r e d such t h a t a l l t h e node % v a l u e s i n a row y = h i appear i n o r d e r o f columns x = h j b e f o r e t h e node % v a l u e s i n t h e n e x t row y = h ( i +1) appear . The u s e r must s p e c i f y t h e % s i z e o f t h e g r i d N which d e t e r m i n e s t h e s p a c i n g h = 1/(N+1) b e t w e e n each % node i n t h e 2D g r i d r a n g i n g from 0 < x < 1 and 0 < y < 1 . The u s e r must % a l s o s p e c i f y t h e boundary c o n d i t i o n s on each o f t h e 4 e d g e s o f t h e g r i d . % Arguments B ( a ) (m) t y p e s p e c i f y t h e t y p e o f t h e boundary c o n d i t i o n % on t h e boundary a ( e i t h e r x or y ) = m ( e i t h e r 0 or 1 ) , and t h e v a l u e o f % t h i s argument must e i t h e r be 0 ( f o r a D i r i c h l e t boundary c o n d i t i o n ) or % 1 ( f o r a Neumann boundary c o n d i t i o n ) . B ( a ) (m) f u n c i s a f u n c t i o n t h a t % t a k e s one v a r i a b l e , t h e l o c a t i o n i n t h e d i r e c t i o n o f t h e a x i s b t h a t i s % not a , and s p e c i f i e s t h e r i g h t hand s i d e o f t h e boundary c o n d i t i o n f o r % t h e c o r r e s p o n d i n g boundary . For D i r i c h l e t boundary c o n d i t i o n s , % u ( a t a = m) = B ( a ) (m) f u n c ( b ) . For Neumann boundary c o n d i t i o n s , % du/ da ( a t a = m) = B ( a ) (m) f u n c ( b ) . % Example : S o l v i n g Laplace ’ s e q u a t i o n w i t h t h e boundary c o n d i t i o n s % u ( 0 , y ) = u ( 1 , y ) = 0 , u ( x , 0 ) = x , du/ dy ( x , 1 ) = 1−x . % [K, F ] = l a p l a c e ( 1 0 , 0 , @( y ) 0 , 0 , @( y ) 0 , . . . % 0 , @( x ) x , 1 , @( x ) 1−x ) ; % U = K\F ; 11/3/10 Page 8 of 10 Areg Hayrapetian (#53) 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 Problem Set #6 18.085 % Get i n d e x from row i , column j indx = @( i , j ) N* ( i −1) + j ; % S p a c i n g b e t w e e n nodes i n 2D g r i d h = 1 / (N+1); % I n i t i a l i z e vectors F = zeros (N*N, 1 ) ; F1 = zeros (N*N, 1 ) ; F2 = zeros (N*N, 1 ) ; % I n i t i a l l y s e t up K m a t r i x K = −4* eye (N*N ) ; f o r i = 1 :N f o r j = 1 :N m = indx ( i , j ) ; if i > 1 K(m, indx ( i −1, j ) ) = end if i < N K(m, indx ( i +1, j ) ) = end if j > 1 K(m, indx ( i , j −1)) = end if j < N K(m, indx ( i , j +1)) = end end end 1; 1; 1; 1; % P r o c e s s x=0 boundary f o r i = 1 :N m = indx ( i , 1 ) ; i f B x 0 t y p e == 1 K(m,m) = K(m,m) + 1 ; F1 (m) = F1 (m) + B x 0 f u n c ( h * i ) ; else F2 (m) = F2 (m) − B x 0 f u n c ( h * i ) ; end end % P r o c e s s x=1 boundary f o r i = 1 :N m = indx ( i ,N ) ; i f B x 1 t y p e == 1 K(m,m) = K(m,m) + 1 ; F1 (m) = F1 (m) − B x 1 f u n c ( h * i ) ; else F2 (m) = F2 (m) − B x 1 f u n c ( h * i ) ; end end 11/3/10 Page 9 of 10 Areg Hayrapetian (#53) 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 Problem Set #6 18.085 % P r o c e s s y=0 boundary f o r j = 1 :N m = indx ( 1 , j ) ; i f B y 0 t y p e == 1 K(m,m) = K(m,m) + 1 ; F1 (m) = F1 (m) + B y 0 f u n c ( h * j ) ; else F2 (m) = F2 (m) − B y 0 f u n c ( h * j ) ; end end % P r o c e s s y=1 boundary f o r j = 1 :N m = indx (N, j ) ; i f B y 1 t y p e == 1 K(m,m) = K(m,m) + 1 ; F1 (m) = F1 (m) − B y 1 f u n c ( h * j ) ; else F2 (m) = F2 (m) − B y 1 f u n c ( h * j ) ; end end % F i n a l i z e v e c t o r s and o u t p u t v a r i a b l e s F1 = F1 * h ; F2 = F2 ; F = F1 + F2 ; 11/3/10 Page 10 of 10