disp('Save your workspace before leaving!')
Save your workspace before leaving!
A=[1 2 3;
0 5 6;
7 8 9]
A =
1 2 3
0 5 6
7 8 9
disp('matrix A')
matrix A
b=[3;
2;
9]
b =
3
2
9
disp('right-hand-side vector b')
right-hand-side vector b
det(A)
ans =
-24
disp('Check condition number')
Check condition number
cond(A)
ans =
37.9389
relErrDueToMatlab=eps(1)
relErrDueToMatlab =
2.2204e-16
relErrDueToMatlab=eps(1)*cond(A)
relErrDueToMatlab =
8.4241e-15
condA=cond(A)
condA =
37.9389
x=inv(A)*b
x =
1.0000
-2.0000
2.0000
disp('inv(..) is bad bad bad')
inv(..) is bad bad bad
disp('correct way, left division, i.e. not b/A but A\b')
correct way, left division, i.e. not b/A but A\b
x=A\b
x =
1
-2
2
disp('If the data have a relative error of 1%, the values')
If the data have a relative error of 1%, the values
disp('of x will have that error multiplied by cond(A)')
of x will have that error multiplied by cond(A)
relErrData=0.01
relErrData =
0.0100
relErrx=cond(A)*relErrData
relErrx =
0.3794
disp('eg')
eg
Diag=diag(-2,3,4)
{Error using diag
Too many input arguments.}
Diag=diag([-2,3,4])
Diag =
-2 0 0
0 3 0
0 0 4
det(Diag)
ans =
-24
relErrx=cond(Diag)*relErrData
relErrx =
0.0200
ASing=A
ASing =
1 2 3
0 5 6
7 8 9
ASing(2,1)
ans =
0
ASing(2,1)=4
ASing =
1 2 3
4 5 6
7 8 9
condASing=cond(ASing)
condASing =
5.0523e+16
eps(1)
ans =
2.2204e-16
relErrDuetoMatlab=condASing*eps(1)
relErrDuetoMatlab =
11.2183
xSing=ASing\b
[Warning: Matrix is close to singular or badly scaled. Results may be
inaccurate. RCOND = 2.202823e-18.]
xSing =
1.0e+16 *
2.5220
-5.0440
2.5220
disp('Matrix Manipulations')
Matrix Manipulations
disp('Transpose')
Transpose
b
b =
3
2
9
b'
ans =
3 2 9
bT=b'
bT =
3 2 9
A=A
A =
1 2 3
0 5 6
7 8 9
AT=A'
AT =
1 0 7
2 5 8
3 6 9
ATT=A''
ATT =
1 2 3
0 5 6
7 8 9
A
A =
1 2 3
0 5 6
7 8 9
disp('Matrix Multiplication')
Matrix Multiplication
A
A =
1 2 3
0 5 6
7 8 9
x
x =
1
-2
2
A.*x
ans =
1 2 3
0 -10 -12
14 16 18
A*x
ans =
3
2
9
b
b =
3
2
9
ASing
ASing =
1 2 3
4 5 6
7 8 9
xSing
xSing =
1.0e+16 *
2.5220
-5.0440
2.5220
ASing*xSing
ans =
-4
24
20
b
b =
3
2
9
y=[2;3]
y =
2
3
A.*y
{Matrix dimensions must agree.}
A*y
{Error using *
Inner matrix dimensions must agree.}
A
A =
1 2 3
0 5 6
7 8 9
x
x =
1
-2
2
b
b =
3
2
9
B=[x b b x]
B =
1 3 3 1
-2 2 2 -2
2 9 9 2
A*B
ans =
3 34 34 3
2 64 64 2
9 118 118 9
A*b
ans =
34
64
118
B*A
{Error using *
Inner matrix dimensions must agree.}
[B A]
ans =
1 3 3 1 1 2 3
-2 2 2 -2 0 5 6
2 9 9 2 7 8 9
B=[x b b]
B =
1 3 3
-2 2 2
2 9 9
A*B
ans =
3 34 34
2 64 64
9 118 118
B*A
ans =
22 41 48
12 22 24
65 121 141
b*x
{Error using *
Inner matrix dimensions must agree.}
b
b =
3
2
9
x
x =
1
-2
2
bT
bT =
3 2 9
bT*x
ans =
17
b'*x
ans =
17
dot(b,x)
ans =
17
b'*b
ans =
94
norm(b)
ans =
9.6954
norm(b)^2
ans =
94.0000
b'*x
ans =
17
b*x'
ans =
3 -6 6
2 -4 4
9 -18 18
b.*b
ans =
9
4
81
sum(b.*b)
ans =
94
sum(b.*x)
ans =
17
A*A
ans =
22 36 42
42 73 84
70 126 150
AT*AT
ans =
22 42 70
36 73 126
42 84 150
A^2
ans =
22 36 42
42 73 84
70 126 150
A.^2
ans =
1 4 9
0 25 36
49 64 81
save lecture7