M -
{Undefined function or variable 'M'.
}
M = rand(4); N = rand(4);
M == N
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
M = round(rand(4)); N = round(rand(4));
M == N
ans =
0 1 0 0
0 1 1 1
1 0 1 0
1 1 0 1
P = (M == N)
P =
0 1 0 0
0 1 1 1
1 0 1 0
1 1 0 1
whos('P')
Name Size Bytes Class Attributes
P 4x4 16 logical
P * 3
ans =
0 3 0 0
0 3 3 3
3 0 3 0
3 3 0 3
P + 2
ans =
2 3 2 2
2 3 3 3
3 2 3 2
3 3 2 3
Q = P + 2
Q =
2 3 2 2
2 3 3 3
3 2 3 2
3 3 2 3
whos('Q')
Name Size Bytes Class Attributes
Q 4x4 128 double
M
M =
0 1 0 0
0 0 0 0
0 1 1 0
1 0 1 1
M = rand(4);
M > .6
ans =
1 0 1 0
1 0 0 0
0 1 1 1
0 0 0 0
find(M > .6)
ans =
1
2
7
9
11
15
pos = find(M > .6)
pos =
1
2
7
9
11
15
[x,y] = find(M > .6)
x =
1
2
3
1
3
3
y =
1
1
2
3
3
4
[x,y,v] = find(M > .6)
x =
1
2
3
1
3
3
y =
1
1
2
3
3
4
v =
1
1
1
1
1
1
[x,y,~] = find(M > .6)
x =
1
2
3
1
3
3
y =
1
1
2
3
3
4
n=5; ein(rand(n))
{Undefined function or variable 'ein'.
}
n=5; eig(rand(n))
ans =
2.5831 + 0.0000i
-0.4182 + 0.1381i
-0.4182 - 0.1381i
0.9140 + 0.0000i
0.5963 + 0.0000i
n=5; [V,L] = eig(rand(n))
V =
Columns 1 through 4
-0.3667 + 0.0000i -0.6259 + 0.0000i -0.5770 + 0.0000i 0.6902 + 0.0000i
-0.6117 + 0.0000i 0.1331 + 0.0000i 0.7112 + 0.0000i -0.5567 - 0.0821i
-0.4258 + 0.0000i 0.5826 + 0.0000i 0.1836 + 0.0000i -0.3250 + 0.0468i
-0.4849 + 0.0000i 0.3494 + 0.0000i -0.0364 + 0.0000i -0.0450 + 0.0111i
-0.2736 + 0.0000i -0.3591 + 0.0000i -0.3553 + 0.0000i 0.3110 + 0.0188i
Column 5
0.6902 + 0.0000i
-0.5567 + 0.0821i
-0.3250 - 0.0468i
-0.0450 - 0.0111i
0.3110 - 0.0188i
L =
Columns 1 through 4
2.0607 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i -0.1945 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.2540 + 0.0000i 0.0000 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.1031 + 0.0542i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i
Column 5
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
0.0000 + 0.0000i
0.1031 - 0.0542i
n=5; [V,~] = eig(rand(n))
V =
Columns 1 through 4
0.3584 + 0.0000i -0.6761 + 0.0000i -0.6761 + 0.0000i 0.3994 + 0.0000i
0.5282 + 0.0000i 0.3165 - 0.4495i 0.3165 + 0.4495i -0.1883 + 0.0000i
0.6718 + 0.0000i 0.3185 + 0.3369i 0.3185 - 0.3369i 0.3131 + 0.0000i
0.3660 + 0.0000i 0.1020 - 0.1108i 0.1020 + 0.1108i -0.8407 + 0.0000i
0.0855 + 0.0000i -0.0109 + 0.0540i -0.0109 - 0.0540i -0.0121 + 0.0000i
Column 5
-0.4735 + 0.0000i
-0.0600 + 0.0000i
-0.4121 + 0.0000i
0.1646 + 0.0000i
0.7585 + 0.0000i
~
~
|
{Error: Expression or statement is incomplete or incorrect.
}
for i = -5:4, exp(i), end
ans =
0.0067
ans =
0.0183
ans =
0.0498
ans =
0.1353
ans =
0.3679
ans =
1
ans =
2.7183
ans =
7.3891
ans =
20.0855
ans =
54.5982
for i = -5:3:2, exp(i), end
ans =
0.0067
ans =
0.1353
ans =
2.7183
for i = -5:3:2, i^2, end
ans =
25
ans =
4
ans =
1
for i = 5:-3:-2, i^2, end
ans =
25
ans =
4
ans =
1
for i = [-1 1 2 2 2 2 -3], i^2, end
ans =
1
ans =
1
ans =
4
ans =
4
ans =
4
ans =
4
ans =
9
for i = [-1 1 2 [2 2 2] -3], i^2, end
ans =
1
ans =
1
ans =
4
ans =
4
ans =
4
ans =
4
ans =
9
[-1 1 [2;2] 3 -3]
{Error using horzcat
Dimensions of matrices being concatenated are not consistent.
}
[[-1;1] [2;2] [3;-3]]
ans =
-1 2 3
1 2 -3
for i = [[-1;1] [2;2] [3;-3]], i^2, end
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
for i = [[-1;1] [2;2] [3;-3]], i, end
i =
-1
1
i =
2
2
i =
3
-3
3^2
ans =
9
[3,2]^2
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
[3;2]^2
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
[3;2].^2
ans =
9
4
[3 2].^2
ans =
9 4
A = [1 2; 3 4]; B = [-1 0; 1 1];
A
A =
1 2
3 4
B
B =
-1 0
1 1
A*B
ans =
1 2
1 4
A.*B
ans =
-1 0
3 4
vs A.\B
{Undefined function or variable 'vs'.
}
A\B
ans =
3.0000 1.0000
-2.0000 -0.5000
inv(A)*B
ans =
3.0000 1.0000
-2.0000 -0.5000
A/B
ans =
1 2
1 4
A*inv(B)
ans =
1 2
1 4
A.\B
ans =
-1.0000 0
0.3333 0.2500
A./B
ans =
-1 Inf
3 4
for i = [[-1;1] [2;2] [3;-3]], i.^2, end
ans =
1
1
ans =
4
4
ans =
9
9
for i = [[-1 0;0 1] [2 3;7 2] [1 3;-3 1]], i.^2, end
ans =
1
0
ans =
0
1
ans =
4
49
ans =
9
4
ans =
1
9
ans =
9
1
n = 10; x = 10*rand(n,1); y = 2*x^2 - 2*x + 10*rand(n,1);
{Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
}
n = 10; x = 10*rand(n,1); y = 2*x.^2 - 2*x + 10*rand(n,1);
plot( x, y, '*' );
n = 100; x = 10*rand(n,1); y = 2*x.^2 - 2*x + 10*rand(n,1);
close all; plot( x, y, '*' );
n = 100; x = 10*rand(n,1); y = 2*x.^2 - 3*x + 20*rand(n,1);
close all; plot( x, y, '*' );
n = 100; x = 10*rand(n,1); y = 2*x.^2 - 3*x + 30*rand(n,1);
close all; plot( x, y, '*' );
n = 100; x = 10*rand(n,1); y = 2*x.^2 - 3*x + 10*randn(n,1);
close all; plot( x, y, '*' );
n = 100; x = 10*rand(n,1); y = 2*x.^2 - 3*x + 20*randn(n,1);
close all; plot( x, y, '*' );
M = [x ones(n,1)];
k2 = M\y
k2 =
17.3802
-33.3978
hold on;
plot( x, k2(1)*x + k2(2), 'b-' )
k1 = M(:,2)\y
k1 =
58.3617
plot( x, k1*ones(n,1), 'r-' )
M = [x.^2 M];
k3 = M\y
k3 =
2.4886
-7.8541
8.7452
plot( x, k3(1)*x.^2 + k3(2)*x + k3(3), '*' )
M = [x.^3 M];
k4 = M\y
k4 =
0.0982
0.9821
-1.6969
3.7073
pwd
ans =
/Users/pauldj/works/courses/LSC/LSC-18/m
ls
Matlab-3.zip lu1.m sqrtsq.m
diary lu2.m sqsqrt.m
diary-1.txt matrix.m to_Inv_or_not_to_Inv.m
diary-2.txt matrixV.m to_Inv_or_not_to_Inv.m~
diary-3.txt matrixVclever.m visit.m
diary-fpa.txt myfft.m
lecture3.m sqrts.m
n = 4; M = rand(n); b = rand(n,1);
n = 4; A = rand(n); b = rand(n,1);
tic; x = inv(A)*b; toc
Elapsed time is 0.000107 seconds.
tic; x = A\b; toc
Elapsed time is 0.001728 seconds.
n = 100; A = rand(n); b = rand(n,1);
tic; x = A\b; toc
Elapsed time is 0.004413 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 0.002602 seconds.
clc
tic; x = inv(A)*b; toc
Elapsed time is 0.000500 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 0.000470 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 0.000547 seconds.
tic; x = A\b; toc
Elapsed time is 0.000369 seconds.
tic; x = A\b; toc
Elapsed time is 0.000314 seconds.
tic; x = A\b; toc
Elapsed time is 0.000347 seconds.
n = 1000; A = rand(n); b = rand(n,1);
clc
tic; x = inv(A)*b; toc
Elapsed time is 0.030461 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 0.026104 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 0.026349 seconds.
tic; x = A\b; toc
Elapsed time is 0.014979 seconds.
tic; x = A\b; toc
Elapsed time is 0.014675 seconds.
tic; x = A\b; toc
Elapsed time is 0.015528 seconds.
n = 5000; A = rand(n); b = rand(n,1);
clc
tic; x = inv(A)*b; toc
Elapsed time is 2.106426 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 2.120551 seconds.
tic; x = inv(A)*b; toc
Elapsed time is 2.195466 seconds.
tic; x = A\b; toc
Elapsed time is 0.883993 seconds.
tic; x = A\b; toc
Elapsed time is 0.870937 seconds.
tic; x = A\b; toc
Elapsed time is 0.913689 seconds.
ls
Matlab-3.zip lu1.m sqrtsq.m
diary lu2.m sqsqrt.m
diary-1.txt matrix.m to_Inv_or_not_to_Inv.m
diary-2.txt matrixV.m to_Inv_or_not_to_Inv.m~
diary-3.txt matrixVclever.m visit.m
diary-fpa.txt myfft.m
lecture3.m sqrts.m
edit to_Inv_or_not_to_Inv.m
to_Inv_or_not_to_Inv
Small condition number - residual
ans =
1.4214e-14
ans =
1.4227e-14
Large condition number - residual
ans =
2.2888e-16
ans =
2.4414e-04
who
Your variables are:
A B N V ans b2 i k3 pos v2 xS
A1 L P V1 b c1 k1 k4 v x y
A2 M Q V2 b1 c2 k2 n v1 xI
whos('A')
Name Size Bytes Class Attributes
A 5000x5000 200000000 double
single(pi)
ans =
3.1416
format long
single(pi)
ans =
3.1415927
ls
Matlab-3.zip lu1.m sqrtsq.m
diary lu2.m sqsqrt.m
diary-1.txt matrix.m to_Inv_or_not_to_Inv.m
diary-2.txt matrixV.m to_Inv_or_not_to_Inv.m~
diary-3.txt matrixVclever.m visit.m
diary-fpa.txt myfft.m
lecture3.m sqrts.m
edit lu1.m
edit lu2.m
2^126
ans =
8.507059173023462e+37
2^127
ans =
1.701411834604692e+38
2^128
ans =
3.402823669209385e+38
2^129
ans =
6.805647338418769e+38
single(2^126)
ans =
8.5070592e+37
single(2^127)
ans =
1.7014118e+38
single(2^128)
ans =
Inf
single(eps)
ans =
2.2204460e-16
eps(single)
{Error using single
Not enough input arguments.
}
eps(single(1))
ans =
1.1920929e-07
single(10^-20)
ans =
9.9999997e-21
2^1000
ans =
1.071508607186267e+301
2^-1000
ans =
9.332636185032189e-302
eps = 10^8; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
0
eps = 10^7; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
0
eps = 10^-5; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
2.5262125e-13
eps = 10^-3; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
4.7497450e-11
eps = 10^-6; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
2.5247572e-15
eps = 10^-7; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
1.1686097e-15
eps = 10^-8; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
1
eps = 10^-7; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)/norm(A)
ans =
7.2224054e-16
eps = 10^-8; A = [eps -1; 1 1]; [L,U] = lu2(eps); norm(L*U-A)
ans =
1
eps = 10^-8; A = [eps -1; 1 1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
3.1622779
eps = 10^-8; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
6.0774707e-17
eps = 10^-10; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
1.3351432e-18
eps = 10^-3; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
4.7497450e-11
eps = 10^3; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
0
eps = 10^5; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
0
eps = 10^7; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
0
eps = 10^8; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
1
eps = 10^8; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
0
eps = 10^16; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
1
eps = 10^15; A = [1 1; eps -1]; [L,U] = lu1(eps); norm(L*U-A)
ans =
0