Root Finding Methods for Linear Systems: Applied to Temperature Distribution in Thin Rods By Kevin Forgie Abstract: The problem being considered involves the spatial temperature distribution through a uninsulated, uniform, long and thin rod. The methods of numerical analysis used to evaluate the temperature distribution are the Thomas Method for Tridiagonal Matrices, the Jacobi Method, the Gauss-Seidel Method, and the Successive Overrelaxation (SOR) Method, as well as standard functions already programmed in the program MATLAB. 1. Introduction A common problem in engineering is understanding the spatial temperature distribution of a long, thin, uniform rod. The rod is positioned between two walls of constant but different temperature, and uninsulated - therefore it interacts with an environment of known ambient temperature. The system is assumed to have reached equilibrium, and is therefore timeindependent. For this investigation, four equally spaced nodes of unknown temperature are identified within the rod. The methods of numerical analysis used to solve for these unknown temperatures were the Thomas Method for Tridiagonal Matrices, the Jacobi Method, the Gauss-Seidel Method, and the Successive Overrelaxation (SOR) Method, as well as standard functions already programmed in the program MATLAB. It was determined that the temperatures of the nodes were 59.74 83.46, 113.05, and 150.89 degrees Celsius (going from the cold end of the rod to the hot end). 2. Physical Analysis Figure 1: Thin rod system with parameters (temperatures in 0C and lengths in meters). After the rod is placed into the environment according to Figure 1, its temperature begins to change due to differences in temperature between the rod, the walls, and the ambient air. This occurs by the processes of conduction (between the rod and the walls) and convection (between the rod and the ambient air). Due to the constant temperatures being applied at the wall contacts, the temperature inside the rod will exhibit a gradient, with lower temperatures at its left end and higher temperatures towards its right end. The analysis begins after the system has had sufficient time to reach equilibrium; therefore all of the temperatures are no longer changing and it is independent of time. The temperature distribution through such a rod is only dependent on location and is described by the following differential equation: (1) in which T is temperature (0C ), x is the distance along the rod (m), is a heat transfer coefficient between the rod and the ambient air ( m-2 ), and Ta is the temperature of the surrounding air (0C) [1]. This differential equation is solved explicitly with appropriate boundary conditions to obtain an exact solution. 3. Numerical Analysis In order to transform the differential equation into a system of linear algebraic equations that can be solved by numerical analysis, the second derivative is replaced with the finite divided centered difference: (2) , where Ti = T (xi ) represents the temperature at node xi. Making this substitution into the differential equation (1) yields an equation representing a system of linear equations: (3) which can be evaluated for nodes 0 through 5, with node 0 representing the left end of the rod and node 5 representing the right end of the rod. Nodes 0 and 5 are the wall contacts and have fixed temperatures, acting as boundary conditions for the system. Nodes 1 through 4 are the interior and unknown nodes, for which values will be obtained using numerical methods for linear systems. For this analysis, it is beneficial to rewrite equation (3) as a system of equations for i=1,2,3,4 with the unknowns T1, T2, T3, and T4: i = 1: i = 2: i = 3: i = 4: -T0 + (2+*x2)T1 –T2 = *x2*Ta -T1 + (2+*x2)T2 –T3 = *x2*Ta -T2 + (2+*x2)T3 –T4 = *x2*Ta -T3 + (2+*x2)T4 –T5 = *x2*Ta. (4) Next, it is convenient to represent this system in matrix form (Ax=b), which yields: Figure 2: Matrix Representation of thin rod system. It is this form that makes it possible to implement MATLAB to solve a linear system. It can also be noted that matrix (5) is a tridiagonal matrix. Thus, the Thomas Method for Tridiagonal Matrices, the Jacobi Method, the Gauss-Seidel Method, and the Successive Overrelaxation (SOR) Method, as well as standard functions already programmed in the program MATLAB, can now be utilized. The Thomas Method for tridiagonal matrices is a special type of method based on the fact that the matrix only has non-zero values on the main diagonal and the diagonals on either side of the main one. This allows for a very efficient algorithm when compared to typical elimination or decomposition methods that require pivoting, because none of the elements outside the diagonals would change their values from zero so it is useless to store them or involve them in calculations. Thus, the three diagonals with non-zero values are relabeled from the matrix A as vectors, namely e, f, and g, with the variable matrix x and the constant matrix r. The Thomas Method is a specific version of an LU decomposition method, and consists of three main steps – decomposition, forward substitution, and back substitution. The key equations for these steps are: ek=ek/fk-1 and fk=fk-ek*gk-1 (decomposition), rk=rk-ek*rk-1 (forward substitution), (5) xn=rn/fn and xk=(rk-gk*xk+1)/fk (back substitution)[2]. The Jacobi Method is an approximate, iterative method. It employs initial guesses and iterates to develop refined estimates of the solution. With the system Ax=b (assuming all diagonal values are nonzero, which for the matrix in this problem this is the case), the first equation can be solved for x1, the second for x2, the third for x3, and the fourth for x4. This is accomplished with the following equations: x1=(b1-a12x2-a13x3-a14x4)/a11, x2=(b2-a21x1-a23x3-a24x4)/a22, (6) x3=(b3-a31x1-a32x2-a34x4)/a33, x4=(b4-a41x1-a42x2-a43x3)/a44. After choosing initial guesses for the solution, these equations are used iteratively until a maximum number of iterations or the desired level of accuracy is reached. The values of x used in these equations are all the values obtained from the initial guess or the previous iteration; they are not updated inside of each general step of the iteration [2]. The Gauss-Seidel Method is very similar method to that of Jacobi, in that it is an approximate, iterative method that refines an initial guess for the solution to the system Ax=b. The main equations for Gauss-Seidel are the same as for Jacobi, namely x1=(b1-a12x2-a13x3-a14x4)/a11, x2=(b2-a21x1-a23x3-a24x4)/a22, (7) x3=(b3-a31x1-a32x2-a34x4)/a33, x4=(b4-a41x1-a42x2-a43x3)/a44. There is an important difference in the way the x-values are treated within these equations between the two methods. For Gauss-Seidel, the updated values are used immediately in the next calculation (for example, the new x1 value will be used to compute the x2 value of the same iteration). Thus, if the system is convergent, it will converge faster by Gauss-Seidel than with Jacobi. As with all iterative methods, the process will continue until it reaches a maximum number of iterations or the desired level of accuracy[2]. The Successive Overrelaxation (SOR) Method is a modification of the Gauss-Seidel Method that is designed to speed up the convergence of a system that is already known to be convergent by placing extra weight on the present value. This is accomplished with the use of a weighting factor, y (with a value between 1 and 2), as per the following equations: x1=[y(b1-a12x2-a13x3-a14x4)-(y-1)a11x1,old]/a11, x2=[y(b2-a21x1-a23x3-a24x4)-(y-1)a22x2,old]/a22, x3=[y(b3-a31x1-a32x2-a34x4)-(y-1)a33x3,old]/a33, (8) x4=[y(b4-a41x1-a42x2-a43x3)-(y-1)a44x4,old]/a44, where xold represents the value calculated by the previous iteration and xi represents the value from the current iteration. The SOR Method will converge faster than the Gauss-Seidel Method, and much faster than the Jacobi Method, given the system is already convergent. As an iterative method, it will continue until it reaches a maximum number of iterations or the desired level of accuracy [1]. The program MATLAB, which stands for Matrix Laboratory, was designed for matrix manipulations and has several built-in methods for solving linear systems (Ax=b). The three most important and used for this problem were: x=A\b, which performs matrix division; x=inv(A)*b, which uses the inverse matrix of A; and rref(Aaug), which row-reduces the augmented matrix of the system. 4. Results The four methods previously described were used to solve the linear system for this problem, which is of the form Ax=b. The algorithm for the Thomas Method is as follows: 1. Decompose the matrix (A) based on its diagonals (vectors) starting with row 2 and going to the bottom of the matrix, via: ek=ek/fk-1 fk=fk-ek*gk-1, k=2,3,4 2. Forward substitute to modify the constants (b) starting with row 2 and going to the bottom of the matrix, via rk=rk-ek*rk-1, k=2,3,4 3. Define solution matrix, via xn=rn/fn, n=1,2,3,4 4. Back substitute to get the solutions, via xk=(rk-gk*xk+1)/fk, k=3,2,1 The algorithm for the Jacobi Method is as follows: 1. Initialize solution matrix to all zeros. 2. Set new solution matrix equal to old solution matrix 3. Update new solution matrix via: x1=(b1-a12x2,old-a13x3,old-a14x4,old)/a11, x2=(b2-a21x1,old-a23x3,old-a24x4,old)/a22, x3=(b3-a31x1,old-a32x2,old-a34x4,old)/a33, x4=(b4-a41x1,old-a42x2,old-a43x3,old)/a44. 4. Repeat steps 2 and 3 until (xi-xi-1)/xi is less than the desired error or the maximum number of iterations is reached. The algorithm for the Gauss-Seidel Method is as follows: 1. Initialize solution matrix to all zeros. 2. Set new solution matrix equal to old solution matrix 3. Update new solution matrix via: x1=(b1-a12x2,old-a13x3,old-a14x4,old)/a11, x2=(b2-a21x1,new-a23x3,old-a24x4,old)/a22, x3=(b3-a31x1,new-a32x2,new-a34x4,old)/a33, x4=(b4-a41x1,new-a42x2,new-a43x3,new)/a44. 4. Repeat steps 2 and 3 until (xi-xi-1)/xi is less than the desired error or the maximum number of iterations is reached. The algorithm for the SOR Method is as follows. 1. Initialize solution matrix to all zeros. 2. Set new solution matrix equal to old solution matrix 3. Update new solution matrix via: x1=[y(b1-a12x2,old-a13x3,old-a14x4,old)-(y-1)a11x1,old]/a11, x2=[y(b2-a21x1,new-a23x3,old-a24x4,old)-(y-1)a22x2,old]/a22, x3=[y(b3-a31x1,new-a32x2,new-a34x4,old)-(y-1)a33x3,old]/a33, x4=[y(b4-a41x1,new-a42x2,new-a43x3,new)-(y-1)a44x4,old]/a44. 4. Repeat steps 2 and 3 until (xi-xi-1)/xi is less than the desired error or the maximum number of iterations is reached. The results from these methods are the following: Thomas Method o t1 = 59.738892 o t2 = 83.456895 o t3 = 113.051449 o t4 = 150.890120 Figure 3: Analytical solution (blue) and point solutions (red) from Thomas Method Jacobi Method o t1 = 59.738892 o t2 = 83.456895 o t3 = 113.051449 o t4 = 150.890120 Figure 4: Analytical solution (blue) and point solutions (red) from Jacobi Method Gauss-Seidel Method o t1 = 59.738892 o t2 = 83.456895 o t3 = 113.051449 o t4 = 150.890120 Figure 5: Analytical solution (blue) and point solutions (red) from Gauss-Seidel Method SOR Method o t1 = 59.738892 o t2 = 83.456895 o t3 = 113.051449 o t4 = 150.890120 Figure 6: Analytical solution (blue) and point solutions (red) from SOR Method The results by the methods already implemented in MATLAB are the following: x=A\b o t1 = 59.7389 o t2 = 83.4569 o t3 = 113.0514 o t4 = 150.8901 x=inv(A)*b o t1 = 59.7389 o t2 = 83.4569 o t3 = 113.0514 o t4 = 150.8901 rref(Aaug) o t1 = 59.7391 o t2 = 83.4571 o t3 = 113.0513 o t4 = 150.8901 The results of solving the differential equation explicitly are the following: T(x) = 10 + 47.209*exp(sqrt(0.02)*x) – 17.209*exp(-sqrt(0.02)*x) o t1 = 59.67 o t2 = 83.34 o t3 = 112.92 o t4 = 150.79 With these results, several important observations can be made. All of the methods were successful in calculating the solution of the linear system to a high degree of accuracy. It is difficult to tell which methods are the most accurate, as there is rounding error in nearly all of the calculations. In general, it is more efficient to use the methods already implemented into MATLAB than to code other numerical methods for linear systems, as all of the methods used attained the same results to six significant figures. By experimenting with different weighting factors for the SOR Method, it was found that a value of y=1.3 achieved the lowest number of iterations for this system. In graphing the results for each method against the curve of the analytical solution, it can be seen that the two are very close, but there is still a small difference in the numeric values. 5. Conclusions As was shown, the solution to our problem with a thin rod in the given environment yields the four internal node temperatures t1 = 59.70C, t2 = 83.40C, t3 = 113.00C, and t4 = 150.90C. This was obtained using for methods of numerical analysis for linear systems, namely the Thomas Method for Tridiagonal Matrices, the Jacobi Method, the Gauss-Seidel Method, and the Successive Overrelaxation (SOR) Method, as well as methods already programmed into MATLAB, and explicitly solving the differential equation modeling the system (Equation 1). All of the methods successfully and accurately found the desired values. By implementing a timer and an iteration counter, the methods can be compared for their computational efficiency. By time, Thomas was the fastest at 0.002619 seconds, followed by SOR - 0.003359 seconds, Gauss-Seidel - 0.005680 seconds, and Jacobi - 0.007644 seconds. By count, SOR was the fastest at 26, followed by Gauss-Seidel – 71, and Jacobi – 138 (Thomas does not have a count as it is not an iterative method). These numbers reflect the earlier assumptions that for a convergent system, SOR is an improvement over Gauss-Seidel, which is an improvement over Jacobi; but for a tridiagonal matrix, Thomas appears to be the most efficient method. 6. References [1] Chapra, Steven C., and Raymond P. Canale. Numerical Methods for Engineers. 6th ed. 296314. [2] DRAPACA, CORINA. "Linear Algebraic Equations." Lecture. Appendix 1 Appendix 2 %E SC 407H Project 2 Code %Kevin Forgie %Assistance from Ryan Trella clear clc t0=40; t5=200; ta=10; alf=0.02; delx=2; error = 0.000001; imax = 100000; xaxis=0:0.001:10; actualsolution = 10+47.209*exp(sqrt(alf)*xaxis)-17.209*exp(-sqrt(alf)*xaxis); Aug = [(2+alf*delx^2),-1,0,0,(alf*delx^2*ta+t0); -1,(2+alf*delx^2),-1,0,(alf*delx^2*ta); 0,1,(2+alf*delx^2),-1,(alf*delx^2*ta); 0,0,-1,(2+alf*delx^2),(alf*delx^2*ta+t5)]; A = [(2+alf*delx^2),-1,0,0; -1,(2+alf*delx^2),-1,0; 0,-1,(2+alf*delx^2),-1; 0,0,1,(2+alf*delx^2)]; b = [(alf*delx^2*ta+t0); (alf*delx^2*ta); (alf*delx^2*ta); (alf*delx^2*ta+t5)]; fprintf('The following are standard MATLAB functions to solve linear systems Ax=b:\n\n') fprintf('Multiplying the matrix b by the inverse of A\n') std1 = inv(A)*b fprintf('\n\nBackdividing b by A:\n') std2 = A\b fprintf('\n\nRow Reducing the augmented matrix to its echelon form:\n') std3 = rref(Aug) %Thomas Method for Tridiagonal Systems tic T=A; G=b; for i=2:1:4 T(i,(i-1)) = T(i,(i-1))/T((i-1),(i-1)); T(i,i) = T(i,i) - T(i,(i-1))*T((i-1),i); end for j=2:1:4 G(j,1) = G(j,1) - T(j,(j-1))*G((j-1),1); end t = [G(1,1)/T(1,1); G(2,1)/T(2,2); G(3,1)/T(3,3); G(4,1)/T(4,4)]; for k=3:-1:1 t(k,1) = (G(k,1) - T(k,(k+1)) * t((k+1),1)) / T(k,k); end fprintf('\n\n\nThe results by the Thomas method are:\n\n') fprintf('t1 = %f\n', t(1,1)) fprintf('t2 = %f\n', t(2,1)) fprintf('t3 = %f\n', t(3,1)) fprintf('t4 = %f\n\n', t(4,1)) toc figure plot(xaxis,actualsolution) grid on hold on xlabel('Location Within Rod (meters)') ylabel('Temperature (degrees Celsius)') title('Temperature vs Distance, for Thomas Method') plot(2,t(1,1),'rx') plot(4,t(2,1),'rx') plot(6,t(3,1),'rx') plot(8,t(4,1),'rx') hold off %Gauss-Seidel Method tic T=A; G=b; t=[0;0;0;0]; count=0; for i=1:1:imax told=t; t(1,1) = (G(1,1) - T(1,2)*t(2,1) - T(1,3)*t(3,1) - T(1,4)*t(4,1))/T(1,1); t(2,1) = (G(2,1) - T(2,1)*t(1,1) - T(2,3)*t(3,1) - T(2,4)*t(4,1))/T(2,2); t(3,1) = (G(3,1) - T(3,1)*t(1,1) - T(3,2)*t(2,1) - T(3,4)*t(4,1))/T(3,3); t(4,1) = (G(4,1) - T(4,1)*t(1,1) - T(4,2)*t(2,1) - T(4,3)*t(3,1))/T(4,4); terror = abs(t-told)./abs(t); count=count+1; if all(terror) < error break end end fprintf('\n\n\nThe results by the Gauss-Seidel method are:\n\n') fprintf('t1 = %f\n', t(1,1)) fprintf('t2 = %f\n', t(2,1)) fprintf('t3 = %f\n', t(3,1)) fprintf('t4 = %f\n\n', t(4,1)) fprintf('The number of iterations to calculate the result is %f\n', count) toc figure plot(xaxis,actualsolution) grid on hold on xlabel('Location Within Rod (meters)') ylabel('Temperature (degrees Celsius)') title('Temperature vs Distance, for Gauss-Seidel Method') plot(2,t(1,1),'rx') plot(4,t(2,1),'rx') plot(6,t(3,1),'rx') plot(8,t(4,1),'rx') hold off %Jacobi Method tic T=A; G=b; t=[0;0;0;0]; count=0; for i=1:1:imax told=t; t(1,1) = (G(1,1) - T(1,2)*told(2,1) - T(1,3)*told(3,1) - T(1,4)*told(4,1))/T(1,1); t(2,1) = (G(2,1) - T(2,1)*told(1,1) - T(2,3)*told(3,1) - T(2,4)*told(4,1))/T(2,2); t(3,1) = (G(3,1) - T(3,1)*told(1,1) - T(3,2)*told(2,1) - T(3,4)*told(4,1))/T(3,3); t(4,1) = (G(4,1) - T(4,1)*told(1,1) - T(4,2)*told(2,1) - T(4,3)*told(3,1))/T(4,4); terror = abs(t-told)./abs(t); count=count+1; if all(terror) < error break end end fprintf('\n\n\nThe results by the Jacobi method are:\n\n') fprintf('t1 = %f\n', t(1,1)) fprintf('t2 = %f\n', t(2,1)) fprintf('t3 = %f\n', t(3,1)) fprintf('t4 = %f\n\n', t(4,1)) fprintf('The number of iterations to calculate the result is %f\n', count) toc figure plot(xaxis,actualsolution) grid on hold on xlabel('Location Within Rod (meters)') ylabel('Temperature (degrees Celsius)') title('Temperature vs Distance, for Jacobi Method') plot(2,t(1,1),'rx') plot(4,t(2,1),'rx') plot(6,t(3,1),'rx') plot(8,t(4,1),'rx') hold off %SOR Method tic T=A; G=b; t=[0;0;0;0]; count=0; y=1.3; for i=1:1:imax told=t; t(1,1) = (y*G(1,1) - y*T(1,2)*t(2,1) - y*T(1,3)*t(3,1) - y*T(1,4)*t(4,1) - (y1)*T(1,1)*told(1,1))/T(1,1); t(2,1) = (y*G(2,1) - y*T(2,1)*t(1,1) - y*T(2,3)*t(3,1) - y*T(2,4)*t(4,1) - (y1)*T(2,2)*told(2,1))/T(2,2); t(3,1) = (y*G(3,1) - y*T(3,1)*t(1,1) - y*T(3,2)*t(2,1) - y*T(3,4)*t(4,1) - (y1)*T(3,3)*told(3,1))/T(3,3); t(4,1) = (y*G(4,1) - y*T(4,1)*t(1,1) - y*T(4,2)*t(2,1) - y*T(4,3)*t(3,1) - (y1)*T(3,3)*told(4,1))/T(4,4); terror = abs(t-told)./abs(t); count=count+1; if all(terror) < error break end end fprintf('\n\n\nThe results by the SOR method are:\n\n') fprintf('t1 = %f\n', t(1,1)) fprintf('t2 = %f\n', t(2,1)) fprintf('t3 = %f\n', t(3,1)) fprintf('t4 = %f\n\n', t(4,1)) fprintf('The number of iterations to calculate the result is %f\n', count) toc figure plot(xaxis,actualsolution) grid on hold on xlabel('Location Within Rod (meters)') ylabel('Temperature (degrees Celsius)') title('Temperature vs Distance, for SOR Method') plot(2,t(1,1),'rx') plot(4,t(2,1),'rx') plot(6,t(3,1),'rx') plot(8,t(4,1),'rx') hold off