0% found this document useful (0 votes)
34 views

1 Solution: Function Length

The LU decomposition rountine and a forward/backward solve rountine for a tridiagonal system is given by the following matlab cod

Uploaded by

Cesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

1 Solution: Function Length

The LU decomposition rountine and a forward/backward solve rountine for a tridiagonal system is given by the following matlab cod

Uploaded by

Cesar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 Solution

The LU decomposition rountine and a forward/backward solve rountine for a tridiagonal system is given by
the following matlab code:

Listing 1: LU decomposition routine


1 f u n c t i o n [ l , d , u ] = LUtriag ( a , b , c )
2 n=l e n g t h ( a ) ;
3 d ( 1 )=a ( 1 ) ;
4 u ( 1 )=c ( 1 ) ;
5
6 f o r i =2:n−1
7 l ( i )=b ( i −1)/d ( i −1) ;
8 d ( i )=a ( i )− l ( i ) ∗u ( i −1) ;
9 u ( i )=c ( i ) ;
10 end
11 l ( n )=b ( n−1)/d ( n−1) ;
12 d ( n )=a ( n )− l ( n ) ∗u ( n−1) ;
13 l=l ( 2 : n ) ;
14 u=u ( 1 : n−1) ;
15 end

Listing 2: Gaussian elimination solver


1 f u n c t i o n x = Gauss (A, b )
2 %Gauss e l i m i n a t i o n method
3 a =[A b ] ;
4 [m, n]= s i z e ( a ) ;
5 f o r j =1:m−1
6 f o r z =2:m
7 i f A( j , j )==0
8 t=a ( j , : ) ; a ( j , : ) =a ( z , : ) ;
9 a ( z , : ) =t ;
10 end
11 end
12 f o r i=j +1:m
13 a ( i , : ) =a ( i , : ) −a ( j , : ) ∗ ( a ( i , j ) / a ( j , j ) ) ;
14 end
15 end
16 x=z e r o s ( 1 ,m) ;
17 f o r s=m: − 1 : 1
18 c =0;
19 f o r k =2:m
20 c=c+a ( s , k ) ∗x ( k ) ;
21 end
22 x ( s ) =(a ( s , n )−c ) / a ( s , s ) ;
23 end
24 end

In order to find the solutions of the systems, we have the Thomas algorithm:

Listing 3: Thomas algorithm


1 f u n c t i o n x = thomas ( l , d , u , b )
2 n=l e n g t h ( d ) ;
3 y ( 1 )=b ( 1 ) ;
4 %Forward s u b t i t u t i o n
5 f o r k =2:n
6 y ( k )=b ( k )− l ( k−1)∗y ( k−1) ;
7 end
8 %Backward s u b t i t u t i o n
9 x ( n )=y ( n ) /d ( n ) ;
10 f o r k=n −1: −1:1
11 x ( k ) =(y ( k )−u ( k ) ∗x ( k+1) ) /d ( k ) ;
12 end
13 end

• The equilibrium equations in matrix form is given by:


 
C1

 C2 

C=
 C3 
 (1)
 .. 
 . 
CN
 
1 0
−1 1 0 
 
A=
 −1 1 0 
 (2)
 .. 
 . 
−1 1
The Stiffness matrix is given by:

K = At CA

Where
– A is a N × N matrix
– C is a N × N matrix
So K is a N × N Tridiagonal matrix
We need to find the solutions of the following system:

Kx = f

where
 
W1
.
 . 
 . 
 . 
f = .  (3)
 .. 
 . 
 . 
Wn
and

Ci = 1000 + 2N − i, i = 1, · · · , N
Wi = 1000 + 5(2N − i), i = 1, · · · , N

• In order to find the rest of the question, we development the following script:

Listing 4: Solution
1 clc
2 clear all
3 N= [ 1 0 , 1 0 0 , 1 0 0 0 ] ;
4 f p r i n t f ( 'N\ t C o n d i t i o n Matrix \ t \ t \ tError TA GE \ t \ t \ t R e s i d u a l TA\ t \ t \ t \ t \ t R e s i d u a l GE \n '
)
5 f o r k =1:3
6 W=1000+5∗(2∗N( k ) −(1:N( k ) ) ) ;
7 C=d i a g (1000+ s q r t ( ( 2 ∗N( k ) −(1:N( k ) ) ) ) , 0 ) ;
8 A=d i a g ( o n e s (N( k ) , 1 ) , 0 )+d i a g (− o n e s (N( k ) −1 ,1) , −1) ;
9
10 K=A' ∗C∗A;
11
12 a=d i a g (K) ;
13 b=d i a g ( K( 2 :N( k ) , 1 :N( k ) −1) ) ;
14 c=d i a g ( K( 1 :N( k ) −1 ,2:N( k ) ) ) ;
15 b2=W' ;
16
17 %t i c
18 [ l , d , u ] = LUtriag ( a , b , c ) ;
19 solLU = thomas ( l , d , u , b2 ) ;
20 %t o c
21
22 %t i c
23 s o l G a u s s=Gauss (K, b2 ) ;
24 %t o c
25
26 Error LU GE=norm ( solLU−s o l G a u s s ) ;
27 ResiLU=norm (K∗ solLU '−b2 ) ;
28 ResiGauss=norm (K∗ s o l G a u s s '−b2 ) ;
29
30 f p r i n t f ( '%d\ t \ t%f \ t \ t \ t %.10 f \ t \ t %.4 e \ t \ t %.4 e \n ' ,N( k ) , cond (K) , Error LU GE , ResiLU ,
ResiGauss )
31 end

• Comparing solution, we obtain the output:

Listing 5: Solution Output


1 N C o n d i t i o n Matrix Error TA GE R e s i d u a l TA R e s i d u a l GE
2 10 175.039649 0.0000000000 2 . 2 1 2 9 e −11 2 . 2 1 2 9 e −11
3 100 16365.835989 0.0000000000 9 . 1 5 5 6 e −09 9 . 1 5 5 6 e −09
4 1000 1627118.802177 0.0000000000 1 . 3 6 6 9 e −05 1 . 3 6 6 9 e −05

The Thomas algorithm is a special version of Gaussian elimination. Thats why they ended up with
the same residual for this linear system
• The condition number is
N Condition Matrix
10 175.039649
100 16365.835989
1000 1627118.802177

• The residual in the computed solution is given by:


N Thomas Algorithm GE
10 2.2129e-11 2.2129e-11
100 9.1556e-09 9.1556e-09
1000 1.3669e-05 1.3669e-05
We can see that if the condition number of the system matrix grow, then the residual also grow.
• Efficient
N Elapsed time of Thomas Algorithm Elapsed time of GE
10 0.003684 seconds. 0.003298 seconds.
100 0.004549 seconds. 0.005699 seconds.
1000 0.005779 seconds. 5.267096 seconds.

We note that when N grow the Thomas Algorithm for tridiagonal system is more efficient

You might also like