Selected Algebraic System Examples from Lectures
Matlab Examples
Matrix addition
>> A=[1 3 2; 2 4 5];
>> B=[3 -4 6; 1 -2 5];
>> D=A+B
D = 4 -1 8
3 2 10 Matrix multiplication
>> C=[2 3; -1 2; 4 -3];
>> E=A*C
E = 7 3
20 -1
Gaussian Elimination Example
Form augmented matrix
Eliminate x1 from second and third equations
1234
3345
1223~
1
3
1
234
345
223
3
2
1
Α
x
x
x
3/13/23/10
3/43/13/20
1223
3/413/823/830
3/533/1033/1040
1223
Gaussian Elimination Example
Eliminate x2 from third equation
Solve triangular system
12/100
3/43/13/20
1223
6/43/16/13/200
3/43/13/20
1223
2
3
3
1
3/4
1
2/100
3/13/20
223
3
2
1
3
2
1
x
x
x
x
x
x
Matlab Example
Ax = b x = A-1b (discuss next lecture)
>> A=[3 -2 2; -5 4 -3; -4 3 -2];
>> rank(A)
ans = 3
>> b=[-1; 3; 1];
>> x=inv(A)*b
x = 3.0000
3.0000
-2.0000
Determinant Examples
By hand
Using Matlab
>> A=[1 2; 3 4];
>> det(A)
ans = -2
>> A=[1 2 3;4 5 6;7 8 9];
>> det(A)
ans = 0
2)3)(2()4)(1(43
21
0)7)(5)(3()9)(4)(2()8)(6)(1(
)8)(4)(3()7)(6)(2()9)(5)(1(
987
654
321
Gauss-Jordan Elimination
Eliminate x2 entry from third row
Make the diagonal elements unity
114500
013720
001211
011031722200
013720
001211
2.02.08.0100
05.05.15.310
001211
)1(2.0)1(2.0)4(2.0)5(2.000
0)1(5.0)3(5.0)7(5.0)2(5.00
00121)1(
Gauss-Jordan Elimination cont.
Eliminate first two entries in third column
Obtain identity matrix
Matrix inverse
2.02.08.0100
7.02.03.1010
4.04.06.0011
2.02.08.0100
)2.0(5.30)2.0(5.35.0)8.0(5.35.1)1(5.35.3)0(5.310
)2.0(20)2.0(20)8.0(21)1(22)0(21)0(21
1
2.02.08.0100
7.02.03.1010
3.02.07.0001
2.02.08.0100
7.02.03.1010
7.04.0)2.0(4.0)3.1(6.00111
AI
2.02.08.0
7.02.03.1
3.02.07.01A
Gauss-Jordan Elimination cont.
Verify result
0)2.0)(2()7.0)(1()3.0)(1(
0)2.0)(2()2.0)(1()2.0)(1(
1)8.0)(2()3.1)(1()7.0)(1(2.02.08.0
7.02.03.1
3.02.07.0
431
113
211
13
12
11
1
c
c
c
AAC
IAAC
100
010
0011
Matlab Examples
>> A=[-1 1 2; 3 -1 1; -1 3 4];
>> inv(A)
ans =
-0.7000 0.2000 0.3000
-1.3000 -0.2000 0.7000
0.8000 0.2000 -0.2000
>> A=[1 2; 3 5];
>> b=[1; 2];
>> x=inv(A)*b
x =
-1.0000
1.0000
Ill-Conditioned Matrix Example
Example
» represents measurement error in b2» Two rows (columns) are nearly linearly dependent
Analytical solution
10% error ( = 0.1)
1
1
11
0001.19999.0x
5.0
5.0
5.49995.0
5.50005.0 0 xx
45.499
55.500
)1.0)(5.4999(5.0
)1.0)(5.5000(5.0x
Matlab Example
>> A=[1 2; 3 5];
>> cond(A)
ans = 38.9743 (well conditioned)
>> A=[0.9999 -1.0001; 1 -1];
>> cond(A)
ans = 2.0000e+004 (poorly conditioned)
>> b=[1; 1.1]
>> x=inv(A)*b
x =
500.5500
499.4500
Matlab: Vector and Matrix Norms
>> x=[2 -3 0 1 -4]';
>> norm(x,2)
ans = 5.4772
>> norm(x,inf)
ans = 4
>> A = [5 1 1; 1 4 2; 1 2 4];
>> norm(A,1)
ans = 7
>> norm(x,inf)
ans = 7
>> norm(A,'fro')
ans = 8.3066
Condition Number
Definition: (A) = ||A|| ||A-1|| A “large” condition number indicates an ill-conditioned
matrix Well conditioned matrix
Ill-conditioned matrix
75.3)30(56
1)7()(
1992
9192
2212
56
1
421
241
115
1
1
1
1
AAA
AA
20002)0001.2(0002.0
1)0001.2()(
0001.11
10001.1
0002.0
1
0001.11
10001.1
1
1
1
1
AAA
AA
Condition Number cont.
Effect of data errors
» Small data errors can lead to large solution errors
» Bound can be very conservative
Example
b
bA
x
xbbAx
)(
0143.0
0143.0
0143.0
0143.9
9857.4
0143.2
1.0
1.0
1.0
28
0
14
421
241
1159
5
2
28
0
14
421
241
115
xxx
xx
0429.0429.0)16(42
3.0)75.3()(
11
11
xb
bAx
Matlab: Condition Number
>> A = [5 1 1; 1 4 2; 1 2 4];
>> AI = inv(A)
AI =
0.2143 -0.0357 -0.0357
-0.0357 0.3393 -0.1607
-0.0357 -0.1607 0.3393
>> norm(A,1)*norm(AI,1)
ans = 3.7500
>> cond(A,1)
ans = 3.7500
>> norm(A,inf)*norm(AI,inf)
ans = 3.7500
>> cond(A,inf)
ans = 3.7500
Overdetermined Systems cont.
Normal equations: (ATA)x = ATb
Solution: x = (ATA)-1ATb» ATA must be nonsingular» (ATA)-1AT is called the left inverse matrix
Example
17
1
25
25
25
112
221
62
29)(
62
29
12
12
21
112
221
25
25
25
12
12
21
1
1 bAAAx
AAx
TT
T
Underdetermined Systems cont.
Example
5.2
5.13
7
25
25
62
29
12
12
21
)(
62
29
12
12
21
112
221
25
25
112
221
1
1bAAAx
AAx
TT
T
Matlab: Linear Algebraic Systems
>> A=[-1 1 2; 3 -1 1; -1 3 4];
>> b=[1 2 3]';
>> x=inv(A)*b
x =
0.6000
0.4000
0.6000
>> x = linsolve(A,b)
x =
0.6000
0.4000
0.6000
>> x=A\b
x =
0.6000
0.4000
0.6000
Matlab: Linear Algebraic Systems cont.
>> A = [1 2; 2 -1; -2 -1];
>> b = [25 -25 -25]';
>> x = linsolve(A,b)
x =
-1.0000
17.0000
>> A=[1 2 -2; 2 -1 -1];
>> b=[25 -25]';
>> x = linsolve(A,b)
x = x =
-5.0000 -7
15.0000 not equal to 13.5
0 2.5
Plotting a Function
y17x
0.6 xy2
5x
0.08 x
» x = [0.1:0.1:10];» y1 = 7*x./(0.6 + x);» y2 = 5*x ./ (0.08+x);
» plot(x,y1,x,y2)» xlabel('x')» ylabel('y')» legend('y1','y2')» figure
» subplot(2,1,1)» plot(x,y1)» ylabel('y1')» subplot(2,1,2)» plot(x,y2)» ylabel('y2')
Square Systems
>>A=[-1 1 2; 3 -1 1; -1 3 4];
>> b=[2 6 4]';
>> x=inv(A)*b;
>> x=A\b;
>> x=linsolve(A,b)
x =
1.0000
-1.0000
2.0000
x1 x2 2x3 2
3x1 x2 x3 6
x1 3x2 4x3 4
Non-Square Systems
>> A = [1 2; 2 -1; -2 -1];
>> b = [25 -25 -25]';
>> x=A\b
x =
-1.0000
17.0000
>> x = linsolve(A,b)
x =
-1.0000
17.0000
252
252
252
21
21
21
xx
xx
xx
Distinct Real Eigenvalue Example
Characteristic matrix
Characteristic equation
Eigenvalues: 1 = -5, 2 = 2
43
21
10
01
43
21IA
0103)3)(2()4)(1( 2 IA
Distinct Real Eigenvector Example
Eigenvalues
Determine eigenvectors: Ax = x
Eigenvector for 1 = -5
Eigenvector for 1 = 2
3
1
03
0261
21
21
xxx
xx
1
2
063
022
21
21
xxx
xx
2
5
43
21
2
1
A
0)4(3
02)1(
43
2
21
21
221
121
xx
xx
xxx
xxx
Repeated Real Eigenvalue Example
Characteristic matrix
Characteristic equation
Eigenvalues: 1 = 2, 2 = 2
20
52
10
01
20
52IA
0)0)(5()2)(2( IA
Repeated Real Eigenvector Example
Eigenvalues
Determine eigenvectors: Ax = x
Eigenvectors for = 2
Eigenvectors are linearly dependent
0
1
0
1
000
05021
21
21
xxxx
xx
2
2
20
52
2
1
A
0)2(0
05)2(
20
52
21
21
221
121
xx
xx
xxx
xxx
Matlab Example
>> A=[2 5; 0 2];
>> e=eig(A)
e =
2
2
>> [X,e]=eig(A)
X =
1.0000 -1.0000
0 0.0000
e =
2 0
0 2
Complex Eigenvalue Example
Characteristic matrix
Characteristic equation
Eigenvalues: 1 = -1+i, 2 = -1-i
11
11
10
01
11
11IA
01)1()1)(1()1)(1( 2 IA
Complex Eigenvector Example
Eigenvalues
Determine eigenvectors: Ax = x
Eigenvector for = -1+i
Eigenvector for = -1-i
1
0
01
21
21
iixx
xixx
i
i
1
1
11
11
2
1
A
0)1(
0)1(
21
21
221
121
xx
xx
xxx
xxx
1
0
02
21
21
iixx
xixx
Matlab Example
>> A=[-1 -1; 1 -1];
>> e=eig(A)
e =
-1.0000 + 1.0000i
-1.0000 - 1.0000i
>> [X,e]=eig(A)
X =
0.7071 0.7071
0 - 0.7071i 0 + 0.7071i
e =
-1.0000 + 1.0000i 0
0 -1.0000 - 1.0000i
Matrix Diagonalization Example
20
05
215
45
13
21
7
1
13
21
43
21
13
21
7
1
13
21
7
1
13
211
2,2
3
1,5
43
21
1
1
121
2211
AXXD
AXXD
XxxX
xxA
Matlab Example
>> A=[-1 2 3; 4 -5 6; 7 8 -9];
>> [X,e]=eig(A)
X =
-0.5250 -0.6019 -0.1182
-0.5918 0.7045 -0.4929
-0.6116 0.3760 0.8620
e =
4.7494 0 0
0 -5.2152 0
0 0 -14.5343
>> D=inv(X)*A*X
D =
4.7494 -0.0000 -0.0000
-0.0000 -5.2152 -0.0000
0.0000 -0.0000 -14.5343
Continuous Bioreactor Example
ODE model
0
0
0
)0()(
)0()(1
)(
)0()(
PPXSqPV
F
dt
dP
SSXSY
SSV
F
dt
dS
XXXSXV
F
dt
dX
f
Fresh Media Feed (substrates)
Exit Gas Flow
Agitator
Exit Liquid Flow(cells & products)
Bioreactor: Fixed-Point Solution
Steady-state biomass equation
Iterative equation
Initialization:
)(00
)()()(02
SDXKSSK
SSXSXDXSX
V
F
im
m
ninnm
nmnnn S
KSSK
SDSSDS
SSDS
21 )(
)(
0S
Bioreactor: Newton-Raphson Solution
Iterative equation
Function
Derivative
innm
nmn KSSK
SDSfSDSf
2)()()(
dSSdf
SfSS
dxxdf
xfxx
n
nnn
n
nnn )(
)(
)(
)(11
22
2
)(
)21()()()(
innm
mnnmminnmnn
KSSK
KSSKSSK
dS
Sd
dS
Sdf
Bioreactor: Secant Solution
Iterative equation
Function
Initialization:
innm
nmn KSSK
SDSfSDSf
2)()()(
)()()(
)()()(
1
11
1
11
nn
nnnnn
nn
nnnnn SfSf
SSSfSS
xfxf
xxxfxx
10 , SS
Solution of a single nonlinear algebraic equation:
Create Matlab m-file function: fun.m:
Call fzero from the Matlab command line to find the solution:
Different initial guesses, xo, give different solutions:
Matlab Example #1
0604.0)9.0(
1
01.0)3(
1)(
22
xxxf
>> xo = 0;>> fzero('fun',xo)ans = 0.5376
>> fzero('fun',1)ans = 1.2694
>> fzero('fun',4)ans = 3.4015
Matlab Example #2
Solution of biomass equation
Parameter values: D=0.04 h-1, m=0.48 h-1, Km=1.2 g/L, Ki=22 g/L
Create Matlab M-file: substrate.m
function f = substrate(x)
D = 0.04;
mm = 0.48;
Km = 1.2;
Ki = 22;
f = -D+mm*x/(Km+x+x^2/Ki);
0)(2
im
m
KSSK
SDSf
Matlab Example #2 cont.
Guess solution and call function fzero
>> xo=0;
>> fzero('substrate',xo)
>> ans = 0.1091 (meaningful)
Solution obtained depends on initial guess
>> xo=10;
>> fzero('substrate',xo)
>> ans = -20.7263 (not meaningful)