Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.

Slides:



Advertisements
Similar presentations
Adding & Subtracting Matrices
Advertisements

Chapter 2 Solutions of Systems of Linear Equations / Matrix Inversion
SOLVING SYSTEMS OF LINEAR EQUATIONS. Overview A matrix consists of a rectangular array of elements represented by a single symbol (example: [A]). An individual.
Lecture 9: Introduction to Matrix Inversion Gaussian Elimination Sections 2.4, 2.5, 2.6 Sections 2.2.3, 2.3.
Part 3 Chapter 9 Gauss Elimination
Chapter 9 Gauss Elimination The Islamic University of Gaza
Linear Algebraic Equations
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
ECIV 520 Structural Analysis II Review of Matrix Algebra.
Algorithm for Gauss elimination 1) first eliminate for each eq. j, j=1 to n-1 for all eq.s k greater than j a) multiply eq. j by a kj /a jj b) subtract.
Chapter 8 Arrays and Strings
Mujahed AlDhaifallah (Term 342) Read Chapter 9 of the textbook
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. by Lale Yurttas, Texas A&M University Part 31 Chapter.
LIAL HORNSBY SCHNEIDER
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Matrix Solution of Linear Systems The Gauss-Jordan Method Special Systems.
Solving Systems of Equations and Inequalities
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
ECON 1150 Matrix Operations Special Matrices
Solving Systems of Equations and Inequalities Section 3.1A-B Two variable linear equations Section 3.1C Matrices Resolution of linear systems Section 3.1D.
Copyright © Cengage Learning. All rights reserved. 7.4 Matrices and Systems of Equations.
AN INTRODUCTION TO ELEMENTARY ROW OPERATIONS Tools to Solve Matrices.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Linear Systems Gaussian Elimination CSE 541 Roger Crawfis.
MATH 250 Linear Equations and Matrices
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Multi-Dimensional Arrays
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
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.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 8 Arrays and Strings
Copyright © 2013, 2009, 2005 Pearson Education, Inc. 1 5 Systems and Matrices Copyright © 2013, 2009, 2005 Pearson Education, Inc.
WEEK 8 SYSTEMS OF EQUATIONS DETERMINANTS AND CRAMER’S RULE.
Yasser F. O. Mohammad Assiut University Egypt. Previously in NM Introduction to NM Solving single equation System of Linear Equations Vectors and Matrices.
ΑΡΙΘΜΗΤΙΚΕΣ ΜΕΘΟΔΟΙ ΜΟΝΤΕΛΟΠΟΙΗΣΗΣ 4. Αριθμητική Επίλυση Συστημάτων Γραμμικών Εξισώσεων Gaussian elimination Gauss - Jordan 1.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Chapter 9 Gauss Elimination The Islamic University of Gaza
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
ECE 530 – Analysis Techniques for Large-Scale Electrical Systems Prof. Hao Zhu Dept. of Electrical and Computer Engineering University of Illinois at Urbana-Champaign.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
Part 3 Chapter 9 Gauss Elimination PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The McGraw-Hill Companies,
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Linear Algebra Review Tuesday, September 7, 2010.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Part 3 Chapter 9 Gauss Elimination
Copyright © 2014, 2010, 2007 Pearson Education, Inc.
Gaussian Elimination and Gauss-Jordan Elimination
ECE 3301 General Electrical Engineering
Spring Dr. Jehad Al Dallal
5 Systems of Linear Equations and Matrices
Linear Equations.
Instruction of Chapter 8 on
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Part 3 - Chapter 9.
Gaussian Elimination and Gauss-Jordan Elimination
Metode Eliminasi Pertemuan – 4, 5, 6 Mata Kuliah : Analisis Numerik
Matrix Solutions to Linear Systems
Review of Matrix Algebra
Numerical Analysis Lecture10.
Array Data Structure Chapter 6
Lecture 13 Simultaneous Linear Equations – Gaussian Elimination (2) Partial Pivoting Dr .Qi Ying.
Ax = b Methods for Solution of the System of Equations (ReCap):
Presentation transcript:

Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lectures 10, Matrices Saturday 3 July 2010

