getting started with matlab numerical methods appendix b ...

42
Getting started with M Getting started with M atlab atlab Numerical Methods Numerical Methods Appendix B Appendix B http://www.mathworks.com/access/he http://www.mathworks.com/access/he lpdesk/help/techdoc/learn_matlab/l lpdesk/help/techdoc/learn_matlab/l earn_matlab.html earn_matlab.html

Post on 21-Dec-2015

230 views

Category:

Documents


3 download

TRANSCRIPT

Getting started with Matlab Getting started with Matlab

Numerical MethodsNumerical MethodsAppendix BAppendix B

http://www.mathworks.com/access/helpdehttp://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/learn_matlask/help/techdoc/learn_matlab/learn_matla

b.htmlb.html

Linear Algebra with MatlabLinear Algebra with Matlab

Introduction of Introduction of Basic Matlab functionsBasic Matlab functions

SolveSolve Ax=bAx=b

MatlabMatlab x= A\bx= A\b

trace(A)trace(A) >> A=[1 3 6;2 1 9; 3 6 1] >> A=[1 3 6;2 1 9; 3 6 1]

A =A =

1 3 61 3 6 2 1 92 1 9 3 6 13 6 1

>> trace(A)>> trace(A)

ans =ans =

33

rank()rank()

>> B=[1 2 3; 3 4 7; 4 -3 1;-2 5 3; 1 -7 6]>> B=[1 2 3; 3 4 7; 4 -3 1;-2 5 3; 1 -7 6] B =B = 1 2 31 2 3 3 4 73 4 7 4 -3 14 -3 1 -2 5 3-2 5 3 1 -7 61 -7 6

>> rank (B)>> rank (B) ans =ans = 33

Number of independen

t rows

Reduced row echelon Reduced row echelon form;rref(B)form;rref(B)

>> B=[1 2 3; 3 4 7; 4 -3 1;-2 5 3; 1 -7 6];>> B=[1 2 3; 3 4 7; 4 -3 1;-2 5 3; 1 -7 6];

>> rref(B)>> rref(B)

ans =ans =

1 0 01 0 0 0 1 00 1 0 0 0 10 0 1 0 0 00 0 0 0 0 00 0 0

inv(A)inv(A) >> A=rand(3,3)>> A=rand(3,3) A =A = 0.1389 0.6038 0.01530.1389 0.6038 0.0153 0.2028 0.2722 0.74680.2028 0.2722 0.7468 0.1987 0.1988 0.44510.1987 0.1988 0.4451

>> inv(A)>> inv(A) ans =ans = -0.8783 -8.5418 14.3617-0.8783 -8.5418 14.3617 1.8694 1.8898 -3.23481.8694 1.8898 -3.2348 -0.4429 2.9695 -2.7204-0.4429 2.9695 -2.7204

>> A*inv(A)>> A*inv(A) ans = ???ans = ???

det(A)det(A) >> A=rand(3,3)>> A=rand(3,3) A =A = 0.9318 0.8462 0.67210.9318 0.8462 0.6721 0.4660 0.5252 0.83810.4660 0.5252 0.8381 0.4186 0.2026 0.01960.4186 0.2026 0.0196

>> det(A)>> det(A) ans =ans = 0.05620.0562

Ax=b; x=A\bAx=b; x=A\b

>> A=rand(3,3)>> A=rand(3,3) A =A = 0.9318 0.8462 0.67210.9318 0.8462 0.6721 0.4660 0.5252 0.83810.4660 0.5252 0.8381 0.4186 0.2026 0.01960.4186 0.2026 0.0196

>> b=rand(3,1)>> b=rand(3,1) b =b = 0.15090.1509 0.69790.6979 0.37840.3784

>> x = A\b>> x = A\b x =x = 3.45393.4539 -5.4966-5.4966 2.35642.3564

>> A*x>> A*x ans =ans = 0.15090.1509 0.69790.6979 0.37840.3784

tic, toc, elapsed_timetic, toc, elapsed_time >> tic>> tic >> toc>> toc elapsed_time =elapsed_time = 2.16302.1630

>> tic>> tic >> x=toc>> x=toc x =x = 2.52402.5240

time.mtime.m

A=rand(1000,1000);A=rand(1000,1000); tictic inv(A);inv(A); time_to_inverse_A=toctime_to_inverse_A=toc

output functionsoutput 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=1.3333>> ver1=1.3333

>> fprintf('As C language %8.2f\n', ver1);>> fprintf('As C language %8.2f\n', ver1); As C language 1.33As C language 1.33

norm(V,n)norm(V,n) v=[3, 4]v=[3, 4]

norm(v,1)norm(v,1) ans =ans = 77

norm(v)norm(v) ans =ans = 55

norm(v,3)norm(v,3) ans =ans = 4.49794.4979

norm(v,inf)norm(v,inf) ans =ans = 44

