Bill Knowlton and Wan Kuang ECE and MSE Departments Boise State University

advertisement
Bill Knowlton and Wan Kuang
ECE and MSE Departments
Boise State University
Linear Algebra and Matrix Algebra Module
Linear equations are very common in Engineering and science problems. This tutorial will teach you how to use Mathematica to solve such equations. Given three linear equations,
4x + 3y + (-1) z = 9
5x + (-9) y+2 z = 3
x+y+z=1
the unknowns x, y, and z can be solved directly with Solve. For example,
Clear@x, y, zD
Solve@84 x + 3 y + H−1L z == 9, 5 x + H−9L y + 2 z == 3, x + y + z == 1<,
8x, y, z<D
99x →
72
114
25
==
,y→
,z→−
67
67
67
However a more efficient approach is to take advantage of matrix mathematics. The unknowns x, y, and z in the previous
equations can be written as,
4
i
j
j
j
j
j5
j
k1
3
−9
1
−1
2
1
xy i9y
y
i
z
j
z
z
j
z j
j z
z
z
j
yz
z
j
z=j
j3z
z
zj
z
j z
z j
j z
z
z
{k { k1{
The three unknowns can be calculated directly with LinearSolve. LinearSolve works on both numerical and
symbolic matrices, as well as SparseArray objects. For example,
M1 = 884, 3, −1<, 85, −9, 2<, 81, 1, 1<<;
M2 = 889<, 83<, 81<<;
xyzAnswer = LinearSolve@M1, M2D;
Print@"8x,y,z< = ", xyzAnswerD
8x,y,z< = 99
114
25
72
==
=, 9
=, 9−
67
67
67
Linear Algebra - Matrix Algebra.nb
2
in which, M1 is the coefficient matrix and M2 is a column vector representing the right-hand-side of the equations. Note
that the matrix can also be solved by multiplying the inversion of M1 and M2, however such an approach is far slower and
sometimes inaccurate when the determinant of M1 is small.
x
i
z
j y
z to obtain
j
yz
We can check the result from LinearSolve by using the Dot product between the two matrices, M1 and j
j
z
j z
kz{
M2:
M1.xyzAnswer
889<, 83<, 81<<
It agrees with the value in M2. It is important to remember that LinearSolve will return one of the possible solutions
for underdetermined linear equations however Solve will return a general solution.
Again, we can apply this approach to solve problem 4-14 where the systems of equations is given by:
Cp
ÅÅÅÅTÅÅÅÅ m + van = -s-pva
vam + vbn = -pvb
M1 = 88Cp ê T, v α<, 8v α, v β<<;
M2 = 88−s − p v α<, 8−p v β<<;
x = LinearSolve@M1, M2D
99
−s T α − p T v α2 + Cp p β
sTβ
=, 9
==
2
T v α2 − Cp β
T v α − Cp β
Assuming that Cp = Cv + T v a2 ê b, the solution simplifies as:
x ê. 8Cp → Cv + T v α ^ 2 ê β< êê Simplify H∗replace Cp with Cv+Tvα^2êβ∗L
99−
sT
sTα
=, 9−p +
==
Cv
Cv β
Linear Algebra - Matrix Algebra.nb
3
à Inverse Approach
As mentioned previously, the Inverse Approach is far slower and sometimes inaccurate when the determinant of M1 is
small. However, since this is the approach typically used in mathematics and engineering courses, it is useful to apply it.
We will apply it to several examples.
Given two linear equations:
am+bn=e
&
cm+dn=f
and defining the unknowns, m and n,matrices can be defined:
e
a b
m
M1 = J
N, x = J N and M2 = J N
f
c d
n
The unknowns m & n can then be solved for by using the following matrix methodology:
M1·X = M2
Finding the inverse matrix of M1, which is M1-1 , then we can solve for X :
X = M1-1 · M2
In this example, we use a 3x3 matrices. That is, 3 equations and 3 unknowns.
Clear@a, b, c, d, M1, M2, M3, IM1, x1, x2, x1x2, M2, m, nD
xy
4 3 −1 y
9 0 0y
i
i
i
j
z
j
z
j
z
j
z
j
z
j
z
j
z
j
z
j
yz
5 −9 2 z
3 0 0z
z
M1 = j
; IM1 = Inverse@M1D; b = j
; M2 = j
;
j
z
j
z
j
z
j
z
j
z
j
z
j z
1 {
1
0
0
z
k1 1
k
{
k {
Print@"Inverse of M1, M1−1 = ", MatrixForm@IM1DD
Inverse of M1, M1−1
11
i
j
67
j
j
j
3
j
j
= j
67
j
j
j
j − 14
k 67
4
67
−
5
67
1
67
3
67
13
67
51
67
y
z
z
z
z
z
z
z
z
z
z
z
{
Solving for X using :
X = M1−1 ⋅ M2
We have :
Print@"b = M1−1 ⋅M2 = ", MatrixForm@IM1.M2D, " =
MatrixForm@FullSimplify@IM1.M2DDD
i 114
j
67
j
j
j 25
j
b = M1−1 ⋅M2 = j
j
67
j
j
j
j − 72
k 67
0
0
0
0z
y
z
z
z
z
0z
=
z
z
z
z
z
0{
i 114
j
67
j
j
j
25
j
j
j
67
j
j
j
j − 72
k 67
0
0
0
0z
y
z
z
z
z
0z
z
z
z
z
0z
{
",
Linear Algebra - Matrix Algebra.nb
4
We can apply this approach to sovle problem Problem 4 −
14 in DeHoff where 2 x2 matrices are used.
Clear@y, yi, m, n, z, Cp, Cv, mn, α, β, v, T, pD
i Cp v α y
−s − p v α
m
z
T
z
j
y=j
z
j
z; yi = Inverse@yD; x = J n N; z = J −p v β N;
j
kvα vβ{
Print@"M1 = ", MatrixForm@yD, ";
X = ", MatrixForm@xD, "; M2 = ",
MatrixForm@zDD
Print@"M1−1 = ", MatrixForm@yiD, " = ", MatrixForm@FullSimplify@yiDDD
mn = yi.z; Cp = Cv +
T v α2
;
β
Print@"X=M1−1 ⋅M2 = ", MatrixForm@mnDD
i Cp
T
j
M1 = j
j
kvα
vαy
z
z
z;
vβ{
vβ
i
j
−v2 α2 + CpTv β
j
j
M1 = j
j
j
j
j − 2 2v αCp v β
k −v α + T
X = J
−
−1
−s − p v α
m
N; M2 = J
N
−p v β
n
vα
−v2 α2 + CpTv β
Cp
T I−v2 α2 + CpTv β M
y
i
z
z
j
z
z
j
= j
z
j
z
j
z
j
z
k
{
Tβ
−T v α2 +Cp β
Tα
T v α2 −Cp β
Tα
T v α2 −Cp β
Cp
v H−T v α2 +Cp βL
p v2 α β
v H−s−p v αL β
i
+
j
2
2
j
j
v JCv+ T vβα N β
v JCv+ T vβα N β
j
j
−v2 α2 +
−v2 α2 +
j
T
T
j
j
j
2
j
X=M1−1 ⋅M2 = j
j
p v JCv+ T vβα N β
j
v α H−s−p v αL
j
j
−
−
j
2
2
i
y
j
j
v JCv+ T vβα N β
v JCv+ T vβα N β z
j
j
z
j
z
j
2 α2 +
j
z
j −v2 α2 +
j
z
T
−v
j
z
j
T
T
j
z
j
z
k
k
{
Simplifying X = M1−1 ⋅ M2, we have :
Print@"X=M1−1 ⋅M2 = ", MatrixForm@FullSimplify@mnDDD
sT
i
j − Cv
j
X=M1−1 ⋅M2 = j
j
j
−p +
k
sTα
Cv β
y
z
z
z
z
z
{
y
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
z
{
y
z
z
z
z
z
z
{
Download