College of Engineering and Computer Science Mechanical Engineering Department Mechanical Engineering 501A Seminar in Engineering Analysis Fall 2004 Number: 17472 Instructor: Larry Caretto November 30 Homework Solutions Hoffman, page 494, problem 64 – Solve problem 58 by the second order equilibrium method. We first convert the differential equation into a finite difference equation, using second-order expressions for the derivatives: dy/dx = (yn+1 – yn-1)/(2h) and d2y/dx2 = (yn+1 + yn-1 – 2yn)/h2. This gives the following finite difference equation. y n 1 y n 1 2 y n y y n 1 1 xn n 1 1 xn y n 1 2 2h h Multiplying this equation by h2 and rearranging gives the following result. 1 xn 1 xn 2 2 1 h y n1 2 1 xn h y n 1 h y n1 h 2 2 This is a set of equations that we can use to solve for the y values. From the initial condition that y(0) = 1 we have y0 = 1 so that the initial equation in the set becomes. 2 1 x h y 2 1 1 1 x1 1 x1 1 x1 2 2 1 h y 2 h 1 h y 0 h 1 h 2 2 2 We can use the second-order backwards difference equation1 to obtain a finite-difference equivalent of the zero gradient boundary condition at x = 1. This is the last grid node, corresponding to n = N. dy dx x xN y N 2 4 y N 1 3 y N 0 2h y N 2 4 y N 1 3 y N 2 With h = 0.125, the boundary node at x = 1 is N = 8. We have almost a tridiagonal system, but in such a system the last equation can only have two unknowns, yN-1, and yN. We have two equations that involve the last three unknowns. In addition to the zero gradient equation above, we also have the general finite-difference equation for n = N – 1 = 7; this equation is shown below. 1 x7 1 x7 2 2 1 h y6 2 1 x7 h y 7 1 h y8 h 2 2 1 This equation was presented in the class notes and is equation 5.101 on page 271 of Hoffman. Engineering Building Room 1333 Email: lcaretto@csun.edu Mail Code 8348 Phone: 818.677.6448 Fax: 818.677.7062 November 30 homework solutions ME501A, L. S. Caretto, Fall 2004 Page 2 We can combine the equation above with the zero gradient equation to obtain a new equation that has only y7 and y8 as unknowns. To do this we write the zero gradient equation for N = 8 and use the result to eliminate y6 from the two equations. Substituting the zero gradient equation for y6 into the above equation gives. 1 x7 1 x7 2 2 1 h 4 y7 3 y8 2 1 x7 h y7 1 h y8 h 2 2 The algebraic manipulation below gives us an equation that we can use as the last equation in a tridiagonal system of equations. 2 1 x h 2 7 1 x7 1 x7 2 4 2h1 x7 y7 1 h 3 3h y8 h 2 2 2 1 x h 7 2 2h y 7 2 2h1 x7 y8 h 2 We still have to use the equation 1 h 1 x7 1 x7 2 y6 2 1 x7 h y7 1 h y8 = h2 2 2 as one of our equations. The purpose of the algebraic manipulations above was to convert one pair of equations, both of which had three variables, into an equivalent pair of equations, one of which has only two variables. We now have a set of linear equations that forms a tridiagonal matrix with the general equation Anyn-1 + Bnyn + Cnyn+1 = Dn. For all equations except the first (n = 1) and last (n = 8) equations, these coefficients are defined as follows. An 1 h 1 xn 2 Bn 2 1 xn h 2 Cn 1 h 1 xn 2 Dn h 2 For the constant step size used here, x n = nh. The first and last equations in the set were derived above. The first equation has no A coefficient and the last equation has no C coefficient. The values of the other coefficients for the first and last equations are B1 2 1 x1 h 2 C1 1 h A8 2 1 x7 h 2 2h 1 x1 2 D1 h 2 1 h B8 2 2h1 x7 1 x1 2 D8 h 2 We can use the Thomas algorithm for applying Gauss elimination to a tridiagonal system of equations. In this algorithm we compute values of Fn and En for a back-substitution process using the following initial equations: E1 = -C1/B1 and F1 = D1/B1. The remaining values of En and Fn are found from the following equations. En Cn Bn An E n1 and Fn Dn An Fn1 Bn An E n1 The last value of y, yN – in this problem, N = 8 – is found from the following equation. November 30 homework solutions ME501A, L. S. Caretto, Fall 2004 yN Page 3 DN AN FN 1 BN AN E N 1 The various coefficients and the solution by back substitution, yn = Fn + En yn+1 are shown in the table below. n 0 1 2 3 4 5 6 7 8 xn 0 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1 An Bn Cn Dn En Fn 0.921875 0.914063 0.90625 0.898438 0.890625 0.882813 1.560547 -1.98242 -1.98047 -1.97852 -1.97656 -1.97461 -1.97266 -1.9707 -1.53125 1.070313 1.078125 1.085938 1.09375 1.101563 1.109375 1.117188 -0.91406 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.539901 0.727113 0.826506 0.891009 0.938224 0.97566 1.007039 0.461084 0.276134 0.180212 0.120315 0.078759 0.047949 0.024072 -0.54466 yn 1 0.616945 0.288685 0.017262 -0.19716 -0.35631 -0.46371 -0.52442 -0.54466 The two approaches are seen to give significant differences. To determine which method is more accurate for this problem I ran the calculation using h = 0.005 and constructed the following table which compares the two methods for different grid sizes. The two methods give approximately the same results, to three significant figures, for N = 200; these presumably more accurate results are closer to the shoot-and-try method than to the finite-difference approach. This confirms that the shoot-and-try method is usually more accurate for boundary value problems in ordinary differential equations. N=8 x 0 0.125 0.25 0.375 0.5 0.625 0.75 0.875 1 Finite differences 1 0.61695 0.28868 0.01726 -0.19716 -0.35631 -0.46371 -0.52442 -0.54466 Shootand-try 1 0.57307 0.20780 -0.09345 -0.33045 -0.50520 -0.62172 -0.68578 -0.70445 N = 200 Finite Shootdifferences and-try 1 1 0.59250 0.59242 0.22739 0.22724 -0.07474 -0.07494 -0.31352 -0.31377 -0.49075 -0.49103 -0.61019 -0.61049 -0.67736 -0.67766 -0.69910 -0.69941 No statement is implied in this table about the relative accuracy of the two methods for N = 200. If we solved the equation using N = 20000 and compared both solutions for N = 200 to the N = 2000 grid, we would expect the shoot-and-try to be more accurate. Of course, this accuracy would not be needed if we only wanted answers to three significant figures. November 30 homework solutions ME501A, L. S. Caretto, Fall 2004 Page 4 Hoffman, page 497, problem 106 – Consider the eigenproblem described by equation 8.178. The exact solution of this eigenproblem is = ±n (n = 1, 2, …). The finite-difference equation corresponding to the eigenproblem is given by equation (8.185). The numerical solution of the eigenproblem for x = 1/5 is presented in Table 8.27. Determine the solution to the eigenproblem for (a) x = 1/6 and (b) x = 1/8. Compare the results of the three solutions in normalized form; that is k/. The finite-difference equations for this eigenproblem, as derived in section 8.9 of Hoffman, have the following general form: –yn-1 + (2 – k2h2)yn – yn+1 = 0. For a grid numbered from zero to N, h = 1/N and the boundary conditions are y0 = yN = 0. The first equation is (2 – k2h2)y1 – y2 = 0 and the last equation is (2 – k2h2)yN-1 – yN = 0. This set of equations has the conventional matrix eigenvalue form, (A – I)y = 0. where = k2h2, y is a column vector, and A is the following tridiagonal matrix 2 1 0 0 0 0 0 1 2 1 0 0 0 0 0 1 2 1 0 0 0 0 0 1 2 0 0 0 A 0 0 0 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 1 2 The solution to the eigenvalues, Det(A – I) = 0 can be found by starting with a grid that x = ¼. Such a grid has only three nodes (besides the two boundary nodes, and the eigenvalue problem is found as follows. To simplify the algebra, we define Z = 2 – = 2 – k2h2. Z 1 0 1 Z 1 Z 3 0 0 0 Z Z Z 3 2Z 0 1 Z For four nodes, we have to solve the equation below. We evaluate the derivative by expanding by the first row. We see that the first minor determinant is the one that we evaluated previously for three rows. Z 1 0 0 Z 1 0 1 1 0 1 Z 1 0 Z 1 Z 1 (1) 0 Z 1 Z ( Z 3 2Z ) ( Z 2 1) Z 4 3Z 2 1 0 1 Z 1 0 1 Z 0 1 Z 0 0 1 Z For five nodes we have the following result. Again we expand by the first row using previous results to evaluate the minor derivatives. Again we find that the first minor is the previous determinant for four rows. We evaluate the second minor by another minor expansion, this time using the first column for the expansion. Here we see that the only minor determinant in this expansion is the entire determinant for three rows. November 30 homework solutions ME501A, L. S. Caretto, Fall 2004 Z 1 0 0 0 1 Z 1 0 0 0 1 Z 1 0 Z 0 0 1 Z 1 0 0 0 1 Z Z 1 0 0 1 Z 1 0 0 1 Z 1 0 0 1 Z Page 5 1 1 0 0 0 Z 1 0 0 1 Z 1 0 0 1 Z (1) Z ( Z 4 3Z 2 1) (1)( Z 3 2Z ) Z 5 4Z 3 3Z 0 The fifth order equation can be written as Z(Z4 – 4Z2 + 3) = 0 so that one root is Z = 0. The other four roots are the roots of a quadratic equation for Y = Z 2. This quadratic equation can be written as Y2 – 4Y + 3 = (Y – 3) (Y – 1) = 0 whose roots are Y = 3 and Y = 1. This gives the following roots for Z = Y : 3 , 1, –1, and – 3 in addition to the previous root, Z = 0. Since we have defined Z = 2 – = 2 – k2h2, we can find k as follows, in this case where k = 1/6. k 2Z 2Z 6 2Z 1 h 6 The solution for x = 1/.8 requires the expansion of two more determinants. First we consider the determinant for six nodes in the field. As usual we expand it using the first row. 2 Z 1 0 0 0 0 Z 1 0 0 0 1 1 0 0 0 1 Z 1 0 0 0 1 Z 1 0 0 0 Z 1 0 0 0 1 Z 1 0 0 Z 0 1 Z 1 0 (1) 0 1 Z 1 0 0 0 1 Z 1 0 0 0 1 Z 1 0 0 1 Z 1 0 0 0 1 Z 1 0 0 0 1 Z 0 0 0 1 Z 0 0 0 0 1 Z Z Z 5 4Z 3 3Z (1) Z 4 3Z 2 1 Z 6 5Z 4 6Z 2 1 0 We can now obtain the determinant that we need for x = 1/8. Z 1 0 0 0 0 1 Z 1 0 0 0 0 1 Z 1 0 0 0 0 1 Z 1 0 0 0 0 1 Z 1 0 0 0 0 0 0 0 0 0 Z 1 0 0 0 0 1 1 0 0 0 0 0 1 Z 1 0 0 0 0 Z 1 0 0 0 0 0 1 Z 1 0 0 0 1 Z 1 0 0 (1) 0 Z 0 0 1 Z 1 0 0 0 1 Z 1 0 0 0 0 0 1 Z 1 0 0 0 1 Z 1 1 Z 1 0 0 0 0 1 Z 0 0 0 0 1 Z 0 1 Z Z Z 6 5Z 4 6 Z 2 1 (1) Z 5 4Z 3 3Z Z Z 6 6Z 4 10Z 2 4 0 2 At this point we see a general recurrence relationship. If D N denotes the determinant for a grid with N nodes (not counting boundary nodes) we can write D N = ZDN-1 – DN-2. November 30 homework solutions ME501A, L. S. Caretto, Fall 2004 Page 6 In this case, as with x = 1/6, we see that one root of the characteristic equation is Z = 0 and the remaining roots can be found by the solution of a cubic equation in Y = Z 2: Y3 – 6Y2 + 10Y – 2 = 0. The roots to this cubic were found using the goal seek method in Excel. One root was Y = 2 so that the cubic equation could be divided by Y – 2 to yield the quadratic equation Y2 – 4Y + 2 = 0 whose roots are Y = 2 2 . This gives the values of Z = Y as 2 and 2 2 as well as the root Z = 0 previously noted. As before we can determine the eigenvalue, k, from the values of Z using the following equation and setting h = 1/8 in this case. k 2Z 2Z 8 2Z 1 h 8 The eigenvalues found in this solution (and the ones in the text example for h = 0.2) are compared to the exact eigenvalues of in the table below. For the values of h = 1/6 and 1/8 computed here, the values of Z as well as the values of k/ are shown. Exact k/ 1 2 3 4 5 6 7 h = .2 Error k/ 0.9837 1.63% 1.8710 6.45% 2.5752 14.16% 3.0273 24.32% Z 1.7321 1 0 -1 -1.7321 h = 1/6 k/ 0.9885 1.9099 2.7009 3.3080 3.6896 Error 1.15% 4.51% 9.97% 17.30% 26.21% Z 1.8478 1.4142 0.7654 0 -0.7654 -1.4142 -1.8478 h = 0.125 k/ 0.9935 1.9490 2.8295 3.6013 4.2347 4.7053 4.9951 Error 0.65% 2.55% 5.68% 9.97% 15.31% 21.58% 28.64% As expected, the use of finer spacing gives more accurate values for the lower eigenvalues. Making the grid finer provides estimates of higher eigenvalues; a grid with a spacing of 1/N gives N+1 eigenvalues. However, the higher eigenvalues are always less accurate than the lower ones. The MATLAB commands and output below shows how MATLAB can be used to determine the eigenvalues of the ODE for any grid size. The number of grid nodes is specified by the variable m. Here we set m = 7 to get the same results as in the finest grid for this problem. However, by setting m to another value you could use the same commands to get a larger set of eigenvalues in which the first ones would be more accurate than the ones found here. The commands uses the MATLAB funcion diag(V,n) to construct the tridiagonal matrix; there is additional information regarding this function following the list of commands and screen output. >> %Solution of numerical ODE eigenvalue problem by MATLAB >> format long >> m = 7 %get more significant figures %define size of system // output deleted here >> h = 1/(m+1) % Get step size // output deleted here %Command below defines tridiagonal matrix; see explanation in text below >> A=-diag(ones(m-1,1),1)-diag(ones(m-1,1),-1)+2*diag(ones(m,1)) A = 2 -1 -1 2 0 -1 0 0 0 0 0 0 0 0 November 30 homework solutions 0 0 0 0 0 -1 0 0 0 0 >> eig(A) 2 -1 0 0 0 -1 2 -1 0 0 ME501A, L. S. Caretto, Fall 2004 0 -1 2 -1 0 0 0 -1 2 -1 Page 7 0 0 0 -1 2 % Get eigenvalues of matrix ans = 0.15224093497743 0.58578643762690 1.23463313526982 2.00000000000000 2.76536686473018 3.41421356237310 3.84775906502257 >> sqrt(ans)/h % Get eigenvalues of ODE ans = 3.12144515225805 6.12293491784144 8.88912372831364 11.31370849898476 13.30351379684072 14.78207252018059 15.69256448645169 The MATLAB function diag(V,n), used above, creates a matrix that is all zeros except for one diagonal, specified by the argument, n. The elements in the diagonal are taken from the one dimensional matrix specified in the first parameter, V. The size of the diagonal matrix is determined by the number of elements in V and the diagonal on which the elements are placed. If n = 0 the elements of V are distributed along the principal diagonal. If n is a nonzero (positive or negative) argument, the elements are placed in the diagonal that is n diagonals above (for positive n) or n diagonals below (for negative n) the principal diagonal. In the diag(V,n) commands above, the vector, V, is created by using another MATLAB function, ones. The command ones(m, n), used above, creates a matrix with m rows and n columns filled with ones. The command ones(m-1, 1), used twice above, creates a column vector of length m – 1, filled with ones. The tridiagonal matrix required for this problem is created as the sum of three individual matrices: (1) one with 2 in the principal diagonal, (2) one with -1 in the diagonal above the principal diagonal, and (3) one with -1 in the diagonal below the principal diagonal. Note that the size of the vector used when the diagonals above or below the principal diagonal are filled is one less than the size required to fill the principal diagonal. The eig command is used to get the matrix eigenvalues. These eigenvalues are represented as a column matrix in MATLAB. Here we have not set this matrix equal to any other symbol so MATLAB gives it the default symbol ans. We can take the square root of the ans matrix and divide it by the scalar h to get the desired result for the ODE eigenvalues. We could further divide this result by pi to get the normalized eigenvalues.