||||V||V||11=(|v1|+|V2|+…|Vn|=(|v1|+|V2|+…|Vn| ) ) ||||V||V||22=(|v1|=(|v1|22+|V2|+|V2|22+…|Vn|+…|Vn|2 2 ) ) -2-2

||||V||V||33=(|v1|=(|v1|33+|V2|+|V2|33+…|Vn|+…|Vn|3 3 ) ) -3-3

||||VV||||infinf=(|v1|=(|v1|∞∞+|V2| +|V2| ∞∞ +…|Vn| +…|Vn| ∞∞) ) - ∞- ∞

If Ax=b has solutionIf Ax=b has solution

ThenThen Ax =0 only when x=0Ax =0 only when x=0 det(A) ≠0det(A) ≠0 reff(A) = Ireff(A) = I rank(A)=nrank(A)=n

LU decompositionLU decomposition

>> A=rand(3)>> A=rand(3) A =A = 0.9991 0.8848 0.46420.9991 0.8848 0.4642 0.3593 0.4178 0.24770.3593 0.4178 0.2477 0.3566 0.0836 0.12630.3566 0.0836 0.1263

>> [L1,U]=lu(A)>> [L1,U]=lu(A) L1 =L1 = 1.0000 0 1.0000 0 00 0.3596 -0.4291 1.00000.3596 -0.4291 1.0000 0.3569 1.0000 00.3569 1.0000 0

U =U = 0.9991 0.8848 0.46420.9991 0.8848 0.4642 0 0 -0.2322-0.2322 -0.0394-0.0394 0 0 0 0 0.06380.0638

>> [L,U,P]=lu(A)>> [L,U,P]=lu(A)

L =L = 1.0000 0 1.0000 0 0 0 0.3569 1.0000 00.3569 1.0000 0 0.3596 -0.4291 1.00000.3596 -0.4291 1.0000

U =U = 0.9991 0.8848 0.46420.9991 0.8848 0.4642 0 0 -0.2322 -0.0394-0.2322 -0.0394 0 0 0 0 0.06380.0638

P =P = 1 0 01 0 0 0 0 10 0 1 0 1 00 1 0

11.1.2 Cholesky decompositio11.1.2 Cholesky decompositionn

>> A=[2 3 4; 3 6 7; 4 7 10];>> A=[2 3 4; 3 6 7; 4 7 10]; >> >> P=chol(A)P=chol(A) P =P = 1.4142 2.1213 2.82841.4142 2.1213 2.8284 0 1.2247 0.81650 1.2247 0.8165 0 0 1.15470 0 1.1547

>> P'*P-A>> P'*P-A ans =ans = 1.0e-015 *1.0e-015 * 0.4441 0 00.4441 0 0 0 0 00 0 0 0 0 00 0 0

A is positive definite A is positive definite symmetricsymmetric

A=PA=PTTPP

QR decompositionQR decomposition

A =A = 0.8138 0.7576 0.22400.8138 0.7576 0.2240 0.1635 0.0536 0.84690.1635 0.0536 0.8469 0.0567 0.5092 0.04660.0567 0.5092 0.0466

>> >> [Q,R]=qr(A)[Q,R]=qr(A) Q =Q = -0.9781 -0.0246 -0.2065-0.9781 -0.0246 -0.2065 -0.1965 -0.2161 0.9564-0.1965 -0.2161 0.9564 -0.0682 0.9761 0.2065-0.0682 0.9761 0.2065

R =R = -0.8319 -0.7862 -0.3887-0.8319 -0.7862 -0.3887 0 0.4667 -0.14300 0.4667 -0.1430 0 0 0.77340 0 0.7734

Q Q Orthogonal matrixOrthogonal matrix

RR Upper triangle matrixUpper triangle matrix

Singular Value Singular Value DecompositionDecomposition

>> A = [1 2 3 4 5; 5 9 2 3 4; 2 2 3 4 2]>> A = [1 2 3 4 5; 5 9 2 3 4; 2 2 3 4 2]

>> >> [U,S,V]=svd(A)[U,S,V]=svd(A) U =U = -0.4581 0.6831 -0.5688-0.4581 0.6831 -0.5688 -0.7993 -0.5966 -0.0727-0.7993 -0.5966 -0.0727 -0.3890 0.4213 0.8193-0.3890 0.4213 0.8193

S =S = 14.0087 0 14.0087 0 0 0 0 0 0 0 0 0 5.1976 0 5.1976 0 0 0 0 0 0 0 0 0 1.9346 0 1.9346 0 0 0

V =V = -0.3735 -0.2803 0.3650 -0.4827 --0.3735 -0.2803 0.3650 -0.4827 -

0.64470.6447 -0.6344 -0.6080 -0.0793 0.2604 0.3920-0.6344 -0.6080 -0.0793 0.2604 0.3920 -0.2955 0.4079 0.3133 -0.5529 0.5852-0.2955 0.4079 0.3133 -0.5529 0.5852 -0.4130 0.5056 0.4052 0.6063 -0.2051-0.4130 0.5056 0.4052 0.6063 -0.2051 -0.4473 0.3601 -0.7734 -0.1609 --0.4473 0.3601 -0.7734 -0.1609 -

