27
Linear System function [A,b,c] = assem_boundary(A,b,p,t,e); np = size(p,2); % total number of nodes bound = unique([e(1,:) e(2,:)]); % boundary nodes inter = setdiff([1:np],bound); % interior nodes b = b(inter); % modify load vector A = A(inter,inter); % modify stiffness matrix c = zeros(np,1); % allocate solution vector c(bound) = sparse(length(bound),1); % boundary nodes solution c(inter) = A\b; % solve for free node values function [u1]=HeatSolver2D(p,e,t) k = 0.001; % time step T = 0.07; % final time n = T/k; np = size(p,2); % number of nodes x = p(1,:)'; y = p(2,:)'; % node coordinates u1 = sparse(np,n); % set zero IC ix=find(sqrt(p(1,:).^2+p(2,:).^2)<0.4); u1(ix,1)=ones(size(ix)); M = MassMatrix(p,t); %primary mass matrix A = StiffMatrix(p,t); %primary stiff matrix [A,M] = boundary(A,M,p,t,e); % M,A after imbosing boundary bound = unique([e(1,:) e(2,:)]); % boundary nodes inter = setdiff([1:np],bound); % interior nodes bold = Vec_b(0,p,e,t,bound,inter); for l = 2:n % time loop time = l*k; bnew = Vec_b(time,p,e,t,bound,inter); LHS = (M + 0.5*k*A); % Crank-Nicholson b Ax solve b A inv b A x * ) ( 1 b A \ expensive

Linear System

  • Upload
    chun

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

Linear System. function [A,b,c] = assem_boundary(A,b,p,t,e); np = size(p,2); % total number of nodes bound = unique([e(1,:) e(2,:)]); % boundary nodes inter = setdiff([1:np],bound); % interior nodes - PowerPoint PPT Presentation

Citation preview

Page 1: Linear System

Linear System

function [A,b,c] = assem_boundary(A,b,p,t,e); np = size(p,2); % total number of nodes

bound = unique([e(1,:) e(2,:)]); % boundary nodes

inter = setdiff([1:np],bound); % interior nodes

b = b(inter); % modify load vector

A = A(inter,inter); % modify stiffness matrix

c = zeros(np,1); % allocate solution vector

c(bound) = sparse(length(bound),1); % boundary nodes solution

c(inter) = A\b; % solve for free node values

function [u1]=HeatSolver2D(p,e,t)k = 0.001; % time stepT = 0.07; % final timen = T/k;np = size(p,2); % number of nodesx = p(1,:)'; y = p(2,:)'; % node coordinatesu1 = sparse(np,n); % set zero ICix=find(sqrt(p(1,:).^2+p(2,:).^2)<0.4);u1(ix,1)=ones(size(ix)); M = MassMatrix(p,t); %primary mass matrixA = StiffMatrix(p,t); %primary stiff matrix[A,M] = boundary(A,M,p,t,e); % M,A after imbosing boundarybound = unique([e(1,:) e(2,:)]); % boundary nodesinter = setdiff([1:np],bound); % interior nodesbold = Vec_b(0,p,e,t,bound,inter); for l = 2:n % time looptime = l*k;bnew = Vec_b(time,p,e,t,bound,inter);LHS = (M + 0.5*k*A); % Crank-Nicholsonrhs = (M - 0.5*k*A)*u1(inter,l-1) + 0.5*k*(bnew + bold);u1(inter,l) = LHS\rhs;end

bAx solve

bAinvbAx *)(1

bA \expensive

Page 2: Linear System

This code simulate two horizontal slices of Model 2 from the 10th SPE Comparative Solution Project. which is publicly available on the net. The model dimensions are 1200 × 2200 × 170 (ft) and the reservoir is described by a heterogeneous distribution over a regular Cartesian grid with 60×220×85 grid-blocks. We pick the top layer, in which the permeability is smooth, and the bottom layer. For both layers, the permeabilities range over at least six orders of magnitude. To drive a flow in the two layers, we impose an injection and a production well in the lower-left and upper-right corners, respectively.

10%90%

MFEM MATLAB code applied to the top layer of the SPE-10 test case

SPE-10 with an injection and a production well in the lower-left and upper-right corners, respectively.

P

I

Page 3: Linear System

tic; factorial(21); toc

Page 4: Linear System

Linear System

We want to solve the following linear system

bAx

These methods generate a sequence of approximate solutions

Gaussian EliminationLU, Choleski

2 classes of methods

Direct Methods

Iterative Methods

,,,, )3()2()1()0( xxxx

bAxx k 1*)( quickly How :method Good

)( requires 3nO

Page 5: Linear System

observations made by Horst Simon [173]. He predicted that by now we will have to solve routinely linear problems with some 5 × 10^9 unknowns. From extrapolation of the CPU times observed for a characteristic model problem, he estimated the CPU time for the most efficient direct method as 520 040 years, provided that the computation can be carried out at a speed of 1 TFLOPS.

On the other hand, the extrapolated guess for the CPU time with preconditioned conjugate gradients, still assuming a processing speed of 1 TFLOPS, is 575 seconds.

‘flops’ floating point operations. Each addition, subtraction, multiplication, division, or square root counts as one flop.

Page 6: Linear System

Linear System

We want to solve the following linear system

bAx 2 classes of

methods

Direct Methods

Iterative Methods

Krylov subspacesplitting others

Jacobi, GS, SOR,.. PCG, MINRES, GMRES, …

Page 7: Linear System

nnn RbRA ,

Linear System

Remark: (1) has a unique solution

We want to solve the following linear system

bAx

A is invertable

Remark: A is invertable det(A)=0

Remark: A is invertable eigenvaluean not is 0

Example: Solve:

15

11

25

6

4

3

2

1

x

x

x

x 10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8

Page 8: Linear System

nnn RbRA ,

Linear System

We want to solve the following linear system

bAx

Remark: solutionexact theis 1* bAx

Definition: xxxx T vector a of norm the

Example:

2

2

1

x3221 222 x

Page 9: Linear System

Iterative Method

bAx These methods generate a sequence of approximate solutions

,,,, )3()2()1()0( xxxx

bAxx k 1*)( quickly How :method Good

,,,, )3()2()1()0( xxxxapprox

residual ,,,, )3()2()1()0( rrrr

)()( kk Axbr

Remark: solutionexact theis )(kx0)( kr

error ,,,, )3()2()1()0( eeee )(*)( kk xxe

Page 10: Linear System

DEF: nnRALet with eigenvalues n ,1

Splittings and Convergence

,,, max)( 21 nA

DEF: NMA

spectral radius of A is defined to be

Splitting bxNM bAx

bNxMx A large family of iteration

bNxMx kk ˆ)(1)1(

Page 11: Linear System

Splittings and Convergence

A large family of iteration

bNxMx kk ˆ)(1)1(

U L D A

23

1312

3231

21

33

22

11

333231

232221

131211

a

aa

aa

a

a

a

a

aaa

aaa

aaa

Diagonal Lower Upper

Jacobi:

)( ULN

DM

Gauss-Seidel:

UN

LDM

Page 12: Linear System

Jacobi Method

Consider 4x4 case

15

11

25

6

4

3

2

1

x

x

x

x

0

0

0

0

given )0(x

10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8

158 3

11 10 2

253 11

6 2 10

432

4321

4321

321

xxx

xxxx

xxxx

xxx

)8/()15 3 (

10/)11 2(

11/)253 (

10/)6 2 (

)0(3

)0(2

)1(4

)0(4

)0(2

)0(1

)1(3

)0(4

)0(3

)0(1

)1(2

)0(3

)0(2

)1(1

xxx

xxxx

xxxx

xxx

10/)6 2 ( 321 xxx

)8/()15 3 (

10/)11 2(

11/)253 (

324

4213

4312

xxx

xxxx

xxxx

Example

8750.1

1000.1

2727.2

6000.0

)1(x

Page 13: Linear System

K=6 K=7 K=8 K=9 K=10x1

x2

x3

X4

K=1 K=2 K=3 K=4 K=5x1

x2

x3

X4

Jacobi Method

0.6000 1.0473 0.9326 1.0152 0.9890 2.2727 1.7159 2.0533 1.9537 2.0114 -1.1000 -0.8052 -1.0493 -0.9681 -1.0103 1.8750 0.8852 1.1309 0.9738 1.0214

1.0032 0.9981 1.0006 0.9997 1.0001 1.9922 2.0023 1.9987 2.0004 1.9998-0.9945 -1.0020 -0.9990 -1.0004 -0.9998 0.9944 1.0036 0.9989 1.0006 0.9998

11.3537 4.9910 2.0299 0.8911 0.3686)(kr

0.1605 0.0671 0.0290 0.0122 0.0053)(kr

)8/()15 3 (

10/)11 2(

11/)253 (

10/)6 2 (

)(3

)(2

)1(4

)(4

)(2

)(1

)1(3

)(4

)(3

)(1

)1(2

)(3

)(2

)1(1

kkk

kkkk

kkkk

kkk

xxx

xxxx

xxxx

xxx

1

1

2

1

*x

Page 14: Linear System

K=1 K=2 K=3 K=4 K=5x1

x2

x3

X4

Gauss Seidel Method

)(kr

0.6000 1.0302 1.0066 1.0009 1.0001 2.3273 2.0369 2.0036 2.0003 2.0000 -0.9873 -1.0145 -1.0025 -1.0003 -1.0000 0.8789 0.9843 0.9984 0.9998 1.0000

5.6930 0.4300 0.0662 0.0082 0.0009

1

1

2

1

*x

)8/()15 3 (

10/)11 2(

11/)253 (

10/)6 2 (

)1(3

)1(2

)1(4

)(4

)1(2

)1(1

)1(3

)(4

)(3

)1(1

)1(2

)(3

)(2

)1(1

kkk

kkkk

kkkk

kkk

xxx

xxxx

xxxx

xxx

Note that in the Jacobi iteration one does not use the most recently available information.

)8/()15 3 (

10/)11 2(

11/)253 (

10/)6 2 (

)(3

)(2

)1(4

)(4

)(2

)(1

)1(3

)(4

)(3

)(1

)1(2

)(3

)(2

)1(1

kkk

kkkk

kkkk

kkk

xxx

xxxx

xxxx

xxx

Page 15: Linear System

Gauss-Seidel iteration for general n:

Gauss Seidel Method

Jacobi iteration for general n:

end

axaxabxii

n

ij

kjij

i

j

kjiji

ki

1

)(1

1

)()1(

n:1ifor

end

axaxabxii

n

ij

kjij

i

j

kjiji

ki

1

)(1

1

)1()1(

n:1ifor

nnnnn

n

b

b

x

x

aa

aa

11

1

111

Page 16: Linear System

Splittings and Convergence

THM:

11 N)ρ(M - Convg

A large family of iteration

bNxMx kk )(1)1(

Page 17: Linear System

Splittings and Convergence

Example:

15

11

25

6

4

3

2

1

x

x

x

x 10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8

10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8

10 11 10 8

-1 2 -1 0 3 -1

-1 2 0 -1 3 -1

Jacobi: )( , ULNDM

0.4264)( 1 JJ NM

U=triu(A,1)L=tril(A,-1)D=diag(diag(A))eig(inv(M)*N)-0.4264 -0.1040 0.1860 0.3445GS: UNLDM ,

0.0898)( 1 gsgs NM 0 0 0.0839 - 0.0322i 0.0839 + 0.0322i

Page 18: Linear System

Splittings and Convergence

THM:

11 N)ρ(M - Convg

A large family of iteration

bNxMx kk ˆ)(1)1(

Proof: (Golub p511)

Remarks:

iAρ max)( )1 )(A )2