Overview  Handling matrices Multi-dimensional arrays  Matrix computations

Multi-dimensional arrays  We can declare and use arrays which have more than one dimension int A[50][40]  Declares a two dimensional array with 50 rows and 40 columns  Each element is now accessed by a reference requiring two index expressions to be specified A[i][j] = 37;  The row index can have a value from 0 to 49, the column index from 0 to 39  All rules for index expression apply to index for each dimension

Matrix  Matrices arise in many applications Represent a system of equations in multiple variables  For example, the following equations in two variables 2y + 4z = 8 eq.1 4y + 3z = 1 eq.2 can be represented as 2 4 y z = 1

Gaussian elimination  This method depends upon some standard facts: The system of equations is not affected if an equation is multiplied by a constant 2 y + 4 z = 8 is same as 1 y + 2 z = 4 (multiply the equation by 0.5)  So we now have 1 y + 2 z = 4 eq.1’ 4 y + 3 z = 1 eq.2

Gaussian elimination …  Representing these equation in the form of matrices: 1 y + 2 z = y 4 eq.1’ 4 y + 3 z = z = 1 eq.2 Note that the original Coefficient matrix was 2 4 y 8 eq z = 1 eq.2

Gaussian elimination … If an equation is replaced by a linear combination of itself and any other row, the system of equations remains the same We replace eq.2 by subtracting from it, 4 times the first equation (eq.1’). 1 y + 2 z = y 4 eq.1’ (* 4) 4 y + 3 z = z = 1 eq.2 We get 1 y + 2 z = y 4 eq.1’ 0 y - 5 z = z = -15 eq.2’

Gaussian elimination …  If we further divide eq.2’ by -5, we will get 1 y + 2 z = y 4 eq.1’ 0 y + 1 z = z = 3 eq.2’’ Starting from the last equation, and using back substitution, we get the solution for all variables. Thus 0 * y + 1 * z = 3 gives us z = 3 1 * y + 2 * 3 = 4 gives us y + 6 = 4 which gives us y = -2

Gaussian elimination …  The essence of the method is to reduce the coefficient- matrix to an upper diagonal matrix with all elements on the diagonal as 1, and then use back-substitution  The process is susceptible to round off errors  There are other variations, such as: Gauss Jordan elimination, Pivoting L U decomposition  A useful reference: Numerical recipes in C, also in C++, Fortran (William H Press, Saul A Teukolsky, William T Vetterling, Brian P Flannery)

General representation In general, a system of linear equations in N variables can be represented by a 11 a 12 a a 1n x 1 b 1 a 21 a 22 a a 2n x 2 = b 2 a 31 a 32 a a 3n x 3 b 3... a n1 a n2 a n3... a nn x n b n

General representation C style Since the matrix indices in c vary from 0 to n-1 We may use another representation: a 00 a 01 a a 0n-1 x 0 b 0 a 10 a 11 a a 1n-1 x 1 = b

Diagonal matrix C style The Gaussian elimination technique essentially reduces the coefficient matrix to an upper triangular form: 1 a 01 a a 0n-1 x 0 b a a 1n-1 x 1 = b

General representation C style …  A general system of n equations can be written as a[][] x[] = b[]  With the coefficient matrix in upper triangular form, we now have the following system using which back substitution can be applied x[0] + a[0][1] x[1] + … + a[0][n-1] x[n-1] = b[0] x[1] + … + a[1][n-1] x[n-1] = b[1]... x[n-1] = b[n-1]

Example of 2 variables  Consider our example equations in two variables 2y + 4z = 20 y + 3z = 11  Representing y by x[0] and z by x[1], this can can be represented as: a[0][0] x[0] + a[0][1] x[1] = b[0] a[1][0] x[0] + a[1][1] x[1] = b[1] Where a[0][0] is 2, a[0][1] is 4, a[1][0] is 4, a[1][1] is 3 and b[0] is 20, b[1] = 11

