Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Finite element method wikipedia , lookup
Horner's method wikipedia , lookup
Dynamic substructuring wikipedia , lookup
Linear least squares (mathematics) wikipedia , lookup
Matrix multiplication wikipedia , lookup
Newton's method wikipedia , lookup
Root-finding algorithm wikipedia , lookup
System of polynomial equations wikipedia , lookup
Gaussian elimination wikipedia , lookup
Least squares wikipedia , lookup
ENT 258 Numerical Analysis Laboratory Module EXPERIMENT 4 SYSTEMS OF SIMULTANEOUS LINEAR ALGEBRAIC EQUATIONS 1.0 OBJECTIVE To understand matrix and vector operation in MATLAB and applying them to solve systems of linear equations 2.0. EQUIPMENT / APPARTRUS Computers and Matlab program. 3.0 INTRODUCTION & THEORY Many practical applications of engineering and science, quantitative problem of business and economics, and mathematical models of social sciences lead to a system of linear algebraic equations. A set of simultaneous linear algebraic equations can be expressed in a general form as a11x1 a12x2 ...a1nxn b1 a21x1 a22x2 ...a2nxn b2 . (4.1) . an1x1 an2x2 ...annxn bn Where the coefficients aij(i = 1, 2, …, n;j = 1, 2, …., n) and the constants bi (i= 1, 2, …., n) are known, xi(i = 1, 2, . . ., n) are the unknowns, and n is the number of equations. Equation (4.1) can be expressed in matrix form as Ax b, (4.2) where the n x n coefficient matrix [A], the n-component constant vector b and the ncomponent vector of unknown x are given by 1 ENT 258 Numerical Analysis a 11 a 21 . A . . n 1 a Laboratory Module a b x 12 . . . a 1 n 1 1 a b x2 22 . . . a 2 n 2 . . . . . . . ,x . ,b . . . . . . . . . . . . . . a b xn n 2 . . . a nn n Several methods can be used for solving systems of linear algebraic equations, Eq. (4.1). These methods can be divided into two types: direct and iterative. Direct methods are those that in the absence of round-off and other errors will yield the exact solution in a finite number of elementary arithmetic operations. Some of the commonly used direct and iterative methods are as follows: Direct (elimination) methods 1. Gauss elimination method 2. Gauss-Jordan method 3. LU decomposition method Indirect (iterative) methods 1. Jacobi method 2. Gauss-Siedel method 3. Relaxation method 3.1. Solving Linear Algebraic Equation with MATLAB function MATLAB provides two direct ways to solve systems of linear algebraic equations. The most efficient way is to employ the backslash, or ‘left-division,’ operator as in >> x = A\b The second is to use matrix inversion: >> x = inv (A)*b Example 4.1 150 100 0 x 588 . 6 1 100 150 50 x 686 . 7 2 0 50 50 x 784 . 8 3 >> A=[ 150 -100 0; -100 150 -50; 0 -50 50]; >> b=[588 686.7 784.8]'; >> x=A\b Using left-division function x= 41.1900 55.9050 71.6010 >> x=inv(A)*b Using inverse function 2 ENT 258 Numerical Analysis Laboratory Module x= 41.1900 55.9050 71.6010 3.2. Solving Linear Algebraic Equation with MATLAB programming 3.2.1 Gauss Elimination Method The most frequently used direct method for the solution of simultaneous algebraic linear equations is the Gauss elimination method. This method is based on the principle of reducing a set of n equations in n unknown to q equivalent upper triangular form. If the original equations are given by Esq. (4.1), the upper triangular form of equations appears as follows: a x a x a x ... a x c 11 1 12 2 13 3 1 n n 1 ' ' ' ' a x a x ... a x c 22 23 2 n 2 2 3 n ' ' ' a x ... a x c 33 3 n 3 3 n (4.3) . . ' ' ' a x a x c n 1 , n 1 n 1 , n n 1 n 1 n a'nnxn c'n The reduction of Eq.(4.1) to the upper triangular form of Eq.(4.3), known as forward elimination, is made such that the solution given by Eq.(4.3) is same as that of Eq. (4.1). The solution of Eq.(4.3) can be determined in a simple manner using a process known as back substitution. Since the last equation of system (4.3) contains only one unknown, xn, it is solved first. The remaining unknowns are found by using back substitution starting with the variable xn and proceeding backwards until the variable x1 is determined. Figure 2:The two phases of Gauss elimination: forward elimination and back substitution 3 ENT 258 Numerical Analysis Laboratory Module 3.2.1.1 Basic Algorithm Gauss Elimination Input A, C Forward Elimination (Poviting) Back Substitution Output Solution Vector, x Stop Figure 1: Flowchart for Gauss Elimination 4 ENT 258 Numerical Analysis Laboratory Module Flowchart Forward Elimination Form the A and B in augment form [A | C] p = 1,..,N-1 Loop over all rows in matrix, except last Find the max value in all rows (p) and column (p) Interchange row p and column p k = p+1:N mkp = Akp/App Akp = Akp –mkp*App Loop over all rows below the diagonal position (p,p) Compute multiplies for row (k) and column (p) k are the numbers of iterations for row No k>N Yes No p>N-1 Update coefficient for row (k) and column (p) Yes Back Substitution 5 ENT 258 Numerical Analysis Laboratory Module Flowchart Back Substitution XN = CN/ANN i = N-1:-1:1 N X i Ci Aik X k / Aii k i 1 Compute last unknown Loop backwards over all rows and columns Compute (i)th unknown No i< 1 Yes Stop 6 ENT 258 Numerical Analysis Laboratory Module Example-4.3 Use Gauss Elimination to solve 2 .51 x 1 .48 x 4 .53 x 5 .56 1 2 3 1 .48 x 0 .93 x 1 .30 x 0 .75 1 2 3 2 .68 x 3 .04 x 1 .48 x 1 .84 1 2 3 using MATLAB M-file. Procedure-MATLAB Programming 1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window. 2. Write the program given below in M-file function X = gauss(A,B) %Input - A is an N x N nonsingular matrix %B is an N x 1 matrix %Output- X is an N x 1 matrix containing the solution to AX=B. [N N]=size(A); C=zeros(1,N+1); %matrix zero %Form the augment matrix:Aug=[A| B] Aug=[A B]; %Forward Elimination process for column p for p=1:N-1 %Partial pivoting for column p [Y,j]=max(abs(Aug(p:N,p))); %function max is used to determine the largest available %coefficient in the column. %Interchange row p and j C=Aug(p,:); Aug(p,:)=Aug(j+p-1,:); Aug(j+p-1,:)=C; for k=p+1:N m=Aug(k,p)/Aug(p,p); Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1); end end %Back Substitution X=zeros(N,1) X(N)=Aug(N,N+1)/Aug(N,N); for k=N-1:-1:1 %the last value of x 7 ENT 258 Numerical Analysis Laboratory Module X(k)=(Aug(k,N+1)-Aug(k,k+1:N)*X(k+1:N))/Aug(k,k); disp(X); end 3. Click on Save As to save it as gauss.m. 4. Define matrixes A,B in Command Window. 5. To see how it works, type gauss(A,B) in MatLab Command Window. The built-in MATLAB function max is used to determine the largest available coefficient in the column below the pivot element. The max function has the syntax [ Y,j ] = max (A) where Y is the largest element in the vector (A), and j is the index corresponding to that element. 3.2.2 Gauss-Seidel Iteration Method The Gauss-Seidel method is the most commonly used iterative method for solving linear algebraic equations. Assume that we are given a set of n equations: Ax b Suppose that for conciseness we limit ourselves to a 3 x 3 set of equations. If the diagonal elements are all nonzeros, the first equation can be solved for x1, the second for x2, and the third for x3 to yield j 1 j 1 b a x a x 1 12 2 12 3 x a 11 j 1 (4.4) j 1 b a x a x 2 21 1 23 3 x a 22 (4.5) b a x a x j 3 31 1 32 2 x 3 a 33 (4.6) j j 2 j j where j and j-1 are the present and previous iterations. To start the solution process, initial guesses must be made for the x’s. A simple approach is to assume that they are all zero. These zeros can be substituted into Eq.(4.4), which can be used to calculate a new value for x1 b1 . Then we substitute this new a11 value of x1 along with the previous guess of zero for x3 into Eq. (4.5) to compute a new value for x2. The process is repeated for is repeated for Eq. (4.6) to calculate a new 8 ENT 258 Numerical Analysis Laboratory Module estimate for x3. Then we return to the first equation and repeat the entire procedure until our solution converges closely enough to the true values. Convergence can be checked using the criterion that for all i, xi xi j Error xi j 1 j 100% esp Before developing an algorithm, let us first recast Gauss-Siedel in a form that is compatible with MATLAB’s ability to perform matrix operation. This is done by expressing Eq. (4.4) to (4.6) as new x1 b1 a11 a olda 13 old 12 x 2 x 3 a a 11 11 a new newb x 2 21 x 2 1 a 22 a 22 a23 old x3 a22 b a new new new 3 a 31 32 x 3 x 1 x 2 a a a 33 33 33 Notice that the solution can be expressed concisely in matrix form as x dC x where b1 / a11 d b2 / a22 b / a 3 33 and /a /a 0 a 12 11a 13 11 C a /a 0 a /a 21 22 23 22 a / a a / a 0 3133 3233 In general form of Gauss-Seidel iteration can be expressed as 1 n i ( k 1 ) 1 k 1 k x b a x a x i = 1,2, …,n , k = 1,2, i i ij j ij j a 1 j i 1 ii j 9 ENT 258 Numerical Analysis Laboratory Module Flowchart for Gauss Seidel Start A,B,P,esp,n Yes k=1 k>n STOP k = k +1 k= 1 No Yes i=1 i>N i = i+1 i =1 No i 1 n b a x a x i ij j ij j j 1 j j 1 k 1 x i a ii k 1 k abs(xik – xik-1/xik) *100< esp No Yes STOP Example-4.4 Solve the following equations using the Gauss-Seidal iteration method: 8x1 x2 x3 8 x1 7x2 2x3 4 2x1 x2 9x3 12 with initial guesses are P0 = 0 = (0, 0, 0), convergence criterion is esp = 0.5 and, number of iterations are n = 10. Use MATLAB M-file to solve this system of equations. 1 0 ENT 258 Numerical Analysis Laboratory Module Procedures -MATLAB Programming 1. Start a new MatLab file by clicking on File, New and M-file that opens an empty file in the Editor/Debugger window. 2. Write the program given below in M-file function x = Gauss_Seidel(A,B,P,esp,n) % input = A,B,P,esp,n %P=initial values %esp=error %n=number of iterations needed %output=x N=length(B); disp(' k x1 x2 x3') for k=1:n for j=1:N if j==1 X(1)=(B(1)-A(1,2:N)*P(2:N))/A(1,1); elseif j==N X(N)=(B(N)-A(N,1:N-1)*(X(1:N-1))')/A(N,N); else X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)'-A(j,j+1:N)*P(j+1:N))/A(j,j); end end err=abs((X'-P)/X’)*100; P=X'; if (err<esp) break end disp([k X]); end 3. Click on Save As to save it as Gauss_Seidel.m. 4. Define the values of A,B,P,esp,n in Command Window 5. To see how it works, type Gauss_Seidel(A,B,P,esp,n) in MatLab Command Window. 1 1 ENT 258 Numerical Analysis 4.0 Laboratory Module LAB ASSIGNMENT QG cG0 cG1 cG2 cG3 cG4 cG5 cL1 cL2 cL3 cL4 cL5 QL QG cL6 QL Figure 1 Figure 1 depicts a chemical exchange process consisting of a series of reactors in which a gas flowing from left to right is passed over a liquid flowing from right to left. The transfer of a chemical from the gas into the liquid occurs at rate that is proportional to the difference between the gas and liquid concentrations in each reactor. At steady state, a mass balance for the first reactor can be written for the gas as QG cG 0 QG cG1 D(c L1 cG1 ) 0 and for the liquid as QL c L 2 QL c L1 DcG1 c L1 0 where QG and Q L are the gas and liquid flow rates, respectively, and D = the gas-liquid exchange rate. Similar balances can be written for the other reactor. Solve for the concentration given the following values: QG = 2, Q L = 1, D = 0.8, cG 0 = 100, c L 6 = 10. Use a) Gauss-Seidel iteration method with initial guesses are 0 and esp = 0.5% b) Gauss Elimination method Data Analysis 1) Fill in the Table 1 & 2 for method (a). 2) Fill in the Table 3 for method (b). 3) Substitute your both results back into the original equations to check your solution. 1 2 ENT 258 Numerical Analysis 5.0 Laboratory Module DATA & RESULTS Assignment 1 Lab Name : ………………….. Pc Number : ………………….. Folder Name : ………………….. Mathematical Model Equation Form for each of reactor Matrix Form 1 3 ENT 258 Numerical Analysis Laboratory Module Table 1 Gas k(iteration) cG1 cG2 cG3 cG4 cG5 cL1 cL2 cL3 cL4 cL5 1 . . . . . . n Liquid k(iteration) 1 . . . . . . n Table 2 Reactor 0 1 2 3 4 5 6 Table 3 Reactor 0 1 2 3 4 5 6 Gas Liquid Gas Liquid 1 4 ENT 258 Numerical Analysis 6.0 DISCUSSION 7.0 CONCLUSION Laboratory Module 1 5