A=[1 2 3
0 5 6
7 8 9]
A =
1 2 3
0 5 6
7 8 9
A=[1,2,3;0,5,6;7,8,9]
A =
1 2 3
0 5 6
7 8 9
b=[3
2
9]
b =
3
2
9
b=[3 2 9]'
b =
3
2
9
cond(A)
ans =
37.9389
disp('result should have about 14 correct digits')
result should have about 14 correct digits
x=A\b
x =
1.0000
-2.0000
2.0000
format long
x=A\b
x =
1.000000000000000
-2.000000000000000
2.000000000000000
% suppose the b values were measured and have a 1% relative error
relDataError=0.01
relDataError =
0.010000000000000
maxSolError=relDataError*cond(A)
maxSolError =
0.379388784960651
ABad=A
ABad =
1 2 3
0 5 6
7 8 9
A(2,1)
ans =
0
A(2,1)=4
A =
1 2 3
4 5 6
7 8 9
A(2,1)=0
A =
1 2 3
0 5 6
7 8 9
ABad(2,1)=4
ABad =
1 2 3
4 5 6
7 8 9
cond(ABad)
ans =
5.052279444538510e+16
eps
ans =
2.220446049250313e-16
relErrSolDueToMatlab=cond(A)*eps
relErrSolDueToMatlab =
8.424123286957535e-15
relErrSolDueToMatlab=cond(ABad)*eps
relErrSolDueToMatlab =
11.218313932334100
xBad=ABad\b
[Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.
RCOND = 2.202823e-18.]
[]
xBad =
1.0e+16 *
2.522015791327477
-5.044031582654954
2.522015791327477
ABad*xBad
ans =
-4
24
20
% Never check the accuracy of a solution by s=ubsituting it into the equations.
% MATRIX MANIPULATIONS
% Transposes
A'
ans =
1 0 7
2 5 8
3 6 9
AT=A'
AT =
1 0 7
2 5 8
3 6 9
b
b =
3
2
9
b'
ans =
3 2 9
b''
ans =
3
2
9
A*x
ans =
3.000000000000000
2.000000000000000
9.000000000000000
A.*b
ans =
3 6 9
0 10 12
63 72 81
A.*x
ans =
1.000000000000000 2.000000000000000 2.999999999999999
0 -9.999999999999998 -11.999999999999996
13.999999999999998 15.999999999999998 17.999999999999996
errors=A*x-b
errors =
1.0e-15 *
0
0.444089209850063
0
38*eps
ans =
8.437694987151190e-15
ABad*xBad
ans =
-4
24
20
ABad*xBad - b
ans =
-7
22
11
max(abs(ans))
ans =
22
A*x
ans =
3.000000000000000
2.000000000000000
9.000000000000000
x*A
{Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in
the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.}
x'*A
ans =
14.999999999999998 8.000000000000000 9.000000000000000
A*[x b x x b]
ans =
1.0e+02 *
Columns 1 through 4
0.030000000000000 0.340000000000000 0.030000000000000 0.030000000000000
0.020000000000000 0.640000000000000 0.020000000000000 0.020000000000000
0.090000000000000 1.180000000000000 0.090000000000000 0.090000000000000
Column 5
0.340000000000000
0.640000000000000
1.180000000000000
format short
A*[x b x x b]
ans =
3.0000 34.0000 3.0000 3.0000 34.0000
2.0000 64.0000 2.0000 2.0000 64.0000
9.0000 118.0000 9.0000 9.0000 118.0000
A
A =
1 2 3
0 5 6
7 8 9
[x b x x b]
ans =
1.0000 3.0000 1.0000 1.0000 3.0000
-2.0000 2.0000 -2.0000 -2.0000 2.0000
2.0000 9.0000 2.0000 2.0000 9.0000
x
x =
1.0000
-2.0000
2.0000
b
b =
3
2
9
x*b
{Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in
the first matrix matches the number of rows in the second matrix. To perform
elementwise multiplication, use '.*'.}
x.*b
ans =
3.0000
-4.0000
18.0000
[x b]
ans =
1.0000 3.0000
-2.0000 2.0000
2.0000 9.0000
x./b
ans =
0.3333
-1.0000
0.2222
x/b
ans =
0 0 0.1111
0 0 -0.2222
0 0 0.2222
A*x
ans =
3.0000
2.0000
9.0000
% A*x = b
% x=b/A
% x=A\b
[x b]
ans =
1.0000 3.0000
-2.0000 2.0000
2.0000 9.0000
x'*b
ans =
17.0000
dot(x,b)
ans =
17.0000
dot(x',b)
ans =
17.0000