Gaussian elimination Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Scientific Computing Linear Systems – Gaussian Elimination.
Advertisements

Systems of Linear Equations
CISE301_Topic3KFUPM1 SE301: Numerical Methods Topic 3: Solution of Systems of Linear Equations Lectures 12-17: KFUPM Read Chapter 9 of the textbook.
Chapter 9 Gauss Elimination The Islamic University of Gaza
P1 RJM 07/08/02EG1C2 Engineering Maths: Matrix Algebra 5 Solving Linear Equations Given the equations 3x + 4y = 10 2x + z = 7 y + 2z = 7 Solve by removing.
Linear Systems of Equations Ax = b Marco Lattuada Swiss Federal Institute of Technology - ETH Institut für Chemie und Bioingenieurwissenschaften ETH Hönggerberg/
Matrices and Systems of Equations
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 14 Elimination Methods.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 15 Solution of Systems of Equations.
ECIV 520 Structural Analysis II Review of Matrix Algebra.
10.1 Gaussian Elimination Method
Copyright © Cengage Learning. All rights reserved. 7.6 The Inverse of a Square Matrix.
Elementary Linear Algebra Howard Anton Copyright © 2010 by John Wiley & Sons, Inc. All rights reserved. Chapter 1.
Multivariate Linear Systems and Row Operations.
Elementary Linear Algebra Howard Anton Copyright © 2010 by John Wiley & Sons, Inc. All rights reserved. Chapter 1.
Multivariable Linear Systems
Scientific Computing Linear Systems – LU Factorization.
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Matrices Square is Good! Copyright © 2014 Curt Hill.
THU, JAN 8, 2015 Create a “Big Book of Matrices” flip book using 4 pages. Do not make your tabs big! BIG BOOK OF MATRICES What is a Matrix? Adding & Subtracting.
Square n-by-n Matrix.
Copyright © 2011 Pearson, Inc. 7.3 Multivariate Linear Systems and Row Operations.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
MA2213 Lecture 5 Linear Equations (Direct Solvers)
Matrix Algebra. Quick Review Quick Review Solutions.
Linear Systems Gaussian Elimination CSE 541 Roger Crawfis.
8.1 Matrices and Systems of Equations. Let’s do another one: we’ll keep this one Now we’ll use the 2 equations we have with y and z to eliminate the y’s.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Review of Matrices Or A Fast Introduction.
Matrix Solutions to Linear Systems. 1. Write the augmented matrix for each system of linear equations.
Matrices Jordi Cortadella Department of Computer Science.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Yasser F. O. Mohammad Assiut University Egypt. Previously in NM Introduction to NM Solving single equation System of Linear Equations Vectors and Matrices.
CMPS 1371 Introduction to Computing for Engineers MATRICES.
8.1 Matrices & Systems of Equations
Unit 6 : Matrices.
10.4 Matrix Algebra 1.Matrix Notation 2.Sum/Difference of 2 matrices 3.Scalar multiple 4.Product of 2 matrices 5.Identity Matrix 6.Inverse of a matrix.
13.6 MATRIX SOLUTION OF A LINEAR SYSTEM.  Examine the matrix equation below.  How would you solve for X?  In order to solve this type of equation,
Jordi Cortadella Dept. of Computer Science, UPC
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Section 7-3 Solving 3 x 3 systems of equations. Solving 3 x 3 Systems  substitution (triangular form)  Gaussian elimination  using an augmented matrix.
Chapter 9 Gauss Elimination The Islamic University of Gaza
ECE 530 – Analysis Techniques for Large-Scale Electrical Systems Prof. Hao Zhu Dept. of Electrical and Computer Engineering University of Illinois at Urbana-Champaign.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
7.3 & 7.4 – MATRICES AND SYSTEMS OF EQUATIONS. I N THIS SECTION, YOU WILL LEARN TO  Write a matrix and identify its order  Perform elementary row operations.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
10.4 Matrix Algebra 1.Matrix Notation 2.Sum/Difference of 2 matrices 3.Scalar multiple 4.Product of 2 matrices 5.Identity Matrix 6.Inverse of a matrix.
Matrices and Matrix Operations. Matrices An m×n matrix A is a rectangular array of mn real numbers arranged in m horizontal rows and n vertical columns.
2.5 – Determinants and Multiplicative Inverses of Matrices.
1 1.4 Linear Equations in Linear Algebra THE MATRIX EQUATION © 2016 Pearson Education, Ltd.
Linear Algebra Engineering Mathematics-I. Linear Systems in Two Unknowns Engineering Mathematics-I.
1 SYSTEM OF LINEAR EQUATIONS BASE OF VECTOR SPACE.
10.4 Matrix Algebra. 1. Matrix Notation A matrix is an array of numbers. Definition Definition: The Dimension of a matrix is m x n “m by n” where m =
Numerical Computation Lecture 6: Linear Systems – part II United International College.
1 Numerical Methods Solution of Systems of Linear Equations.
College Algebra Chapter 6 Matrices and Determinants and Applications
12-1 Organizing Data Using Matrices
Lecture 2 Matrices Lat Time - Course Overview
Reasoning with invariants
Chapter 8: Lesson 8.1 Matrices & Systems of Equations
Linear Algebra Lecture 15.
Matrix Operations SpringSemester 2017.
Elementary Matrix Methid For find Inverse
Review of Matrix Algebra
Numerical Analysis Lecture14.
Linear Systems Numerical Methods.
1.8 Matrices.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
Linear Systems of Equations: solution and applications
Presentation transcript:

Gaussian elimination Jordi Cortadella Department of Computer Science

System of Linear Equations Introduction to Programming© Dept. CS, UPC2

System of Linear Equations Introduction to Programming© Dept. CS, UPC3 Gaussian elimination Back-substitution

Gaussian elimination An essential algorithm in Linear Algebra with multiple applications: – Solving linear systems of equations – Finding the inverse of a matrix – Computing determinants – Computing ranks and bases of vector spaces Named after Carl Friedrich Gauss ( ), but known much before by the Chinese. Alan Turing contributed to provide modern numerical algorithms for computers (characterization of ill-conditioned systems). Introduction to Programming© Dept. CS, UPC4

System of Linear Equations Pre: // Type definitions for real vectors and matrices typedef vector rvector; typedef vector rmatrix; // Pre: A is an nn matrix, b is an n-element vector. // Returns x such that Ax=b. In case A is singular, // it returns a zero-sized vector. // Post: A and b are modified. rvector SystemEquations(rmatrix& A, rvector& b) { bool invertible = GaussElimination(A, b); if (not invertible) return rvector(0); // A is in row echelon form return BackSubstitution(A, b); } Introduction to Programming© Dept. CS, UPC5

Back-substitution Introduction to Programming© Dept. CS, UPC6 = Axb Ax=b Assumption: triangular system of equations without zeroes in the diagonal

Back-substitution Introduction to Programming© Dept. CS, UPC7 = i 0 n-1

Back-substitution Introduction to Programming© Dept. CS, UPC8 = i 0 n-1

Back-substitution Pre: // Pre: A is an invertible nn matrix in row echelon form, // b is a n-element vector // Returns x such that Ax=b rvector BackSubstitution(const rmatrix& A, const rvector& b) { int n = A.size(); rvector x(n); // Creates the vector for the solution // Calculates x from x[n-1] to x[0] for (int i = n - 1; i >= 0; --i) { // The values x[i+1..n-1] have already been calculated double s = 0; for (int j = i + 1; j < n; ++j) s = s + A[i][j]x[j]; x[i] = (b[i] – s)/A[i][i]; } return x; } Introduction to Programming© Dept. CS, UPC9

Gaussian elimination Introduction to Programming© Dept. CS, UPC10 A b k Invariant: The rows 0..k have been reduced (zeroes at the left of the diagonal) 0 n-1

Gaussian elimination Introduction to Programming© Dept. CS, UPC11 What is the best pivot? (max absolute value) swap rows

Gaussian elimination Introduction to Programming© Dept. CS, UPC12 k j i For each row i, add a multiple of row k such that A[i][k] becomes zero. The coefficient is –A[i][k]/A[k][k].