2AAρ T

sym)A (if )(A )32

Page 19: Linear System

Splittings and Convergence

THM:

definite positive

symmetric )0(any for

x

ConvergesGS

Proof: (Golub p512) show that all eigenvalues are less than one.

A

0

0

x

AxxTPositive Definite

AAT

Symmetric

A

M

Stiffness Matrix

Mass Matrix

Page 20: Linear System

Example:

Splittings and Convergence

DEF:

n

ijj

ijii aa1

idominant diagonallystrictly A IF

31

131

131

13

THM:

)0(any for

Jacobi and

x

Converges

GSAdominant

diagonallystrictly

Page 21: Linear System

Successive over Relaxation

The Gauss-Seidel iteration is very attractive because of its simplicity. Unfortunately, if the spectral radius is close to one, then convergence is vey slow. One solution for this

UDNLDM )1( ,

Successive over Relaxation

NM minimizes that Find 1

GS: 1

Page 22: Linear System

Successive over Relaxation

UDN

LDM

)1(

,

Successive over Relaxation Example:

15

11

25

6

4

3

2

1

x

x

x

x 10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 8

03.1

Page 23: Linear System

K=1 K=2 K=3x1

x2

x3

X4

Successive over Relaxation

UDN

LDM

)1(

,

Successive over Relaxation

Example:

15

11

25

6

4

3

2

1

x

x

x

x 10 -1 2 0 -1 11 -1 3 2 -1 10 -1 0 3 -1 803.1

0.6180 1.0553 1.0055 2.3988 2.0273 2.0004-1.0132 -1.0211 -1.0014 0.8743 0.9905 1.0000

1

1

2

1

*x

Page 24: Linear System

MATLAB CODE

Jacobi iteration for general n:

end

axaxabxii

n

ij

kjij

i

j

kjiji

ki

1

)(1

1

)()1(

n:1ifor

Ex:Write a Matlab function for Jacobi

function [sol,X]=jacobi(A,b,x0)n=length(b);maxiter=10;x=x0;for k=1:maxiterfor i=1:n sum1=0; for j=1:i-1 sum1=sum1+A(i,j)*x(j); end sum2=0; for j=i+1:n sum2=sum2+A(i,j)*x(j); end xnew(i)=(b(i)-sum1-sum2)/A(i,i)endX(1:n,k)=xnew;x=xnew;endsol=xnew;

Page 25: Linear System

MATLAB CODE

GS iteration for general n:Ex:Write a Matlab function for GS

function [sol,X]=gs(A,b,x0)n=length(b);maxiter=10;x=x0;for k=1:maxiterfor i=1:n sum1=0; for j=1:i-1 sum1=sum1+A(i,j)*x(j); end sum2=0; for j=i+1:n sum2=sum2+A(i,j)*x(j); end x(i)=(b(i)-sum1-sum2)/A(i,i)endX(1:n,k)=x;endsol=x;

end

axaxabxii

n

ij

kjij

i

j

kjiji

ki

1

)(1

1

)1()1(

n:1ifor

Page 26: Linear System

Another Look

Remark:

Given:

We want to improve this approximate:

)0(x

bAx

)( )0(*)0()1( xxxx

)0()0()1( exx

)( )0(1)0()1( xbAxx

)( )0(1)0()1( AxbAxx

)0(1)0()1( rAxx

)0(1)0()1( rGxx

)0(1)0()1( rAxx

11 AGwhere

)(1)()1( kkk rGxx

Jacobi: DG

GS: LDG

Page 27: Linear System

Examples of Splittings

1) Non-symmetric Matrix:

22

TT AAAAA

2) Domain Decomposition:

1

2

PCC

B

BA

r

r

1

11

symmetric Skew-symmetric