COMPUTATIONAL MODELING FOR ENGINEERING MECN 6040 Professor: Dr. Omar E. Meza Castillo omeza@bayamon.inter.edu http://facultad.bayamon.inter.edu/omeza Department of Mechanical Engineering IMPLEMENTATION OF SOLUTION METHODS Gaussian elimination, LU factorization, and others INTRODUCTION 3 ▪ An equation of the form ax+by+c=0 or equivalently ax+by=-c is called a linear equation in x and y variables. ▪ ax+by+cz=d is a linear equation in three variables, x, y, and z. ▪ Thus, a linear equation in n variables is a1x1+a2x2+ … +anxn = b ▪ A solution of such an equation consists of real numbers c1, c2, c3, … , cn. If you need to work more than one linear equations, a system of linear equations must be solved simultaneously. SOLVING SMALL NUMBER OF EQUATIONS ▪ For small number of equations (n ≤ 3) linear equations can be solved readily by simple techniques such as “method of elimination.” ▪ Linear algebra provides the tools to solve such systems of linear equations. 4 ▪ Nowadays, easy access to computers makes the solution of large sets of linear algebraic equations possible and practical. SOLVING SMALL NUMBER OF EQUATIONS ▪ There are many ways to solve a system of linear equations: ▪ Graphical method ▪ Cramer’s rule ▪ Method of elimination 5 ▪ Computer methods. THE GRAPHICAL METHOD For two equations: a11 x1 a12 x2 b1 a21 x1 a22 x2 b2 Solve both equations for x2: a21 b2 x1 x2 a22 a22 x2 (slope) x1 intercept 6 a11 b1 x1 x2 a12 a12 THE GRAPHICAL METHOD Or equate and solve for x1 a11 a21 b1 b2 x1 x1 x2 a12 a22 a12 a22 a21 a11 b1 b2 x1 0 a12 a22 a22 a12 7 b1 b2 b2 b1 a12 a22 a22 a12 x1 a21 a11 a21 a11 a22 a12 a22 a12 THE GRAPHICAL METHOD Infinite solutions Ill-conditioned (Slopes are too close) 8 No solution 9 THE GRAPHICAL METHOD 2 x1 x 2 3 x 2 2 x1 3 rearrange x x 3 x 3 x 2 1 1 2 x1 + x2 = 3 2x1 – x2 = 3 10 One solution THE GRAPHICAL METHOD No solution 2x1 – x2 = – 1 11 2x1 – x2 = 3 THE GRAPHICAL METHOD Infinite solutions 6x1 – 3x2 = 9 12 2x1 – x2 = 3 CRAMER’S RULE ▪ Compute the determinant D D ▪ 3 x 3 matrix a11 a11 a21 a22 a12 a13 D a21 a22 a31 a32 a23 a33 a11 a12 a22 a23 a32 a33 a12 a11a22 a12a21 a21 a23 a31 a33 a13 a21 a22 a31 a32 13 ▪ 2 x 2 matrix CRAMER’S RULE ▪ To find xk for the following system a11 x1 a12 x2 ... a1n xn b1 a21 x1 a22 x2 ... a2 n xn b2 an1 x1 an 2 x2 ... ann xn bn ▪ Replace kth column of as with bs (i.e., aik bi ) 14 D( new matrix) xk D(aij ) b1 D1 1 x1 b2 D D b3 ▪ 3 x 3 matrix a11 a12 a13 D a21 a22 a31 a32 a23 a33 a12 a22 a32 a13 a23 a33 a11 b1 D2 1 x2 a21 b2 D D a31 b3 a13 a23 a33 a11 a12 D3 1 x3 a21 a22 D D a31 a32 b1 b2 b3 15 CRAMER’S RULE 16 EXAMPLE 9.3 0.5 x1 x2 1.9 x3 0.67 0.1x1 0.3x2 0.5 x3 0.44 0.3 0.52 D 0.5 0.1 1 0.3 1 1.9 0.5 0.3 0.01 1 D2 1 x2 0.5 0.67 1.9 29.5 D D 0.1 0.44 0.5 0.3 0.52 0.01 D3 1 x3 0.5 1 0.67 19.8 D D 0.1 0.3 0.44 17 0.3x1 0.52 x2 x3 0.01 0.01 0.52 1 D1 1 x1 0.67 1 1.9 14.9 D D 0.44 0.3 0.5 ELIMINATION METHOD a11x1 a12 x2 b1 a21x1 a22 x2 b2 Eliminate x2 Subtract to get a22a11x1 a22a12 x2 a22b1 a12a21x1 a12a22 x2 a12b2 a22a11 x1 a12a21 x1 a22b1 a12b2 Not very practical for large number (> 4) of equations 18 a22b1 a12b2 a11b2 a21b1 x1 x2 a22a11 a12a21 a11a22 a12a21 ▪ 2 x 2 matrix x1 2 x2 2 19 3x1 2 x2 18 2(18) 2(2) x1 4 3(2) 2(1) 3(2) (1)(18) x2 3 3(2) 2(1) NAVIE GAUSS ELIMINATION The system can be written in a matrix format as Gauss elimination is the most important algorithm to solve systems of linear equations. It involves equations unknowns. by to combining eliminate a11 a12 a1n x1 b1 a a a x b 2n 2 2 21 22 an1 an 2 ann xn bn It involves 2 phases: 2. Back substitution: work from the last equation up. 20 1. Forward elimination phase: reduce the set of equations to an upper triangular system. NAVIE GAUSS ELIMINATION In the first step of the forward elimination phase, x1 is eliminated from all equations except the first one. The coefficient of x1 in the first equation is called the pivot element. The second step is to eliminate x2 from the third equation through the nth equation. Do the same for all variables x3 to xn-1. The goal is to set up upper triangular matrix 21 The back-substitution phase starts from the last equation up, to find the values of x1, x2, …, xn. NAVIE GAUSS ELIMINATION Forward elimination phase Gauss Elimination Pseudocode 22 Back substitution NAVIE GAUSS ELIMINATION Use Gauss elimination to solve Carry 6 significant figures Solution 7.00333 x2 0.293333 x3 19.5617 23 1. Forward elimination: eliminate x1 from equation (2): - (0.1/3) 0.1x1 0.00333333 x2 0.00666667 x3 0.261667 0.1x1 7 x2 0.3 x3 19.3 Eliminate x1 from - (0.3/3) equation (3): After eliminating x1 from equations (2) and (3), the system becomes Eliminate x2 from equation (3): + (0.190000/7.00333) 24 After eliminating x2 from equation (3), the system becomes 2. Back Substitution: find the value of x3 from equation (3): 70.0843 x3 7.0000 10.0120 Substitute the value of x3 in equation (2) to find the value of x2: 7.00333x2 0.293333(7.0000) 19.5617 x2 19.5617 0.293333(7.0000) 2.50000 7.00333 Substitute the values of x2 and x3 in equation (1) to find the value of x1: 7.85 0.1(2.50000) 0.2(7.0000) x1 3.00000 3 25 3x1 0.1(2.50000) 0.2(7.0000) 7.85 PITFALLS OF ELIMINATION METHODS Division by zero (Partially solved by the pivoting technique) Round-off errors (Important when large number of equation are to be solved) Ill-conditioned systems: Small changes in coefficients result in large changes in the solution. D detA 0 26 When TECHNIQUES FOR IMPROVING SOLUTIONS Use of more significant figures (The simplest remedy) Pivoting (Determine the largest available coefficient in the column below the pivot element and switch rows so that the largest element is the pivot element) 0.0003x1 3.0000 x2 2.0001 27 1.0000 x1 1.0000 x2 1.0000 TECHNIQUES FOR IMPROVING SOLUTIONS ▪ Scaling (Divide each row by the largest element in that row) x1 With Scaling: With Scaling and Pivoting: x2 2 0.00002 x1 x2 1 x1 x2 2 x1 x2 2 0.00002 x1 x2 1 28 Without Scaling: 2 x1 100,000 x2 100,000 29 MATLAB SCRIPT FILE: NGAUSSELIM.M 30 Example 9.5 >>Enter Matrix A > [3 -0.1 -0.2; 0.1 7 -0.3; 0.3 -0.2 10] A= 3.0000 -0.1000 -0.2000 0.1000 7.0000 -0.3000 0.3000 -0.2000 10.0000 >>Enter Solution Vector B > [7.85 -19.3 71.4] B= 7.8500 -19.3000 71.4000 31 S= 3.0000 -2.5000 7.0000 32 MATLAB SCRIPT FILE: GAUSSELIMPPIVOT.M Example 1x1 0 x2 2 x3 3 x4 1 1x1 2 x2 2 x3 3 x4 1 0 x1 1x2 1x3 4 x4 2 6 x1 2 x2 2 x3 4 x4 1 >>Enter Matrix A > [1 0 2 3; -1 2 2 -3; 0 1 1 4; 6 2 2 4] A= 1 0 2 3 -1 2 2 -3 0 1 1 4 6 2 2 4 33 >>Enter Solution Vector B > [1 -1 2 1] B= 1 -1 2 1 S= -0.1857 0.2286 -0.1143 0.4714 GAUSS-JORDAN METHOD It is a variation of Gauss elimination. Both methods use row operations to eliminate variables The major difference is that what an unknown is eliminated, it is eliminated from all other equations. Also, all rows (equations) are normalized by dividing by their pivot elements. The elimination phase produces an identity matrix. the back 34 It does not involve substitution phase. 35 MATLAB SCRIPT FILE: GAUSSJORDAN.M 36 Example 9.12 >>Dame la matriz aumentada Cuantas filas tiene la matriz: 3 Cuantas columnas tiene la matriz: 4 3.0 - 0.1 fila : 1 0.1 7.0 columna : 1Numero de esta fila y columna: 3 fila : 2 columna : 1Numero de esta fila y columna: 0.1 0.3 - 0.2 fila : 3 columna : 1Numero de esta fila y columna: 0.3 … Continuar ingresando los valores de la matriz aumentada a= 3.0000 -0.1000 -0.2000 7.8500 0.1000 7.0000 -0.3000 -19.3000 0.3000 -0.2000 10.0000 71.4000 a= 1.0000 0 0 3.0000 0 1.0000 0 -2.5000 0 0 1.0000 7.0000 - 0.2 7.85 - 0.3 - 19.3 10.0 71.4 37 resultado MATLAB’s Methods To solve the system Enter the following commands >> A=[3 -.1 -.2; .1 7 -0.3;.3 -.2 10]; >> b=[7.85;-19.3;71.4]; >> inv(A)*b ans = 38 3.0000 -2.5000 7.0000