Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html
Linear Algebra with Matlab Introduction of Basic Matlab functions
Solve Solve Ax=b Ax=b Matlab Matlab x= A\b
trace(A) >> A=[1 3 6;2 1 9; 3 6 1] >> A=[1 3 6;2 1 9; 3 6 1] A = A = >> trace(A) >> trace(A) ans = ans = 3 3
rank() >> B=[1 2 3; 3 4 7; ;-2 5 3; ] >> B=[1 2 3; 3 4 7; ;-2 5 3; ] B = B = >> rank (B) >> rank (B) ans = ans = 3 3 Number of independent rows
Reduced row echelon form;rref(B) >> B=[1 2 3; 3 4 7; ;-2 5 3; ]; >> B=[1 2 3; 3 4 7; ;-2 5 3; ]; >> rref(B) >> rref(B) ans = ans =
inv(A) >> A=rand(3,3) >> A=rand(3,3) A = A = >> inv(A) >> inv(A) ans = ans = >> A*inv(A) >> A*inv(A) ans = ??? ans = ???
det(A) >> A=rand(3,3) >> A=rand(3,3) A = A = >> det(A) >> det(A) ans = ans =
Ax=b; x=A\b >> A=rand(3,3) >> A=rand(3,3) A = A = >> b=rand(3,1) >> b=rand(3,1) b = b = >> x = A\b >> x = A\b x = x = >> A*x >> A*x ans = ans =
tic, toc, elapsed_time >> tic >> tic >> toc >> toc elapsed_time = elapsed_time = >> tic >> tic >> x=toc >> x=toc x = x =
time.m A=rand(1000,1000); A=rand(1000,1000); tic tic inv(A); inv(A); time_to_inverse_A=toc time_to_inverse_A=toc
output functions disp( ‘ strings to be shown on screen ’ ); disp( ‘ strings to be shown on screen ’ ); fprintf( ‘ As C language %8.2f\n ’, ver1); fprintf( ‘ As C language %8.2f\n ’, ver1); >> ver1= >> ver1= >> fprintf('As C language %8.2f\n', ver1); >> fprintf('As C language %8.2f\n', ver1); As C language 1.33 As C language 1.33
norm(V,n) v=[3, 4] v=[3, 4] norm(v,1) norm(v,1) ans = ans = 7 7 norm(v) norm(v) ans = ans = 5 5 norm(v,3) norm(v,3) ans = ans = norm(v,inf) norm(v,inf) ans = ans = 4 4 ||V|| 1 =(|v1|+|V2|+…|Vn | ) ||V|| 1 =(|v1|+|V2|+…|Vn | ) ||V|| 2 =(|v1| 2 +|V2| 2 +…|V n| 2 ) -2 ||V|| 2 =(|v1| 2 +|V2| 2 +…|V n| 2 ) -2 ||V|| 3 =(|v1| 3 +|V2| 3 +…|V n| 3 ) -3 ||V|| 3 =(|v1| 3 +|V2| 3 +…|V n| 3 ) -3 ||V|| inf =(|v1| ∞ +|V2| ∞ +…|Vn| ∞ ) - ∞ ||V|| inf =(|v1| ∞ +|V2| ∞ +…|Vn| ∞ ) - ∞
If Ax=b has solution Then Then Ax =0 only when x=0 Ax =0 only when x=0 det(A) ≠0 det(A) ≠0 reff(A) = I reff(A) = I rank(A)=n rank(A)=n
LU decomposition >> A=rand(3) >> A=rand(3) A = A = >> [L1,U]=lu(A) >> [L1,U]=lu(A) L1 = L1 = U = U = >> [L,U,P]=lu(A) >> [L,U,P]=lu(A) L = L = U = U = P = P =
Cholesky decomposition >> A=[2 3 4; 3 6 7; ]; >> A=[2 3 4; 3 6 7; ]; >> P=chol(A) >> P=chol(A) P = P = >> P'*P-A >> P'*P-A ans = ans = 1.0e-015 * 1.0e-015 * A is positive definite symmetric A is positive definite symmetric A=P T P A=P T P
QR decomposition A = A = >> [Q,R]=qr(A) >> [Q,R]=qr(A) Q = Q = R = R = Q Orthogonal matrix R Upper triangle matrix
Singular Value Decomposition >> A = [ ; ; ] >> A = [ ; ; ] >> [U,S,V]=svd(A) >> [U,S,V]=svd(A) U = U = S = S = V = V = >> U*S*V' >> U*S*V' ans = ans = A=USV T A=USV T A.. m x n U.. m x m S.. m x n (singular value) V.. n x n
Reduce 3 columns V = V = >> U*S*V' >> U*S*V' ans = ans =
Pseudo-inverse A = A = >> Aplus=pinv(A) >> Aplus=pinv(A) Aplus = Aplus = >> Aplus*A >> Aplus*A ans = ans = >> A*Aplus >> A*Aplus ans = ans = Since A is not n by n, there is no inverse A -1. Since A is not n by n, there is no inverse A -1. Ax=b can be solved by pseudo-inverse A +. Ax=b can be solved by pseudo-inverse A +. x = A + * b x = A + * b
cond(A), rcond(A) >> A=eye(3).*20 >> A=eye(3).*20 A = A = >> [cond(A) rcond(A)] >> [cond(A) rcond(A)] ans = ans = cond(A) =1 cond(A) =1 A is perfectly conditioned stable cond(A) > large number cond(A) > large number A is ill-conditioned too sensitive rcond is a rapid version of cond rcond is a rapid version of cond rcond~1/cond
Condition of a Hilbert matrix >> hilb(4) >> hilb(4) ans = ans = >> cond(hilb(4)) >> cond(hilb(4)) ans = ans = e e+004
Matlab Programming
if, else, and elseif if A > B 'greater' 'greater' elseif A < B 'less' elseif A == B 'equal'else error('Unexpected situation') end
switch and case switch (rem(n,4)==0) + (rem(n,2)==0) case 0 M = odd_magic(n) case 1 M = single_even_magic(n) case 2 M = double_even_magic(n) otherwise error('This is impossible') end
for for n = 3:32 r(n) = rank(magic(n)); end r Display the result
While a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3-2*x-5; if sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end x The result is a root of the polynomial x^3 - 2x - 5 x = (bisection method)
continue fid = fopen('magic.m','r'); count = 0; while ~feof(fid) line = fgetl(fid); if isempty(line) | strncmp(line,'%',1) continue end count = count + 1; end disp(sprintf('%d lines',count));
break a = 0; fa = -Inf; b = 3; fb = Inf; while b-a > eps*b x = (a+b)/2; fx = x^3-2*x-5; if fx == 0 break elseif sign(fx) == sign(fa) a = x; fa = fx; else b = x; fb = fx; end end x
try - catch try statement... statement catch statement... statement end
return function d = det(A) %DET det(A) is the determinant of A. if isempty(A) d = 1; return else... end return terminates the current sequence of commands and returns control to the invoking function
GUIDE
GUIDE
Blank GUI
M-file editor
Tools ->Run
GUI with Axes and Menu
Run
M-file editor
Try it yourself, and think about how to use it.