Solving Smultaneous Equations with MATLAB

advertisement

Engineering H192 - Computer Programming

MATLAB: Simultaneous Equations

Lecture 22

Winter Quarter Gateway Engineering Education Coalition Lect 22 P. 1

Engineering H192 - Computer Programming

Simultaneous Equations with MATLAB

• MATLAB is very powerful 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, and other engineering applications. It can, of course, be used for any application with n equations and n unknowns.

• For example, suppose that a Calculus text (C), a Physics text (P) and a Graphics text (G) can be purchased for $146.

Then suppose the Calculus and Graphics texts can be purchased for $87 and the Physics and Calculus texts can be purchased for $105. How much does each text cost?

Winter Quarter Gateway Engineering Education Coalition Lect 22 P. 2

Engineering H192 - Computer Programming

Simultaneous Equations with MATLAB

• The problem can be written as:

1P + 1C + 1G = 146

1C + 1G = 87

1P + 1C = 105

>>Books=[1,1,1;0,1,1;1,1,0]

Books =

1 1 1

0 1 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 Gateway Engineering Education Coalition Lect 22 P. 3

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

Engineering H192 - Computer Programming

Gateway Engineering Education Coalition Lect 22 P. 4

Engineering H192 - 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 Gateway Engineering Education Coalition Lect 22 P. 5

Engineering H192 - 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 1 1 146

0 1 1 87

1 1 0 105

Winter Quarter Gateway Engineering Education Coalition Lect 22 P. 6

Engineering H192 - 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 Gateway Engineering Education Coalition Lect 22 P. 7

Download