Engineering H 192 Computer Programming MATLAB Simultaneous Equations

Engineering H 192 - Computer Programming MATLAB: Simultaneous Equations Lecture 22 The Ohio State University Gateway Engineering Education Coalition 1

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • MATLAB is a very powerful tool for solving systems of N equations with N unknowns. • It is used extensively for analyzing: – electric power grids and circuits – fluid flow in piping systems – numerous other engineering applications. • It can, of course, be used for any application with n equations and n unknowns. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 2

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • For example, let's suppose that: – a Calculus text (C), a Physics text (P) and a Graphics text (G) can be purchased for $146. – a Calculus text and a Graphics text can be purchased for $87 – a Physics text and Calculus text can be purchased for $105. • How much does each text cost? Winter Quarter The Ohio State University Gateway Engineering Education Coalition 3

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • The problem can be written as: 1 P + 1 C + 1 G = 146 1 C + 1 G = 87 1 P + 1 C = 105 >>Books=[1, 1, 1; 0, 1, 1; 1, 1, 0] Books = 1 1 1 0 • In MATLAB, the coefficients of the variables P, C & G are placed in a matrix and the Dollar values in a column vector. >>Dollars=[146; 87; 105] Dollars = 146 87 105 Winter Quarter The Ohio State University Gateway Engineering Education Coalition 4

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • Now, the inverse of the matrix Books is multiplied by the vector Dollars: >>Cost = inv (Books) * Dollars Cost = 59 46 41 • This vector contains the cost of P in row one, C in row 2 and G in row 3. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 5

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • Another MATLAB solution would be to use the reverse division sign as follows: >>Cost = Books Dollars Cost = 59 46 41 Winter Quarter The Ohio State University Gateway Engineering Education Coalition 6

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • A third MATLAB solution can be obtained by using the Row Reduced Echelon Form (rref) function as follows. First combine the matrix and vector: EDU» COMBINED=[Books Dollars] COMBINED = 1 146 0 1 1 87 1 1 0 105 Winter Quarter The Ohio State University Gateway Engineering Education Coalition 7

Engineering H 192 - Computer Programming Simultaneous Equations with MATLAB • Then invoke the rref function with the resulting matrix. >> Cost = rref (COMBINED) Cost = 1 0 0 59 0 1 0 46 0 0 1 41 • The last column in the new matrix is the cost vector. Winter Quarter The Ohio State University Gateway Engineering Education Coalition 8
- Slides: 8