Вы находитесь на странице: 1из 10

Lab #8: Linear Algebraic Equations and Matrices

1 Matrix
One- or two-dimensional array of numbers. rows (1st index) columns (2nd index) Individual elements: ,

Matrix dimensions = (# rows # columns) (2 4) matrix

2 Matrix Operations
2.1 Matrix Addition Matrices added must be the same size. Individual elements are added.

Matrix addition is commutative: In Python: For addition, matrices can be defined as either arrays or matrices

* Notes based on Chapters 8 and 11 from Applied Numerical Methods with MATLAB for Engineers and Scientists, Steven C. Chapra, 3rd Edition, McGraw-Hill, 2012.

2.2 Multiplication by a scalar

In Python: For multiplication by a scalar, the matrix can be defined as either an array or a matrix

2.3 Multiplication of matrices Matrices must have compatible sizes. For : - Number of columns in must be equal to number of rows in . - will have the same number of rows as and the same number of columns as . Example:

equal (multiplication is possible) (3 3) (3 2) = (3 2) size of product

Is possible? (3 2) (3 3) not equal (multiplication is not possible) Therefore, matrix multiplication is not commutative.

How to multiply matrices For , elements of the resulting matrix = (row in )* (column in )

(3 3) (3 1) = (3 1)

In Python: For matrix multiplication, the matrices have to be defined using the command matrix()

2.4 Identity Matrix, For a squared matrix , an identity matrix exists such that

The diagonal elements are 1s, and the off-diagonal elements are 0s

In Python: The numpy.identity(n) function creates an array that contains an (n n) identity matrix. If this is multiplied by an array, Python will perform element-by-element calculation. If it is multiplied by a matrix, then the output will be the same matrix entered as input:

2.5 Transpose of a Matrix,

or

Obtained by switching rows and columns of a matrix:

In Python: The numpy.transpose() function works on both arrays and matrices.

2.6 Determinant of a Matrix, Scalar that can be calculated only for a squared matrix, and provides some information about it.

For a (2 2) matrix:

, the determinant is:

For a (3 3) matrix:

, the determinant is:

In Python: The numpy.det() function works on both arrays and matrices.

2.7 Inverse of a Matrix, For a squared matrix , if its inverse matrix For a (2 2) matrix: exists, then

, the inverse is:

We can see from the equation that the inverse is only defined if the determinant In Python: The numpy.inv() function works on both arrays and matrices but, when multiplying the inverse by the original matrix to get the identity matrix, need to make sure the original matrix is defined as a matrix.

3 Linear Simultaneous Equations


Systems of equations that contain only linear terms of the independent variables. Example:

How to solve these for 3.1 Matrix Inversion

and ?

First, express the system of equations in matrix form:

Then, multiply by

on both sides of the matrix equation:

For our 2-equation system:

In Python:

Example

A steel company manufactures four different types of steel alloys, called A1, A2, A3, and A4. Each alloy contains small amounts of chromium (Cr), molybdenum (Mo), titanium (Ti), and nickel (Ni). The required composition of each alloy is given below. Alloy % Cr % Mo % Ti % Ni A1 1.6 0.7 1.2 0.3 A2 0.6 0.3 1.0 0.8 A3 0.3 0.7 1.1 1.5 A4 1.4 0.9 0.7 2.2 In making these allows, the company uses the following amounts of alloying materials: Material Availability (kg/day) Cr 1200 Mo 800 Ti 1000 Ni 1500 Use matrix inversion to determine the daily production rate for each alloy in metric tons per day (1 metric ton = 1000kg).

First, we designate an array , that contains all the variables we are solving for: = production rate for alloy 1 in kg/day = production rate for alloy 2 in kg/day = production rate for alloy 3 in kg/day = production rate for alloy 4 in kg/day Our balance equations for each of the elements are: For Cr: For Mo: For Ti: For Ni: Expressed in matrix form, this is:

We can now solve for the vector using matrix inversion. (Remember to convert the results in kg/day to the requested unit of metric tons).

So the company produces 28.8 metric tons of alloy A1, 6.3 metric tons of alloy A2, 25.3 metric tons of alloy A3 and 44.7 metric tons of alloy A4.

4. Practice Problem
Five reactors linked by pipes are shown in the figure:

The rate of mass flow through each pipe is computed as the product of flow ( ) and concentration ( ). At steady state, the mass flow into and out of each reactor must be equal. For example, for the first reactor, a mass balance can be written as

Write mass balances for the remaining reactors and express the equations in matrix form. Then use matrix inversion with Python to solve for the concentrations in each reactor.

5. Solution
Mass balances in each reactor: For reactor 1: For reactor 2: For reactor 3: For reactor 4: For reactor 5: Rearranging the equations gives:

In matrix form:

Now solve using matrix inversion:

The concentrations for the different tanks are:

Вам также может понравиться