Discussion: how large should a pivot be? Gaussian elimination Pre: // Pre: A is an nn matrix, b is a n-element vector. // Returns true if A is invertible, and false if A is singular. // Post: If A is invertible, A and b are the result of // the Gaussian elimination (A is in row echelon form). bool GaussianElimination(rmatrix& A, rvector& b) { int n = A.size(); // Reduce rows 0..n-1 for (int k = 0; k < n; ++k) { // Rows 0..k have already been reduced int imax = find_max_pivot(A, k); // finds the max pivot if (abs(A[imax][k]) < 1e-10) return false; // Singular matrix swap(A[k], A[imax]); swap(b[k], b[imax]); // Swap rows k and imax // Force 0’s in column A[k+1..n-1][k] for (int i = k + 1; i < n; ++i) { double c = A[i][k]/A[k][k]; // coefficient to scale row A[i][k] = 0; for (int j = k + 1; j < n; ++j) A[i][j] = A[i][j] – cA[k][j]; b[i] = b[i] – cb[k]; } } return true; // We have an invertible matrix } Introduction to Programming© Dept. CS, UPC13

Gaussian elimination Pre: // Pre: A is an nn matrix, k is the index of a row. // Returns the index of the row with max absolute value // for the subcolumn A[k..n-1][k]. int find_max_pivot(const rmatrix& A, int k) { int n = A.size(); double imax = k; // index of the row with max pivot double max_pivot = abs(A[k][k]); for (int i = k + 1; i max_pivot) { max_pivot = a; imax = i; } } return imax; } Introduction to Programming© Dept. CS, UPC14

Solving multiple systems of equations Introduction to Programming© Dept. CS, UPC15 = = = =

Back-substitution Pre: // Pre: A is an invertible nn matrix in row echelon form, // B is a nm matrix // Returns X such that AX=B rmatrix BackSubstitution(const rmatrix& A, const rmatrix& B) { int n = A.size(); int m = B[0].size(); rmatrix X(n, rvector(m)); // Creates the solution matrix // Calculates X from X[n-1] to X[0] for (int i = n - 1; i >= 0; --i) { // The values X[i+1..n-1] have already been calculated for (int k = 0; k < m; ++k) { double s = 0; for (int j = i + 1; j < n; ++j) s = s + A[i][j]X[j][k]; X[i][k] = (B[i][k] – s)/A[i][i]; } } return X; } Introduction to Programming© Dept. CS, UPC16

Gaussian elimination Pre: // Pre: A is an nn matrix, B is an nm matrix. // Returns true if A is invertible, and false if A is singular. // Post: If A is invertible, A and B are the result of // the Gaussian elimination (A is in row echelon form). bool GaussianElimination(rmatrix& A, rmatrix& B) { int n = A.size(); int m = B[0].size(); // Reduce rows 0..n-1 for (int k = 0; k < n; ++k) { // Rows 0..k have already been reduced int imax = find_max_pivot(A, k); // finds the max pivot if (abs(A[imax][k]) < 1e-10) return false; // Singular matrix swap(A[k], A[imax]); swap(B[k], B[imax]); // Swap rows k and imax // Force 0’s in column A[k+1..n-1][k] for (int i = k + 1; i < n; ++i) { double c = A[i][k]/A[k][k]; // coefficient to scale row A[i][k] = 0; for (int j = k + 1; j < n; ++j) A[i][j] = A[i][j] – cA[k][j]; for (int l = 0; l < m; ++l) B[i][l] = B[i][l] – cB[k][l]; } } return true; // We have an invertible matrix } Introduction to Programming© Dept. CS, UPC17

Inverse of a Matrix Reduce the problem to a set of systems of linear equations: Introduction to Programming© Dept. CS, UPC18

Computing determinants Rules of determinants: 1.Swapping two rows: determinant multiplied by -1 2.Multiplying a row by a scalar: the determinant is multiplied by the same scalar 3.Adding to one row the scalar multiple of another row: determinant does not change Algorithm: – Do Gaussian elimination and remember the number of row swaps (odd or even). – The determinant is the product of the elements in the diagonal (negated in case of an odd number of swaps). – Rule 2 is not used, unless some row is scaled. Introduction to Programming© Dept. CS, UPC19

Summary Introduction to Programming© Dept. CS, UPC20