gauss.cpp #include using namespace std; int main(){ int i, j, k, n; float MatA[100][100], MatB[100], X[100]; float Divisor, Factor, sum;

gauss.cpp … cin >> n; //reading matrix A for(i=0; i< n; i++){ for(j=0; j < n; j++){ cin >> MatA[i][j]; } //reading matrix B for(i=0; i< n; i++){ cin >> MatB[i]; }

gauss.cpp … //Gauss elimination for (i=0; i< n; i++){ // divide each row by the coeffs on the diagonal Divisor = MatA[i][i]; MatA[i][i] = 1.0; // Now recalcualte all coefficients in that row for (j = i+1; j < n; j++){ MatA[i][j] = MatA[i][j]/Divisor; } //normalize the corresponding rhs element MatB[i] = MatB[i]/Divisor;

gauss.cpp … // now replace subsequent rows, by subtracting the // appropriate portion of the ith eqaution from it if (i+1 < n) { for (k=i+1; k<n; k++){ Factor = MatA[k][i]; MatA[k][i] = 0.0; for (j = i+1; j < n; j++){ MatA[k][j] = MatA[k][j]-Factor*MatA[i][j]; } MatB[k] = MatB[k] - Factor * MatB[i]; }

gauss.cpp … // back substitution starting with last variable X[n-1] = MatB[n-1]; for (i = n-2; i>=0; i--){ // Sum up ith row using values of X already determined sum = 0.0; for (j = i+1; j < n; j++){ sum = sum + MatA[i][j] * X[j]; } X[i] = MatB[i] - sum; }

gauss.cpp … //output the results for(i=0;i< n;i++){ for (j = 0; j < n; j++) { cout << MatA[i][j] << " "; } cout << " " << MatB[i] << "\n"; } for (i=0; i<n; i++){ cout << "X[" << i << "] is: " cout << X[i] << "\n"; } return 0; }

Useful facilities from the OS  Ordinarily, we have been using the key board to input data, and the screen to see the output values.  If we have to repeatedly execute a program during debugging trials, the same input has to be given again and again. This is wasteful of our time.  If the input values are large in number, we have seen how a ‘file’ can be used. Such a file is pre-edited to contain the required input in the prescribed format For example: “rollmarks.txt”

Redirection  Even when we use cin >> …, for input, it is possible to ‘redirect’ the standard input file (stdin) to any other file of our choice when executing a program $./a.out < inputdata.txt This will read ALL input only from the named file.  It is similarly possible to redirect ALL output to a named file, instead of the standard output device (stdout) which is your terminal $./a.out > outputdatafile.txt  both redirections can be used simultaneously

Input datafile “coeffs.txt”

Sample results $./a.out result.txt $ cat result.txt X[0] is: 1 X[1] is: 2 X[2] is: 3 X[3] is: 4

Results should look better $ cat result.txt X[0] is: 1 X[1] is: 2 X[2] is: 3 X[3] is: 4

Need for debugging When you are testing a program and are not sure of the behaviour of your algorithm you may include output statements to print a few key variables at strategic points in your program These will help you identify errors Such statements are called debugging statements

Useful facilities from C (GCC) Ordinarily, once you correct these errors, you will like to remove such instructions In a large program, with many such debugging statements, it will amount to a lot of painful work C (gcc) provides us with a neat option. We can write statements enclosed in a special if statement written on two separate lines: #if xxxx #endif

Compiling with -D option “xxxx” is any name-tag of our choice for example, “notsure” #if notsure cout << i << “ “ << j << “ “ << sum[i][j]; #endif Ordinarily, c compiler ignores the block But if you compile your program with: $ c++ -D notsure prog.cpp these statements are compiled and are executed as part of your program