0.21490.2149

>> U*S*V'>> U*S*V' ans =ans = 1.0000 2.0000 3.0000 4.0000 5.00001.0000 2.0000 3.0000 4.0000 5.0000 5.0000 9.0000 2.0000 3.0000 4.00005.0000 9.0000 2.0000 3.0000 4.0000 2.0000 2.0000 3.0000 4.0000 2.00002.0000 2.0000 3.0000 4.0000 2.0000

A=USVA=USVTT

A .. m x nA .. m x n U .. m x m U .. m x m S .. m x n S .. m x n

(singular value)(singular value) V .. n x nV .. n x n

Reduce 3 columnsReduce 3 columns

V =V =

-0.3735 -0.2803 0 0 0-0.3735 -0.2803 0 0 0 -0.6344 -0.6080 0 0 0-0.6344 -0.6080 0 0 0 -0.2955 0.4079 0 0 0-0.2955 0.4079 0 0 0 -0.4130 0.5056 0 0 0-0.4130 0.5056 0 0 0 -0.4473 0.3601 0 0 0-0.4473 0.3601 0 0 0

>> U*S*V'>> U*S*V'

ans =ans =

1.4016 1.9127 3.3447 4.4458 4.14901.4016 1.9127 3.3447 4.4458 4.1490 5.0514 8.9888 2.0441 3.0570 3.89125.0514 8.9888 2.0441 3.0570 3.8912 1.4215 2.1258 2.5035 3.3579 3.22581.4215 2.1258 2.5035 3.3579 3.2258

Pseudo-inversePseudo-inverse

A =A = 0 00 0 0 00 0 0 00 0 1 01 0 0 10 1

>> >> Aplus=pinv(A)Aplus=pinv(A) Aplus =Aplus = 0 0 0 1 00 0 0 1 0 0 0 0 0 10 0 0 0 1

>> Aplus*A>> Aplus*A ans =ans = 1 01 0 0 10 1

>> A*Aplus>> A*Aplus

ans =ans =

0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 1 00 0 0 1 0 0 0 0 0 10 0 0 0 1

Since A is not n by n, there is Since A is not n by n, there is no inverse Ano inverse A-1-1..

Ax=b can be solved by pseudAx=b can be solved by pseudo-inverse Ao-inverse A++..

x = Ax = A++ * b * b

cond(A), rcond(A)cond(A), rcond(A) >> A=eye(3).*20>> A=eye(3).*20 A =A = 20 0 020 0 0 0 20 00 20 0 0 0 200 0 20

>> [cond(A) rcond(A)]>> [cond(A) rcond(A)] ans =ans = 1 11 1

cond(A) =1cond(A) =1 A is perfectly conditionedA is perfectly conditioned stablestable

cond(A) > large numbercond(A) > large number A is ill-conditionedA is ill-conditioned too sensitivetoo sensitive

rcond is a rapid version rcond is a rapid version of condof cond rcond~1/condrcond~1/cond

Condition of a Hilbert matrix Condition of a Hilbert matrix

>> hilb(4)>> hilb(4) ans =ans = 1.0000 0.5000 0.3333 0.25001.0000 0.5000 0.3333 0.2500 0.5000 0.3333 0.2500 0.20000.5000 0.3333 0.2500 0.2000 0.3333 0.2500 0.2000 0.16670.3333 0.2500 0.2000 0.1667 0.2500 0.2000 0.1667 0.14290.2500 0.2000 0.1667 0.1429

>> cond(hilb(4))>> cond(hilb(4)) ans =ans = 1.5514e+0041.5514e+004

Matlab ProgrammingMatlab Programming

if, else, and elseifif, else, and elseif

if A > Bif A > B 'greater' 'greater'

elseif A < B elseif A < B 'less' 'less'

elseif A == B elseif A == B 'equal' 'equal'

else else error('Unexpected situation') error('Unexpected situation')

endend

switch and caseswitch 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

forfor

for n = 3:32

r(n) = rank(magic(n));

end

r r

Display the result

WhileWhilea = 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

end x

The result is a root of the polynomial x^3 - 2x - 5

x = 2.09455148154233

(bisection method)(bisection method)

continuecontinue 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));

breakbreaka = 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 - catchtry - catch

try statement ... statement

catch statement ... statement

end

returnreturn

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 functi

on

GUIDEGUIDE

GUIDEGUIDE

Blank GUIBlank GUI

M-file editorM-file editor

Tools ->RunTools ->Run

GUI with Axes and MenuGUI with Axes and Menu

RunRun

M-file editorM-file editor

Try it yourself, and think Try it yourself, and think about how to use it.about how to use it.