حل تمرین های سری اول درس روش های عددی در جبر خطی

Upload: milad-naderi

Post on 11-Oct-2015

305 views

Category:

Documents


38 download

DESCRIPTION

حل تمرین های سری اول درس روش های عددی در جبرخطیمدرس آقای پروفسور دهقاندانشگاه صنعتی امیرکبیرترم دوم 1392-1393

TRANSCRIPT

  • In The Name Of God

    Course:

    Numerical methods in linear algebra

    Home Work:

    Series 1

    Student:

    Milad Naderi

    Student No:

    92130901

    .

    "Every human activity, good or bad, except mathematics, must come to an end."

    Paul Erdos

  • 2

    ( ) 1

    :

    k d XB Xk k...,2,1,0 1

    . n n B n1 d

    ) .

    )spets m retfa rotcaf ecnegrevnoC() 1

    )spets m retfa rotcaf ecnegrevnoc egareva ehT() 2

    )spets m retfa etar ecnegrevnoc egareva ehT() 3

    ) 4

    0

    aB

    a

    )R a . (

  • 3

    )( 1

    )

    B k d XB Xk k...,2,1,0 1

    :

    . . m Bm

    1

    . m Bm m

    1

    B B Rmm gol ) (m

    .

    . Bm

    :) etar ecnegrevnoc citotpmysa (

    B B R B RK k) ( gol ) ( mil ) (

    )

    4 4 01

    0 0

    m m

    m

    m

    am aB B

    a a

    :

    1

    1n

    ji

    j

    n i a xaM A

    :

    am a Bm m m41: m

    : m 11

    am a Bmm m m m) 4 (1

    am a B Rmm m) 4 (gol ) (11: m

  • 4

    ( ) 2

    b XA

    011 51 8 1 42 26

    011 61 41 7 05 32

    011 22 02 85 6 4,

    011 3 66 91 21 01

    011 45 2 52 81 11

    b A

    ( ROS .

    ) .

  • 5

    ( ) 2

    ) tnanimod yllanogaid yltcirtS ( A

    . b XA

    . 06 61 41 7 32 05 DDS A

    .

    . A

    . A D.P ROS

    M elpaM .

    ROS

    M .

    . .

    .

    :

    U D L

    >

  • 6

    U L D MJ) (1

    >

    :

    MJ1 ) ( 875. ,624. ,1-e544.0 ,121.- ,829.-

    .

    .

    U L D MSG) ( ) (1

    >

    :

    MSG1 ) ( 703. ,I*1-e325.0+241. ,I*1-e325.0-241. ,1-e742.0

  • 7

    .

    .

    )

    59

    : )

    program Jacobi !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Solving linear system (AX=b) by Jacobi iterative method !! !! 2) Investigation on effect of number of iteratives !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 04/17/14 MiladNaderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Sepecificaton of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Allocatable::A(:,:),b(:),Xk(:),Xk1(:),dif(:) !Real,Allocatable:: Real::tol,maxdif,dummy,len integer::i,j,k,n character::con,y ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write& Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Read (*,*) tol !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input) Open(Unit=4,File='X0.dat',status='OLD', action='READ') !availabe initial value vector (input) Open(Unit=5,File='Results.dat') !Results file contains iterative number,maximumdiffrence and xi at each level. ! Read A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) allocate (A(n,n)) allocate (Xk(n));allocate (Xk1(n));allocate (dif(n)) allocate (b(n))

  • 8

    doi=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2) ! Read B vector doi=1,n read(3,*) b(i) end do !close(3) ! Read X0 vector k=0 doi=1,n read(4,*) Xk(i) end do !close(4) Write(5,100) (i,i=1,n) 100 FORMAT (((10x,'K|',1000000(3x,'X('I10')|')))) write(5,101) k,(XK(i),i=1,n) 101 FORMAT (7x,I4,1000000('|',F16.4)) 300 dummy=0;len=0 If (k>0 .AND. MOD(k,50)==0) then Write (5,200) k 200 FORMAT(/,'After'I10'steps, solution was not convergence',/) Write (*,400) k 400 FORMAT(/,'After'I10'steps, solution was not convergence',/) END IF Doi=1,n Do j=1,n if ( i/=j) then len=A(i,j)*Xk(j) dummy=len+dummy end if end do Xk1(i)=(1./A(i,i))*(b(i)-dummy) dummy=0 END Do k=k+1 write(5,102) k,(XK1(i),i=1,n) 102 FORMAT (7x,I4,1000000('|',F16.4)) doi=1,n dif(i)=Xk1(i)-XK(i) if( abs(dif(i))>tol) then XK=XK1 go to 300 end if end do Write (5,*) 'solution is convergence' write(*,*) 'END OF JACOBI PROGRAM' END Program

  • 9

    XT0,0,0,0,0 500.0=loT

    )5 (X )4(X )3 (X )2 (X )1 (X K

    0 0 0 0 0 0

    730.2 7666.1 6698.1 2.2 2477.1 1

    5200.0 3953.0 2991.0 2000.0- 1481.0 2

    498.1 3185.1 957.1 689.1 1427.1 3

    9051.0 9154.0 5803.0 8111.0 8413.0 4

    1677.1 305.1 2056.1 2738.1 1136.1 5

    7272.0 7925.0 204.0 5922.0 8214.0 6

    7076.1 3434.1 3855.1 3817.1 6445.1 7

    1573.0 7595.0 484.0 1533.0 7494.0 8

    4875.1 4473.1 1084.1 9716.1 964.1 9

    4264.0 1256.0 2555.0 1724.0 8465.0 01

    4894.1 6223.1 2314.1 9135.1 9304.1 11

    2735.0 5007.0 8616.0 5605.0 2526.0 21

    3924.1 8772.1 7553.1 854.1 8743.1 31

    5106.0 1247.0 76.0 575.0 3776.0 41

    7963.1 3932.1 3603.1 4493.1 5992.1 51

    9656.0 9777.0 8517.0 436.0 1227.0 61

    4813.1 1602.1 8362.1 6933.1 9752.1 71

    5407.0 8808.0 2557.0 8486.0 7067.0 81

    2472.1 5771.1 1722.1 5292.1 1222.1 91

    5547.0 3538.0 2987.0 6827.0 9397.0 02

    1632.1 8251.1 6591.1 9152.1 3191.1 12

    9087.0 2858.0 5818.0 3667.0 5228.0 22

    3302.1 6131.1 4861.1 9612.1 7461.1 32

    3118.0 9778.0 7348.0 7897.0 1748.0 42

    1571.1 3311.1 1541.1 8681.1 8141.1 52

    5738.0 8498.0 4568.0 7628.0 4868.0 62

    8051.1 6790.1 9421.1 8061.1 2221.1 72

    1068.0 4909.0 1488.0 7058.0 6688.0 82

    9921.1 480.1 6701.1 5831.1 2501.1 92

    5978.0 229.0 2009.0 5178.0 4209.0 03

    8111.1 4270.1 6290.1 3911.1 6090.1 13

    2698.0 8239.0 419.0 3988.0 9519.0 23

    3690.1 3260.1 8970.1 7201.1 870.1 33

    6019.0 2249.0 629.0 7409.0 6729.0 43

    9280.1 7350.1 7860.1 5880.1 2760.1 53

    329.0 2059.0 3639.0 9719.0 7739.0 63

    4170.1 2640.1 2950.1 2670.1 8750.1 73

    7339.0 1759.0 1549.0 3929.0 3649.0 83

    5160.1 8930.1 9050.1 6560.1 8940.1 93

    9249.0 1369.0 7259.0 1939.0 8359.0 04

  • 10

    350.1 3430.1 9340.1 5650.1 9240.1 14

    9059.0 2869.0 3959.0 6749.0 2069.0 24

    6540.1 5920.1 8730.1 6840.1 9630.1 34

    7759.0 6279.0 9469.0 9459.0 7569.0 44

    3930.1 4520.1 5230.1 9140.1 8130.1 54

    6369.0 4679.0 8969.0 1169.0 5079.0 64

    8330.1 9120.1 820.1 1630.1 4720.1 74

    6869.0 7979.0 479.0 5669.0 6479.0 84

    1920.1 9810.1 1420.1 1130.1 6320.1 94

    379.0 5289.0 6779.0 2179.0 1879.0 05

    ecnegrevnoc ton saw noitulos ,spets05 retfA

    1520.1 2610.1 8020.1 8620.1 3020.1 15

    7679.0 9489.0 7089.0 2579.0 1189.0 25

    6120.1 410.1 9710.1 320.1 5710.1 35

    89.0 789.0 4389.0 6879.0 8389.0 45

    6810.1 210.1 4510.1 8910.1 1510.1 55

    7289.0 8889.0 7589.0 6189.0 689.0 65

    610.1 4010.1 3310.1 1710.1 310.1 75

    1589.0 4099.0 7789.0 1489.0 889.0 85

    8310.1 9800.1 4110.1 7410.1 2110.1 95

    2789.0 7199.0 4989.0 3689.0 6989.0 06

    9110.1 7700.1 8900.1 7210.1 6900.1 16

    989.0 9299.0 9099.0 2889.0 1199.0 26

    2010.1 6600.1 5800.1 9010.1 3800.1 36

    5099.0 9399.0 1299.0 9989.0 3299.0 46

    8800.1 7500.1 3700.1 4900.1 1700.1 56

    8199.0 7499.0 2399.0 3199.0 4399.0 66

    6700.1 9400.1 3600.1 1800.1 1600.1 76

    399.0 4599.0 2499.0 5299.0 3499.0 86

    5600.1 2400.1 4500.1 700.1 3500.1 96

    9399.0 1699.0 599.0 5399.0 1599.0 07

    6500.1 6300.1 7400.1 600.1 6400.1 17

    8499.0 6699.0 7599.0 4499.0 8599.0 27

    8400.1 1300.1 400.1 2500.1 9300.1 37

    5599.0 1799.0 3699.0 2599.0 4699.0 47

    2400.1 7200.1 5300.1 4400.1 4300.1 57

    1699.0 5799.0 8699.0 9599.0 9699.0 67

    6300.1 3200.1 300.1 8300.1 9200.1 77

    7699.0 8799.0 2799.0 4699.0 3799.0 87

    1300.1 200.1 6200.1 3300.1 5200.1 97

    1799.0 1899.0 6799.0 9699.0 7799.0 08

    7200.1 7100.1 2200.1 8200.1 2200.1 18

  • 00

    82 0.998 0.9974 0.998 0.9984 0.9975

    83 1.0019 1.0024 1.0019 1.0015 1.0023

    84 0.9983 0.9977 0.9982 0.9986 0.9979

    solution is convergence

    Tol=0.005 0 2,0,1,0,1TX 22

    .Tol=0.0001 111

    :

    1,1,1,1,1TX

    )

    program GS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Solving linear system (AX=b) by Gauss sidel iterative method !! !! 2) Investigation on effect of number of iteratives !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 04/17/14 MiladNaderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Sepecificaton of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Allocatable::A(:,:),b(:),Xk(:),Xk1(:),dif(:) !Real,Allocatable:: Real::tol,maxdif,dummy,len integer::i,j,k,n character::con,y ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write& Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Read (*,*) tol !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input)

  • 02

    Open(Unit=4,File='X0.dat',status='OLD', action='READ') !availabe initial value vector (input) Open(Unit=5,File='Results.xls') !Results file contains iterative number,maximumdiffrence and xi at each level. ! Read A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) allocate (A(n,n)) allocate (Xk(n));allocate (Xk1(n));allocate (dif(n)) allocate (b(n)) doi=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2) ! Read B vector doi=1,n read(3,*) b(i) end do !close(3) ! Read X0 vector k=0 doi=1,n read(4,*) Xk(i) end do !close(4) Write(5,100) (i,i=1,n) 100 FORMAT (((10x,'K|',1000000(3x,'X('I10')|')))) write(5,101) k,(XK(i),i=1,n) 101 FORMAT (7x,I4,1000000('|',F16.4)) 300 dummy=0;len=0 If (k>0 .AND. MOD(k,50)==0) then Write (5,200) k 200 FORMAT(/,'After'I10'steps, solution was not convergence',/) Write (*,400) k 400 FORMAT(/,'After'I10'steps, solution was not convergence',/) END IF Doi=1,n Do j=1,i-1 len=len+A(i,j)*Xk1(j) End do Do j=i+1,n len=len+A(i,j)*Xk(j) end Do dummy=len+dummy Xk1(i)=(1./A(i,i))*(b(i)-dummy) dummy=0 len=0 END Do k=k+1 write(5,102) k,(XK1(i),i=1,n)

  • 30

    ))4.61F,'|'(0000001,4I,x7( TAMROF 201 n,1=iod )i(KX-)i(1kX=)i(fid neht )lot>))i(fid(sba (fi 1KX=KX 003 ot og fi dne od dne 'ecnegrevnoc si noitulos' )*,5( etirW 'MARGORP lediS ssuaG FO DNE' )*,*(etirw margorP DNE

    - XT0,0,0,0,0 500.0=loT

    7 .

    . 14

    )5(X )4 (X )3(X )2(X )1(X K

    0 0 0 0 0 0

    2434.0 7676.0 136.1 9383.1 2477.1 1

    5997.0 8209.0 7603.1 1471.1 8910.1 2

    739.0 9079.0 8401.1 6350.1 7889.0 3

    9089.0 2199.0 6230.1 2510.1 5699.0 4

    2499.0 3799.0 9900.1 3400.1 3999.0 5

    2899.0 2999.0 300.1 3100.1 9999.0 6

    5999.0 8999.0 9000.1 4000.1 1 7

    ecnegrevnoc si noitulos

    1,0,1,0,2 0 500.0=loT 2 XT

    : 11 1000.0=loT.

    XT1,1,1,1,1

  • 04

    )SOR

    program SOR

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Solving linear system (AX=b) by Successive over relaxation method !! !! 2) Investigation on effect of number of iterative !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 04/17/14 MiladNaderi Original code !! !! !!

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Sepecificaton of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Allocatable::A(:,:),b(:),Xk(:),Xk1(:),dif(:) !Real,Allocatable:: Real::tol,maxdif,dummy,len,w integer::i,j,k,n character::con,y ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write& Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Write(*,*) 'Please enter tolerance,tol= ' Read (*,*) tol Write(*,*) 'Please enter relaxation factor,w= ' Read (*,*) w !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input) Open(Unit=4,File='X0.dat',status='OLD', action='READ') !availabe initial value vector (input) Open(Unit=5,File='Results.dat') !Results file contains iterative number,maximumdiffrence and xi at each level. ! Read A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) allocate (A(n,n)) allocate (Xk(n));allocate (Xk1(n));allocate (dif(n)) allocate (b(n)) doi=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2)

  • 05

    ! Read B vector doi=1,n read(3,*) b(i) end do !close(3) ! Read X0 vector k=0 doi=1,n read(4,*) Xk(i) end do !close(4) Write(5,100) (i,i=1,n) 100 FORMAT (((10x,'K|',1000000(3x,'X('I10')|')))) write(5,101) k,(XK(i),i=1,n) 101 FORMAT (7x,I4,1000000('|',F16.4)) 300 dummy=0;len=0 If (k>0 .AND. MOD(k,50)==0) then Write (5,200) k 200 FORMAT(/,'After'I10'steps, solution was not convergence',/) Write (*,400) k 400 FORMAT(/,'After'I10'steps, solution was not convergence',/) END IF Doi=1,n Do j=1,i-1 len=len+A(i,j)*Xk1(j) End do Do j=i,n len=len+A(i,j)*Xk(j) end Do dummy=len+dummy Xk1(i)=( XK(i)) +(( w / A(i,i)) * (b(i) - dummy)) dummy=0 len=0 END Do k=k+1 write(5,102) k,(XK1(i),i=1,n) 102 FORMAT (7x,I4,1000000('|',F16.4)) doi=1,n dif(i)=Xk1(i)-XK(i) if( abs(dif(i))>tol) then XK=XK1 go to 300 end if end do Write (5,*) 'solution is convergence' write(*,*) 'END OF SOR PROGRAM' END Program

    Tol=0.005 0,0,0,0,0TX SOR 1.2

    7 .

  • 60

    )5(X )4(X )3(X )2(X )1(X K

    0 0 0 0 0 0

    6442.0 8036.0 8719.1 8464.1 921.2 1

    3529.0 889.0 9492.1 862.1 718.0 2

    1940.1 7920.1 7989.0 2869.0 39.0 3

    7410.1 7500.1 8969.0 7379.0 1010.1 4

    8799.0 3899.0 5699.0 6999.0 6500.1 5

    5899.0 3999.0 1200.1 1200.1 1 6

    1 1 6000.1 3000.1 6999.0 7

    ecnegrevnoc si noitulos

    100.0=loT

    . XT0,0,0,0,0

    2 o570.1

    .

    0

    5

    01

    51

    02

    52

    03

    53

    04

    54

    2 5.1 1 5.0 0

    tIe

    arit

    ev

    )agemO(rotcaF noitaxaleR

    dohtem ROS ta rebmun evitareti no rotcaf noitaxaler tceffE

    K

  • 70

    ( ) 3

    :

    1 1

    2 2

    xb B A

    yb A B

    .

    1 1

    2 2 1 1

    1 1 1

    2 2 1 1

    , .1

    , .2

    k k k k

    k k k k

    b y A xB b yB x A

    b y A xB b yB x A

    .)

    ( ) )

  • 80

    ) ( 3

    )

    . yk)1 ( xk)1 (

    .

    .

    c XM Xk k1

    : 1 M

    1 1

    1 1 1 1

    1 1

    1 1

    1 1 1

    1 1 1

    ) (

    k k k k

    k k

    k k

    b yB x A b yB x A

    b yB A x

    b A yB A x

    A1

    B A1 ) (11 c XM Xk k1

    B A B A) ( ) (1 11 1( :. )

    1 1

    2 2 2 2

    1 1

    2 2

    1 1 1

    2 2 2

    ) (

    k k k k

    k k

    k k

    b xB y A b y A xB

    b xB A y

    b A xB A y

    :

    A2 2

    B A1 ) (1 B A B A) ( ) (1 12 2(

    )

    )

    yk)1 ( xk)1 ( ()

    .

    .

    .

    c XM Xk k1

    : 1 M

  • 90

    1

    1 1

    1 1

    2 2

    k k

    k k

    b yB x A

    b y A xB

    :

    1 1

    ) (1 1 yB b A xk k

    :

    1 1 1 1 1

    2 2 1 1 1 2 2 1 1

    1 1

    1 1

    1 1 1 1

    1 1 1 2 2

    ) (

    ) (

    ) (

    k k k k

    k k

    k k

    b y A yB AB b AB b y A yB b AB

    yB b A x

    yB AB b AB b A y

    1 1

    1 1

    1 1 1 1 1 1

    1 2 1 1 2 2 2

    ) (

    )

    k k

    k k

    yB b A x

    yB AB A b AB A b A y

    11 2 1

    12 4 3

    1 1 1

    ;0 ; ;01 2 4 3 1 2 1

    k k

    k k

    x xc M M

    y yc M M

    B AB A M M B A M M

    2

    1 1

    1 2

    1 1 1 1 1 1

    1 2 1 2 1 2

    0 0 ) (ted0

    1 ) ( ,0 0 ) (fi egrevnoC

    MM I

    B AB A

    B AB A B AB A B AB A

    A2 A1 1 1

    B AB A1 ) (1 2 .

    )

    -

    ... . ROS

  • 12

    ( ) 4

    n j i a Ajik k,...,2,1 , , ) ( ) ( . A

    . : A A

    . Ak) ( k k n k1 1)

    k k)1 ( ) () a xaM a xaMji ji

  • 02

    ( ) 4

    )

    .

    : X . A

    V X XA XT0

    ,..., ,2 1 T

    x x x Xn

    :

    1 1

    0n n

    T

    j i ji

    j i

    x x a XA X

    : A

    1 21 111 21 11

    )1( )1(

    2 12 )1(

    )1( )1(

    2

    00

    00

    nn

    n

    nn n

    a a aa a a

    a aA

    B

    a a

    : citardauQ

    12 )1(1 11 11

    11 112 1 1 2 2 2 2

    ) ( ) (n n n n n n n

    i Tj ii j i ji j i ji j i ji

    i j i j i j i

    a a ax x a x x a x x a x x a xB x

    a a

    x xn ,..., 2 A)1(

    :

    )1(

    1 1

    0n n

    j i ji

    j i

    x x a

    : x1 x xn ,..., 2

  • 22

    11

    11 2

    ni

    i

    i

    ax x

    a

    1 1

    0n n

    j i ji

    j i

    x x a

    A .

    n k1 1 Ak) ( . A)1(

    .

    )

    . aii0 A A)1(

    . : aii0 )1(

    21 )1(

    11

    iii ii

    aa a

    a

    :

    a aii ii 0)1(

    : A)1( A

    a aji jixam xam xam)1(

    :

    a aji jik kxam xam xam) ( )1 (

  • 32

    ( ) 5

    :

    1 1 1

    2 2 2 1

    2 1

    2 2 2

    1 1 2

    1 2

    0 0

    0

    0

    0

    n n

    n n n

    n n n

    e d c

    e d c b

    b a

    e d a

    d c b

    c b a

    . e d c b ai i i i i, , , ,

    . n 6-n5

    D N M A. lanogaidatnep A)1 ,1 ,01,1 ,1 (

    lanogaidatnep M)0,0,1,1 ,1 (01 01 lanogaiD D)8,...,8(01 01 . M NT

    :

    ) ( )1 (

    ) ( )1 (

    ) ( )1 (

    ...,2,1,0 , ) (.1

    ...,2,1,0 , ) ( .2

    ...,2,1,0 , ) (.3

    k k

    k k

    k k

    k b xN x D M

    k b x N M xD

    k b xD x N M

  • 42

    ( ) 5

    : N D M

    =N , =M , =D

    )1

    , =D+M

    N D M) (1

    >

    . elpaM

  • 52

    )2

    N M D HH) (1

    :

    >

    .

    )3

    D N M E) (1

    :

    >

    . 1

  • 62

    ( ) 6

    0 , . Rn x M) ( x N) ()

    :

    x x x M N M) ( ) ( ) (

    :xRn)

    2 1 2 x x x n

    An n ) - . A A A K) (1

    . :

    2

    1A Kn A K A K) ( ) ( ) (

    n

    A K) ( A A A K) (1

    A K) ( 2

    1

    2 2 2 A A A K) (

  • 72

    ( ) 6

    )

    :

    x M0 ) (

    .

    0 ,. x N0 ) (

    .

    x M0 ) (

    ) ( x N) ( x M) ( ) (

    N

    M

    x

    x-

    ,. 0 ,

    :

    ) (0

    ) (

    N

    M

    x

    x

    : x M) (

    x x x M N M) ( ) ( ) (

    .

    )

    2 1

    12 11

    1 12 2 22 2 2

    22 11

    ) ,..., , (

    ...

    ) ... ( ) (

    n

    n

    n i

    i

    n

    n i

    i

    x x x

    x x x x

    x x x x

    x

    x

    x

  • 82

    :

    22 2 2

    1 3 2 1 3 1 2 1 2 1 2 1

    12 2 2

    21 3 2 1 3 1 2 1 2 1 2 1

    12 2 2

    22 1 2 1 2 1

    ) ... ... (2 .... ) ... (

    )) ... ... (2 .... ( ...

    ) .... ( ...

    n n n n n

    n n n n n

    n n

    x x x x x x x x x x x x x x x x

    x x x x x x x x x x x x x x x x

    x x x x x x

    x x

    .

    12 2

    ) (x

    x :

    2

    12 22 1 2 1

    2 2 2 12 2 2

    2 1 222 1

    2 2 2

    121 3 2 1 3 1 2 1 2 1

    2 2 2

    2 1 2

    ) ... ( ...) ( ) (

    ....) .... (

    ) ... ... (2 ....) (

    ....

    n n

    nn

    n n n n

    n

    x x x x x x

    x x xx x x

    x x x x x x x x x x x x x

    x x x

    x

    x

    x

    x

    x x x n x x x x x x x x x xn n n n.... )1 ( ) ... ... (22 1 1 3 2 1 3 1 2 12 2 2

    :

    2 1 2 12 2 2 2 2 2 12

    2 2 2

    2 1 2

    .... )1 ( ....) (

    ....

    n n

    n

    x x x n x x xn

    x x x

    x

    x

    :

    1

    2 1

    2

    n n ) (x

    x xx

    :

    2 1 2 x x x n

  • 92

    )

    :] 2 [ 1-7-1

    n m A 2

    1A m A A

    n 33.

    .

    : n n A

    2

    1A n A A

    n

    : A1

    1 1 1

    2

    1A n A A

    n

    :

    1 2 1 1 2

    2 2

    1A A n A A A A) ( ) (

    n

    1 1 1

    2 2

    1A A n A A A A ) (n

    :

    2

    1A Kn A K A K) ( ) ( ) ( ) (n

    .

  • 13

    ( ) 7

    q p) , ( A . q p) , (

    . q) ,0( U p)0, ( L UL

  • 03

    ( ) 7

    aji0 p j i ) dnaB rewoL ( -p An m

    . aji0 q i j) dnaB reppU ( -q

    q p0

    q p1.

    . q p2

    .

    . ) grebnesseH rewoL(

    . q m p1 ,1

    )grebnesseH reppU (

    . p m q1 ,1

    )

    q p A

    .

    : q p) , ( A

  • 23

    1 ,1 21 11

    22 12

    ,... 1,1

    ,1

    ..,1

    0 0

    0

    0

    0 0

    q

    n p

    n n

    nn p n

    a a a

    a a

    Aa a

    a

    a a

    : A . n

    1 1

    0 10 1

    00

    TT

    T

    n n

    AB BI I

    T

    B

    . q p n n1 1

    q T p A

    UL U1 L1 . T

    B

    :

    1 1

    0 1

    ,0

    T

    U LU L

    . UL=A

  • 33

    ( ) 8

    N P1 P N P A) ( ) 1( R An n

    . n1 ...1 2

    b x x k P N Pk k...,2,1,0 ) ( ) 1() ( )1 (

    . x)0(

    . ) mumitpo (

  • 43

    ( ) 8

    : M

    b x x k P N Pk k...,2,1,0 ) ( ) 1() ( )1 (

    .

    ( M

    . n1 ...1 2 ) .

    : M

    n,,2,1=i

    : . M

    |

    |

    : .

    :

    .

    :

  • 53

    (

    : ).

    : M) ( . )

    11

    1

    ) ( ) (

    1 1

    2

    nn

    ntpo

  • 63

    ( ) 1

    .

    ( ) 1

    .

    .

    )K( )V(

    : .

    . ) 1

    v v 0 ) ( ) 2

    v v v .1&0 .0) 3

    .v v 0 ) ( ) v ( V vV )1

    ) :9

    W W V

    . )K(

    ) V ( p) V ( W

    : napS

  • 73

    . :

    k k k) ( 1 ) ( )1 (

    r P x xk

    . xA b rk k) ( ) (

    k I=P

    :

    - K A A pk) ( r A p rk k) ()0( ) ( :

    : m .

    k )0( ) (

    ) ; (1k

    . r A K rk

    : k

    : xk) (

  • 83

    1

    ) (

    0

    kj

    j

    j

    r

    ) I=P ( . 1-k A

    . WK X

    :

    . WK X qk1

    . WK

    SERMG idlonrA

    ) sisab ( .

    .

    . v A Km) ; (() m

    2 1

    . v A Km) ; (1 v v v/

    :

    w w v k k k/2 1 w k0

    v v vm ,..., ,2 1 m . k

    . v A Km) ; ( m n

    R Vm

    . vi

  • 93

    R mm m )1 ( H

    .

    ) sisab (

    : .

    wK xk) ( r)0(

    .

    .

    ( wK xk) ( k) (

    ) w xk

    . .

    :

    w xk k) () 1 : r A Kk) ; ()0( rk) (

    ) dohtem noitazilanogohtro lluf(MOF

    .

    w xk k) ( )2) (

    2

    :rk

    ) laudiser muminim dezilareneg( SERMG

    .

    k

    ( . Vk r A Kk) ; ()0( )0( )0(

    2 1). r r v/

    :

  • 14

    .zk) (

    :

    : r A Kk) ; ()0( rk) (

    :

    xk) ( zk) ( zk) (

    .

    . Hk

    )n m ( m n

    .

    .

  • 04

    .

    :SERMG

    : k xk) (

    ( r v r2 1)0( )0(

    ) Hm

    :

    : zk) (

  • 24

    ( ) 2

    ( ) 2

    -

    . .

    .

    .

    .

    . ) GC ( A

    renoitidnocerP . noitidnocerP

    .

    A dnoC A dnoC) ( ) ( ) S ( gninoitidnocerP

    x b bS x x ST) (1 b x A S . SAS AT

    M M S ST) (1 . x

    . renoitidnocerP

  • 34

    ( ) 3

    ( ) 3

    revo ibocaJ(ROJ b=xA

    xk)1 (. ) noitaxaler

    :

    :

    : BJ

    F ) ( ralugnairT reppU E I(

    - A D ) ( ralugnairT rewoL

    .

    . 1

    A

    ROJ1

    20

    A D) (

    .

    :

    : ROJ

  • 44

    1

    JB I D A

    1 1 1

    1 1

    1 1

    1 1

    ( ) ( ) ( ) 1 ( ) 1

    . ( ) ( )

    1 1 ( ) 1 2 ( ) 0

    2 20 0

    ( ) ( )

    J i i

    i

    i i

    i

    B I D A I D A D A

    A is P D D A D A

    D A D A

    D A D A

  • 54

    ( ) 4

    . UL

    ( ) 4

    : n n A

    11 D: L( R D L11 1 11 R Ar r

    22) ( ) ( ) . R . R Ar n r n

    : A11 UL A

    :

    . 2

    . UL

    m m n n A

    . 1-m A UL

  • 64

    ( ) 5

    .

    .

    ( ) 5

    .

    . .

    .

    .

    . .

    .

    x b x A

    :

    x b b E A) (

    . E b,

    Ak) (

    .

    A A An ,...., ,)1 ( )1( ) ( ) (

    :( ) A

    ) ,...., , (xam1 1

    ) (xam

    n

  • 74

    j ,

    ji xami

    ) ( aj ,

    ji kk xami

    . a

    :

    ) 1

    : gnitovip latoT

    12 1 11

    n nn .... 4. 3. 2.1 23 1

    . n

    nosnikliW .

    . dluoG. n n

    .

    ) 2

    : gnitovip laitraP

    n21

    .

    n21

    :

  • 84

    Ak) (

    .

    .

    ) 3

    ( ) (

    . )

    .

    ( ) 6

    .

    ( ) 6

    :

    21 1121 11

    22 12 22 12

    q q

    q q

    A AA A

    A A A A A

  • 94

    ( ) 1

    .

    () UL.

    )ROS eniL(ROSL )ROS tnioP(ROSP )ROS cirtemmyS(ROSS .

    .

  • 15

    ( ) 1

    :

    .

    .

    () UL)

    . ( )UL

    dnuofxam.

    .

    ( L

    . . P )

    P P.... P P P1 2 2-n 1-n

    UL L U

    :

    1

    P M P M P Ln n) ... (1 1 1 1

    UAP L1

    : U L b x A

    1

    1

    b U L b UL b bP AP b A) (

    b L Y b YL xU

    Y U X U

    x x x x

    Y

    Y x

  • 50

    :

    Doolittle LU decomposition

    program LU !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Dollitle LU Factorization with partial pivoting !! !! 2) Solving linear system (AX=b) !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 05/02/14 Milad Naderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Specification of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real*8,Allocatable::A(:,:),b(:),x(:),L(:,:),U(:,:),Uinv(:,:),d(:,:),P(:,:),v(:,:) Integer,Allocatable:: ri(:),II(:,:) Real*8::dummy,vas integer::i,j,k,n,rk ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((L)) is Lower triangular matrix with unit diagonal elements ! ((U)) is Upper triangular matrix ! ((n)) is size of matrix !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write & Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input) Open(Unit=4,File='U.dat') !Upper triangular matrix Open(Unit=5,File='L.dat') !Lower triangular matrix with unit diagonally Open(Unit=6,File='X.dat') !Solution of linear system Open(Unit=7,File='P.dat') !Permutation matrix ! Read A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) allocate (A(n,n)) allocate (U(n,n));allocate (L(n,n));allocate (X(n)); allocate (b(n));allocate (d(1,n));allocate (ri(n-1));allocate(II(n,n));allocate(P(n,n));allocate(V(n,n));allocate(Uinv(n,n)) do i=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2) ! Read b vector

  • 52

    do i=1,n read(3,*) b(i) end do !close(3) U=A ! U matrix calculation do k=1,n-1 call maxfound(U,rk,k,n) ri(k)=rk do j=k,n d(1,j)=U(k,j);U(k,j)=U(rk,j);U(rk,j)=d(1,j) end do do i=k+1,n U(i,k)=-1.*(U(i,k)/U(k,k)) end do do i=k+1,n do j=k+1,n U(i,j)=U(i,j)+U(i,k)*U(k,j) end do end do end do do i=1,n do j=1,n if ( i>j ) then U(i,j)=0. end if end do end do ! Write Upper triangular matrix in U.dat! do 2 i = 1,n do 1 j = 1,n 1 write(4,'(F10.3,$)') U(i,j) 2 write(4,*) ! Identity matrix Formation for Permutation matrix creation! !write(5,*) (ri(i),i=1,n-1) do i=1,n do j=1,n if (i==j) then II(i,j)=1 else II(i,j)=0 end if end do end do V=II;P=II do k=1,n-1 if (ri(k) /= k) then d(1,:)=II(k,:) P(k,:)=II(ri(k),:) P(ri(k),:)=d(1,:) end if V=MATMUL(P,V) P=II end do do 2000 i = 1,n do 1000 j = 1,n 1000 write(7,'(F10.5,$)') V(i,j) 2000 write(7,*)

  • 53

    ! L matrix Formation! Call inverse(U,Uinv,n) L=MATMUL(V,MATMUL(A,Uinv)) do 200 i = 1,n do 100 j = 1,n 100 write(5,'(F10.5,$)') L(i,j) 200 write(5,*) !Solving Linear system equation by inverse matrix of L and U' !Comment: Since we MUST use Inverse subroutine for L matrix calculation ( at above procedure); we use here matrix inversion for ! Finding of system solution instead using of Forward and Backward Subsituation ! b=MATMUL(V,b) Call inverse(L,P,n) X=MATMUL(P,b) X=MATMUL(Uinv,X) write(6,*) 'X(i) |------Result------|' do i=1,n Write(6,30) i,X(i) 30 Format (/,x,'X(',I3,')',2x,'|',F10.5) end do END Program Subroutine maxfound(U,rk,k,n) !============================================================ ! Max Found at one array ! Method: Based on Comparing entires in array ! Milad Naderi 2014 !----------------------------------------------------------- ! input ... ! U(n,n) - array of coefficients for matrix U ! n - size of matrix ! k - dummy ! output ... ! rk - number of row including maximum absolute value !=========================================================== Implicit None Integer::k,n,rk,i Real*8::max Real*8,Dimension(n,n)::U rk=k max=abs(U(k,k)) do i=k+1,n IF( max < abs(U(i,k)) ) then max=abs(U(i,k)) rk=i end if end do RETURN ; END SUBROUTINE subroutine inverse(a,c,n) !============================================================ ! Inverse matrix ! Method: Based on Doolittle LU factorization for Ax=b ! Alex G. December 2009 !----------------------------------------------------------- ! input ... ! a(n,n) - array of coefficients for matrix A ! n - dimension ! output ...

  • 54

    ! c(n,n) - inverse matrix of A ! comments ... ! the original matrix a(n,n) will be destroyed ! during the calculation !=========================================================== implicit none integer:: n Real*8,Dimension(n,n)::a,c,U,L Real*8,Dimension(n)::b,d,x !double precision L(n,n), U(n,n), b(n), d(n), x(n) Real*8::coeff integer::i,j,k ! step 0: initialization for matrices L and U and b ! Fortran 90/95 aloows such operations on matrices L=0.0 U=0.0 b=0.0 ! step 1: forward elimination do k=1, n-1 do i=k+1,n coeff=a(i,k)/a(k,k) L(i,k) = coeff do j=k+1,n a(i,j) = a(i,j)-coeff*a(k,j) end do end do end do ! Step 2: prepare L and U matrices ! L matrix is a matrix of the elimination coefficient ! + the diagonal elements are 1.0 do i=1,n L(i,i) = 1.0 end do ! U matrix is the upper triangular part of A do j=1,n do i=1,j U(i,j) = a(i,j) end do end do ! Step 3: compute columns of the inverse matrix C do k=1,n b(k)=1.0 d(1) = b(1) ! Step 3a: Solve Ld=b using the forward substitution do i=2,n d(i)=b(i) do j=1,i-1 d(i) = d(i) - L(i,j)*d(j) end do end do ! Step 3b: Solve Ux=d using the back substitution x(n)=d(n)/U(n,n) do i = n-1,1,-1 x(i) = d(i) do j=n,i+1,-1 x(i)=x(i)-U(i,j)*x(j) end do x(i) = x(i)/u(i,i) end do ! Step 3c: fill the solutions x(n) into column k of C do i=1,n c(i,k) = x(i) end do b(k)=0.0 end do Return; end subroutine

  • 55

    :

    1)

    5 1 1

    1 5 1

    1 1 5

    A

    ,

    7

    7

    7

    b

    :

    1.00000 0.00000 0.00000

    0.20000 1.00000 0.00000

    0.20000 0.16667 1.00000

    L

    5.00000 1.00000 1.00000

    0.00000 4.80000 0.80000

    0.00000 0.00000 4.66700

    U

    1.00000 0.00000 0.00000

    0.00000 1.00000 0.00000

    0.00000 0.00000 1.00000

    P

    ,

    1

    1

    1

    X

    2)

    1 2 1 4

    2 0 4 3

    4 2 2 1

    -3 1 3 2

    A

    ,

    13

    28

    20

    6

    b

    :

    1.000 0.000 0.000 0.000

    -0.750 1.000 0.000 0.000

    0.500 -0.400 1.000 0.000

    0.250 0.600 -0.4583 1.000

    L

    4.000 2.000 2.000 1.000

    0.000 2.500 4.500 2.750

    0.000 0.000 4.800 3.600

    0.000 0.000 0.000 3.750

    U

    0.000 0.000 1.000 0.000

    0.000 0.000 0.000 1.000

    0.000 1.000 0.000 0.000

    1.000 0.000 0.000 0.000

    P

    ,

    3.000

    1.000

    4.000

    2.000

    X

  • 56

    -1 ) SSOR

    program SSOR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Solving linear system (AX=b) by Symmetric Successive over relaxation method(SSOR) !! !! 2) Investigation on effect of number of iterates !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 04/05/14 Milad Naderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Specification of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Allocatable::A(:,:),b(:),Xk(:),Xk5(:),dif(:),Xk1(:) !Real,Allocatable:: Real::tol,maxdif,dummy,len,w integer::i,j,k,n character::con,y ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk5)) is unknowns vector for k+1/2 step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write & Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Write(*,*) 'Please enter tolerance,tol= ' Read (*,*) tol Write(*,*) 'Please enter relaxation factor,w= ' Read (*,*) w !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input) Open(Unit=4,File='X0.dat',status='OLD', action='READ') !availabe initial value vector (input) Open(Unit=5,File='Results.dat') !Results file contains iterative number,maximum diffrence and xi at each level. ! Read A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) allocate (A(n,n)) allocate (Xk(n));allocate (Xk1(n));allocate (dif(n));allocate (Xk5(n)); allocate (b(n)) do i=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2)

  • 57

    ! Read B vector do i=1,n read(3,*) b(i) end do !close(3) ! Read X0 vector k=0 do i=1,n read(4,*) Xk(i) end do !close(4) Write(5,100) (i,i=1,n) 100 FORMAT (((10x,'K|',1000000(3x,'X('I10')|')))) write(5,101) k,(XK(i),i=1,n) 101 FORMAT (7x,I4,1000000('|',F16.4)) 300 dummy=0;len=0 If (k>0 .AND. MOD(k,50)==0) then Write (5,200) k 200 FORMAT(/,'After'I10'steps, solution was not convergence',/) Write (*,400) k 400 FORMAT(/,'After'I10'steps, solution was not convergence',/) END IF ! Forward SOR ! Do i=1,n Do j=1,i-1 len=len+A(i,j)*Xk5(j) End do Do j=i+1,n dummy=dummy+A(i,j)*Xk(j) end Do dummy=len+dummy Xk5(i)=((1-w)* XK(i)) + (( w / A(i,i)) * (b(i) - dummy)) dummy=0 len=0 END Do ! Backward SOR ! Do i=n,1,-1 Do j=1,i-1 len=len+A(i,j)*Xk5(j) End do Do j=i+1,n dummy=dummy+A(i,j)*Xk1(j) end Do dummy=len+dummy Xk1(i)=((1-w)* XK5(i)) + (( w / A(i,i)) * (b(i) - dummy)) dummy=0 len=0 END Do k=k+1 write(5,102) k,(XK1(i),i=1,n) 102 FORMAT (7x,I4,1000000('|',F16.4)) do i=1,n dif(i)=Xk1(i)-XK(i) if( abs(dif(i))>tol) then

  • 58

    XK=XK1 go to 300 end if end do Write (5,*) 'solution is convergence' write(*,*) 'END OF SOR PROGRAM' END Program

    :

    1)

    62 24 1 8 15 110

    23 50 7 14 16 110

    ,4 6 58 20 22 110

    10 12 19 66 3 110

    11 18 25 2 54 110

    A b

    ( )

    5

    kx ( )4kx ( )3kx ( )2kx ( )1 kx Iterations(k) Tolerance 0.9997 0.9998 1.0002 1.0000 1.0001 9 1

    0.001

    0.9993 0.9996 1.0003 1.0000 1.0002 9 1.1 0.9996 0.9997 1.0001 1.0000 1.0001 11 1.2 0.9992 0.9995 1.0002 1.0000 1.0002 12 1.3 0.9988 0.9993 1.0003 0.9999 1.0002 14 1.4 0.9985 0.9991 1.0003 0.9998 1.0003 17 1.5 0.9976 0.9985 1.0004 0.9997 1.0005 21 1.6 0.9966 0.9979 1.0006 0.9996 1.0007 28 1.7 0.9936 0.9962 1.0010 0.9992 1.0012 40 1.8 0.9862 0.9917 1.0022 0.9981 1.0026 73 1.9 0.9715 0.9828 1.0046 0.9961 1.0054 128 1.95 0.8465 0.9072 1.0252 0.9745 1.0281 395 1.99

    2)

    4 1 0 1 0 1

    1 4 1 0 1 2

    0 1 4 1 0 , 1

    1 0 1 4 1 2

    0 1 0 1 4 1

    A b

  • 95

    ) (

    5

    ecnareloT )k(snoitaretI xk 1) ( xk2) ( xk3) ( xk4) ( xk 1 8 0001.0- 0007.0 0006.0- 0007.0 0001.0-

    1000.0

    2.1 8 0001.0- 0007.0 0006.0- 0007.0 0001.0- 4.1 9 0001.0- 0007.0 0006.0- 9996.0 0001.0- 6.1 41 0001.0- 9996.0 0006.0- 9996.0 0001.0- 8.1 72 1001.0- 8996.0 1006.0- 7996.0 2001.0-

    ROSL ROSP ) 3 2 -

    . ROSL ROSP

    9

    :

    x y

    .

    noitaxaler revo evisseccus tnioP ROSP

    :

    noitaxaler revo evisseccus eniL ROSL

    :

    1+k ROSP

    ,1)1 (( k

    uj i

    )1 (

    1 ,

    k

    uj i

    )

  • 16

    . ROS DRAWROF . k

    (

    )

    ROSF ROSP.

    . 2

    ROSL

    . DFC

    !!!

    . LPN LN

    . bb aa

    LN n n x xn ......1

    . LPN LPN

    .

    . 1+k

    LN,1=i oD

    i aaLPN i bb;1 )LPN )1 ((

    n,1=J oD

    ) ( )1 ( ) ( )1 (

    1 1

    ) ( ) 1(n bb

    k k k k

    j ji j ji i i i

    iibb j j

    x a x a b x xa

    OD DNE ;OD DNE

    - A b

    . b x ALPN LPN

  • 60

    program LSOR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Solving linear system (AX=b) by Line Successive over relaxation method(LSOR) !! !! 2) Investigation on effect of number of iteratives !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 06/05/14 Milad Naderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    implicit none !^^^^^^^^^^^^^^^^^^ Specification of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Allocatable::A(:,:),b(:),Xk(:),dif(:),Xk1(:),Aprim(:,:),bprim(:),Xkprim(:) !Real,Allocatable:: Real::tol,maxdif,dummy,len,w,f,e integer::i,j,k,n,NL,p,aa,bb,ia,jb,t,l,m,npl ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk5)) is unknowns vector for k+1/2 step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order ! ((NL)) is number of lines which divides coefficient matrix(A) to N/NL submatrix of NL order ! ((NPL)) is number of points in each line !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Write & Read Principal data ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Write(*,*) 'Please enter tolerance,tol= ' Read (*,*) tol Write(*,*) 'Please enter relaxation factor,w= ' Read (*,*) w Write(*,*) 'Please enter number of lines(NL),Note NL must be a integer multiplier of Matrix order(N),NL= ' Read (*,*) NL !^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ OPEN DAT FILES ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='b.dat',status='OLD', action='READ') !available b vector (input) Open(Unit=4,File='X0.dat',status='OLD', action='READ') !availabe initial value vector (input) Open(Unit=5,File='Results.dat') !Results file contains iterative number,maximum diffrence and xi at each level. ! INPUT A matrix n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) NPL=N/NL;ia=1;jb=NPL;e=0;f=0 allocate (A(n,n));allocate (APRIM(NPL,NPL)) allocate (Xk(n));allocate (Xk1(n));allocate (dif(n)) allocate (b(n));allocate (bprim(NPL));allocate (Xkprim(NpL))

  • 62

    do i=1,n read(2,*) ( A(i,j), j = 1, n ) end do !close(2) ! INPUT B vector do i=1,n read(3,*) b(i) end do !close(3) ! INPUT X0 vector k=0 do i=1,n read(4,*) Xk(i) end do !close(4) ! Write header in Results.dat ! Write(5,100) (i,i=1,n);100 FORMAT (((10x,'K|',1000000(3x,'X('I10')|'))));write(5,101) k,(XK(i),i=1,n); 101 FORMAT (7x,I4,1000000('|',F16.4)) ! First of loop! 300 dummy=0;len=0 ! Write a note text if number of iterations be large in Results.dat ! IF (k>0 .AND. MOD(k,50)==0) then;Write (5,200) k;200 FORMAT(/,'After'I10'steps, solution was not convergence',/);Write (*,400) k; 400 FORMAT(/,'After'I10'steps, solution was not convergence',/);END IF ! LSOR ! Do P=1,NL aa=((p-1)*NPL)+1;bb=P*NPL DO i=aa,bb DO j=1,n IF (j>bb) THEN f=f+A(i,j)*XK(j) ELSE IF ( (J =1) ) THEN e=e+A(i,j)*XK1(j) END IF END DO bprim(1+i-aa)=((1-w)*XK(i))+(w*b(i)/A(i,i))-e-f e=0;f=0 END DO Do L=1,NPL DO M=1,NPL IF (L==M) THEN APRIM(L,L)=1+w ELSE APRIM(L,M)=((w*A(aa+L-1,aa+m-1))/A(aa+L-1,aa+L-1)) END IF END DO END DO call GS (APRIM,Bprim,NPL,XKPRIM,tol) do t=1,NpL XK1(aa+t-1)=XKprim(t) END DO END DO

  • 63

    k=k+1 write(5,102) k,(XK1(i),i=1,n) 102 FORMAT (7x,I4,1000000('|',F16.4)) do i=1,n dif(i)=Xk1(i)-XK(i) if( abs(dif(i))>tol) then XK=XK1 go to 300 end if end do Write (5,*) 'solution is convergence' write(*,*) 'END OF SOR PROGRAM' END Program Subroutine GS(A,B,N,Xk1,tol) implicit none !^^^^^^^^^^^^^^^^^^ Sepecificaton of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real,Dimension(n,n)::A Real,Dimension(n)::b,Xk,Xk1,dif Real::tol,maxdif,dummy,len integer::i,j,k,n character::con,y ! ((A)) is coefficient square matrix (n*n) ! ((b)) is RHS of Ax=b and is a vector (n*1) ! ((Xk)) is unknowns vector for k step ! ((Xk1)) is unknowns vector for k+1 step ! ((Tolerance)) is allowable diffrence between Xk and Xk+1 (convergence factor) ! ((n)) is matrix order XK=0. k=0 300 dummy=0;len=0 If (k>0 .AND. MOD(k,50)==0) then Write (*,400) k 400 FORMAT(/,'After'I10'steps, solution was not convergence',/) END IF Do i=1,n Do j=1,i-1 len=len+A(i,j)*Xk1(j) End do Do j=i+1,n len=len+A(i,j)*Xk(j) end Do dummy=len+dummy Xk1(i)=(1./A(i,i))*(b(i)-dummy) dummy=0 len=0 END Do k=k+1 do i=1,n dif(i)=Xk1(i)-XK(i)

  • 64

    if( abs(dif(i))>tol) then XK=XK1 go to 300 end if end do write(*,*) 'END OF Gauss Sidel PROGRAM' END Subroutine;Return

    )

    .

    L .

    : .

    TA LL T

    ij jiL L Do i=1,n

    11

    2 2

    1

    ( )i

    ii ii ik

    k

    L a L

    1

    1

    1( ) 1, 2,...,

    i

    ji ij ik jk

    kii

    L a L L j i i nL

    END DO

    program CholeskiMilad !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Purposes: !! !! 1) Choleski Factorization in form of A=L*LT !! !! !! !! Notes: !! !! !! !! !! !! Record of revisions: !! !! Date Programmer Description of change !! !! ==== ========== ===================== !! !! 04/17/14 Milad Naderi Original code !! !! !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! implicit none !^^^^^^^^^^^^^^^^^^ Sepecificaton of all parameters used in this program ^^^^^^^^^^^^^^^^^^^^ ! Real*8,Allocatable::A(:,:),L(:,:),LT(:,:) Real*8::dummy,dummy2 integer::i,j,k,n ! ((A)) is Symmetric Positive definite matrix (n*n) ! ((L)) is Lower triangular matrix created by choleski factorization ! ((LT)) is Transpose of Lower triangular matrix created by choleski factorization

  • 65

    ! ((n)) is matrix order ! ((i,j,k)) are indices ! ((dummy and dummy2)) are dummy parameter in loops for calculate Sigma values. !** OPEN DAT FILES **! Open(Unit=2,File='A.dat',status='OLD', action='READ') !availabele A matrix (input) Open(Unit=3,File='L.dat') !Calculated L Matrix (output) Open(Unit=4,File='LT.dat') !Calculated LT Matrix (output) !** calculate size of A **! n=0 do read(2,*,end=20) dummy n=n+1 end do 20 rewind(2) !** Allocate A,L and LT matrices **! allocate (A(n,n));allocate (L(n,n));allocate (LT(n,n)) !** Read A **! do i=1,n read(2,*) ( A(i,j), j = 1, n ) end do !** First of calculations **! dummy=0.;dummy2=0. Do i=1,n Do k=1,i-1 dummy=dummy+(L(i,k))**2 END DO L(i,i)=sqrt((A(i,i)-dummy)) dummy=0 Do j=i+1,n Do k=1,i-1 dummy2=dummy2+(L(i,k)*L(j,k)) END DO L(j,i)=((A(j,i)-dummy2)/(L(i,i))) dummy2=0 END DO END DO DO 2000 i = 1,n DO 1000 j = 1,n 1000 write(3,'(F10.5,$)') L(i,j) 2000 write(3,*) DO i=1,n DO j=1,n LT(i,j)=L(j,i) END DO END DO DO 200 i = 1,n DO 100 j = 1,n 100 write(4,'(F10.5,$)') LT(i,j) 200 write(4,*) write(*,*) 'END OF Choleski decomposition PROGRAM' END Program

  • 66

    :

    16 4 4 -4

    4 10 4 2

    4 4 6 -2

    -4 2 -2 4

    A

    4.00000 0.00000 0.00000 0.00000

    1.00000 3.00000 0.00000 0.00000

    1.00000 1.00000 2.00000 0.00000

    -1.00000 1.00000 -1.00000 1.00000

    L

    4.00000 1.00000 1.00000 -1.00000

    0.00000 3.00000 1.00000 1.00000

    0.00000 0.00000 2.00000 -1.00000

    0.00000 0.00000 0.00000 1.00000

    TL

    2 :

    25 15 -5

    15 18 0

    -5 0 11

    A

    5.00000 0.00000 0.00000

    3.00000 3.00000 0.00000

    -1.00000 1.00000 3.00000

    L

    5.00000 3.00000 -1.00000

    0.00000 3.00000 1.00000

    0.00000 0.00000 3.00000

    TL

  • 67

    2 ) (

    Linear algebra .

    2 ) (

    Linear algebra :

    Linear Algebra

    MATLAB Linear Algebra Functions

    Matrices in the MATLAB Environment

    Systems of Linear Equations

    Inverses and Determinants

    Factorizations

    Powers and Exponentials

    Eigenvalues

    Singular Values

    Category Function Description

    Matrix analysis norm Matrix or vector norm

    normest Estimate the matrix 2-norm

    rank Matrix rank

    det Determinant

    trace Sum of diagonal elements

    null Null space

    orth Orthogonalization

    rref Reduced row echelon form

    subspace Angle between two subspaces

  • 68

    Category Function Description

    Linear equations \ and / Linear equation solution

    inv Matrix inverse

    cond Condition number for inversion

    condest 1-norm condition number

    estimate

    chol Cholesky factorization

    cholinc Incomplete Cholesky

    factorization

    linsolve Solve a system of linear

    equations

    lu LU factorization LU

    ilu Incomplete LU factorization LU

    luinc Incomplete LU factorization LU

    qr Orthogonal-triangular

    decomposition

    lsqnonneg Nonnegative least-squares

    pinv Pseudoinverse

    lscov Least squares with known

    covariance

    Eigenvalues and

    singular values

    eig Eigenvalues and eigenvectors

    svd Singular value decomposition SVD

    eigs A few eigenvalues

    svds A few singular values

    poly Characteristic polynomial

    polyeig Polynomial eigenvalue problem

    condeig Condition number for

  • 69

    Category Function Description

    eigenvalues

    hess Hessenberg form

    qz QZ factorization QZ

    schur Schur decomposition

    Matrix functions expm Matrix exponential Exponential

    logm Matrix logarithm

    sqrtm Matrix square root

    funm Evaluate general matrix

    function

  • 17

    :AMELP arbegla raeniL

    sdnammoC sisaB

    laimonyloPcitsiretcarahC

    tcudorPssorC

    woReteleD

    tnanimreteD

    noisnemiD

    tcudorPtoD

    seulavnegiE

    srotcevnegiE

    noitanimilEnaissuaG

    serauqStsaeL

    evloSraeniL

    b=X.A

    paM

    esrevnIxirtaM

    ylpitluMralacSxirtaM

    ecapSlluN

    )secapslluN) (sisab (

    irtaMmodnaRx

    mroFnolehcEwoRdecudeR

    xirtaMbuS

    esopsnarT

  • 07

    :ELPAM

    >

    5*5

    >

    . :

    >

    2

    >

    >

    >

    >

  • 27

    V M

    >

    >

    >

  • 37

    ( ) 3

    .

    evlos - nork leknah dnoc ruhcs dvsg giedro sdvs dvs lohc rq uli ul

    yrellag

    ( ) 3

    :ul

    :

    tinu a htiw L xirtam ralugnairt rewol a ,U ni xirtam ralugnairt reppu na snruter )A(ul = ]P,U,L[

    .A*P = U*L taht hcus ,P xirtam noitatumrep a dna ,lanogaid

    :

  • 74

    ilu:

    .

    .

    ilu(A,setup) returns L+U-speye(size(A)), where L is a unit lower triangular matrix and U

    is an upper triangular matrix.

    [L,U] = ilu(A,setup) returns a unit lower triangular matrix in L and an upper triangular

    matrix in U.

    [L,U,P] = ilu(A,setup) returns a unit lower triangular matrix in L, an upper triangular

    matrix in U, and a permutation matrix in P.

    ilu works on sparse square matrices only

    qr:

    The qr function performs the orthogonal-triangular decomposition of a matrix. This factorization is

    useful for both square and rectangular matrices. It expresses the matrix as the product of a real

    complex unitary matrix and an upper triangular matrix.

    [Q,R] = qr(A) produces an upper triangular matrix R of the same dimension as A and a unitary

    matrix Q so that A = Q*R. For sparse matrices, Q is often nearly full

    :

    A = [ 1 2 3

    4 5 6

    7 8 9

    10 11 12 ]

    [Q,R] = qr(A)

    Q =

    -0.0776 -0.8331 0.5444 0.0605

    -0.3105 -0.4512 -0.7709 0.3251

    -0.5433 -0.0694 -0.0913 -0.8317

    -0.7762 0.3124 0.3178 0.4461

    R =

    -12.8841 -14.5916 -16.2992

    0 -1.0413 -2.0826

    0 0 0.0000

    0 0 0

  • 75

    chol:

    .

    R = chol(A) produces an upper triangular matrix R from the diagonal and upper triangle of matrix

    A, satisfying the equation R'*R=A where R' is transpose of R. The lower triangle is assumed to be

    the (complex conjugate) transpose of the upper triangle. Matrix A must be positive definite.

    L = chol(A,'lower') produces a lower triangular matrix L from the diagonal and lower triangle

    of matrix A, satisfying the equation L*L'=A

    :

    A =

    1 -1 -1 -1 -1

    -1 2 0 0 0

    -1 0 3 1 1

    -1 0 1 4 2

    -1 0 1 2 5

    C=chol(A)

    ans =

    1 -1 -1 -1 -1

    0 1 -1 -1 -1

    0 0 1 -1 -1

    0 0 0 1 -1

    0 0 0 0 1

    isequal(C'*C,A)

    ans =

    1

    svd:

    ( Singular Value Decomposition)

    s = svd(X) returns a vector of singular values. .

    [U,S,V] = svd(X) produces a diagonal matrix S of the same dimension as X, with nonnegative

    diagonal elements in decreasing order, and unitary matrices U and V so that X = U*S*V'.

    [U,S,V] = svd(X,0) produces the "economy size" decomposition. If X is m-by-n with m > n, then

    svd computes only the first n columns of U and S is n-by-n.

    [U,S,V] = svd(X,'econ') also produces the "economy size" decomposition. If X is m-by-n with

    m >= n, it is equivalent to svd(X,0). For m < n, only the first m columns of V are computed and S is

    m-by-m.

  • 76

    :

    X =

    1 2

    3 4

    5 6

    7 8

    [U,S,V] = svd(X)

    U =

    -0.1525 -0.8226 -0.3945 -0.3800

    -0.3499 -0.4214 0.2428 0.8007

    -0.5474 -0.0201 0.6979 -0.4614

    -0.7448 0.3812 -0.5462 0.0407

    S =

    14.2691 0

    0 0.6268

    0 0

    0 0

    V =

    -0.6414 0.7672

    -0.7672 -0.6414

    [U,S,V] = svd(X,0)

    U =

    -0.1525 -0.8226

    -0.3499 -0.4214

    -0.5474 -0.0201

    -0.7448 0.3812

    S =

    14.2691 0

    0 0.6268

    V =

    -0.6414 0.7672

    -0.7672 -0.6414

    svds:

    .

    .

    s = svds(A) computes the six largest singular values and associated singular vectors of

    matrix A. If A is m-by-n, svds(A) manipulates eigenvalues and vectors returned by eigs(B),

    where B = [sparse(m,m) A; A' sparse(n,n)], to find a few singular values and vectors

    of A. The positive eigenvalues of the symmetric matrix B are the same as the singular values

    of A.

    s = svds(A,k) computes the k largest singular values and associated singular vectors of

    matrix A.

  • 77

    s = svds(A,k,sigma) computes the k singular values closest to the scalar shift sigma. For

    example, s = svds(A,k,0) computes the k smallest singular values and associated singular

    vectors.

    ordeig:

    E = ordeig(T) takes a quasitriangular Schur matrix T, typically produced by schur, and

    returns the vector E of eigenvalues in their order of appearance down the diagonal of T.

    E = ordeig(AA,BB) takes a quasitriangular matrix pair AA and BB, typically produced by qz,

    and returns the generalized eigenvalues in their order of appearance down the diagonal of AA-

    *BB.

    :

    A= rand(10)

    A =

    Columns 1 through 8

    0.8223 0.3262 0.6153 0.5815 0.0702 0.7337 0.1239 0.7284

    0.7229 0.9730 0.5831 0.9377 0.0693 0.6505 0.4674 0.4068

    0.9259 0.3650 0.6983 0.0478 0.1360 0.5163 0.6567 0.9383

    0.4926 0.3091 0.0293 0.0540 0.7889 0.3264 0.2902 0.2554

    0.6549 0.1209 0.5279 0.0206 0.0924 0.6618 0.7545 0.5332

    0.8901 0.9158 0.0321 0.6815 0.2379 0.1176 0.5581 0.9548

    0.5385 0.1355 0.8271 0.5986 0.2436 0.1478 0.4278 0.2677

    0.2822 0.3321 0.3400 0.1140 0.1048 0.0198 0.2672 0.2501

    0.9760 0.8975 0.8467 0.7962 0.8584 0.9643 0.7537 0.9277

    0.0364 0.4996 0.2461 0.6179 0.6982 0.9704 0.8984 0.0686

    Columns 9 through 10

    0.2994 0.1125

    0.5916 0.5158

    0.2033 0.8378

    0.6359 0.9208

    0.7984 0.4982

    0.5017 0.2776

    0.6508 0.6525

    0.7960 0.9173

    0.2334 0.5098

    0.6008 0.9742

    >> [U, T] = schur(A)

    U =

    Columns 1 through 7

  • 87

    1522.0 4930.0- 9022.0- 1303.0 5354.0- 9281.0- 5352.0-

    4750.0 7282.0- 6361.0 4565.0 5061.0- 2352.0- 2463.0-

    3330.0 3506.0- 4173.0- 4992.0- 0902.0 9722.0 3013.0-

    2045.0 7514.0 7602.0 3901.0 3825.0 4201.0 2362.0-

    4043.0- 3324.0 7714.0- 8710.0 0961.0 5954.0- 8982.0-

    0426.0- 6350.0 4660.0 1153.0 6922.0 2974.0 7403.0-

    9990.0- 8431.0 3151.0- 4844.0- 8650.0- 3052.0- 8872.0-

    7361.0 6943.0- 6160.0 4410.0- 3534.0 2882.0- 6132.0-

    6562.0 6632.0 5251.0- 0751.0- 7463.0- 3184.0 9054.0-

    3102.0- 6030.0- 6717.0 8073.0- 8691.0- 8741.0- 0553.0-

    01 hguorht 8 snmuloC

    5352.0 3266.0 9620.0-

    9034.0- 5793.0- 0560.0-

    2292.0- 5771.0 0013.0

    5542.0- 0542.0 2040.0

    9740.0 5921.0- 9234.0

    2011.0 1891.0 3132.0-

    2792.0- 6660.0 3517.0-

    3066.0 4181.0- 6722.0-

    8152.0 8934.0- 9820.0

    6740.0 3851.0 6303.0

    = T

    7 hguorht 1 snmuloC

    1201.0- 9133.0 9091.0- 5711.0- 7012.0- 2014.0 7101.5

    1960.0 0190.0- 4031.0- 5275.0 1423.0 7728.0- 0

    5320.0- 4801.0 3713.0 8665.0- 7728.0- 0672.0- 0

    0885.0 4790.0- 7510.0 4577.0 0 0 0

    6643.0- 2278.0 9954.0 0 0 0 0

    1122.0- 9954.0 4613.0- 0 0 0 0

    2783.0- 0 0 0 0 0 0

    9057.0- 0 0 0 0 0 0

    0 0 0 0 0 0 0

    0 0 0 0 0 0 0

    01 hguorht 8 snmuloC

    6343.0- 1829.0- 7325.0

    3721.0 9950.0 6841.0-

    5783.0 1093.0- 7991.0

    1790.0 0870.0- 9612.0-

    0475.0- 7515.0- 2012.0

    3270.0 2421.0- 4270.0

    7940.0 4811.0 8535.0

    7951.0 1530.0 2783.0-

    4904.0 9731.0 0

    9731.0 6091.0- 0

    )T(giedro >>

  • 79

    ans =

    5.1017

    -0.8277 + 0.2991i

    -0.8277 - 0.2991i

    0.7754

    0.4599 + 0.5253i

    0.4599 - 0.5253i

    -0.3872 + 0.6343i

    -0.3872 - 0.6343i

    0.1379 + 0.2793i

    gsvd:

    [U,V,X,C,S] = gsvd(A,B) returns unitary matrices U and V, a (usually) square matrix X, and

    nonnegative diagonal matrices C and S so that

    A = U*C*X'

    B = V*S*X'

    C'*C + S'*S = I

    A and B must have the same number of columns, but may have different numbers of rows. If A

    is m-by-p and B is n-by-p, then U is m-by-m, V is n-by-n and X is p-by-q where q =

    min(m+n,p).

    sigma = gsvd(A,B) returns the vector of generalized singular values,

    sqrt(diag(C'*C)./diag(S'*S)).

    :

    A = reshape(1:15,5,3)

    B = magic(3)

    A =

    1 6 11

    2 7 12

    3 8 13

    4 9 14

    5 10 15

    B =

    8 1 6

    3 5 7

    4 9 2

    [U,V,X,C,S] = gsvd(A,B)

    X =

    2.8284 -9.3761 -6.9346

    -5.6569 -8.3071 -18.3301

    2.8284 -7.2381 -29.7256

  • 81

    C =

    0.0000 0 0

    0 0.3155 0

    0 0 0.9807

    0 0 0

    0 0 0

    S =

    1.0000 0 0

    0 0.9489 0

    0 0 0.1957

    [U,V,X,C,S] = gsvd(A,B,0)

    U =

    0.5700 -0.6457 -0.4279

    -0.7455 -0.3296 -0.4375

    -0.1702 -0.0135 -0.4470

    0.2966 0.3026 -0.4566

    0.0490 0.6187 -0.4661

    C =

    0.0000 0 0

    0 0.3155 0

    0 0 0.9807

    The other three matrices, V, X, and S are the same as those obtained with the full

    decomposition.

    The generalized singular values are the ratios of the diagonal elements of C and S.

    sigma = gsvd(A,B)

    sigma =

    0.0000

    0.3325

    5.0123

    schur:

    ( Schur)

    The schur command computes the Schur form of a matrix.

    T = schur(A) returns the Schur matrix T.

    T = schur(A,flag) for real matrix A, returns a Schur matrix T in one of two forms

    depending on the value of flag:

    'complex' T is triangular and is complex if A has complex eigenvalues.

    'real' T has the real eigenvalues on the diagonal and the complex eigenvalues in 2-by-2

    blocks on the diagonal. 'real' is the default.

  • 80

    If A is complex, schur returns the complex Schur form in matrix T. The complex Schur form

    is upper triangular with the eigenvalues of A on the diagonal

    [U,T] = schur(A,...) also returns a unitary matrix U so that A = U*T*U' and

    U'*U = eye(size(A)).

    :

    H is a 3-by-3 eigenvalue test matrix:

    H = [ -149 -50 -154

    537 180 546

    -27 -9 -25 ]

    Its Schur form is

    schur(H)

    ans =

    1.0000 -7.1119 -815.8706

    0 2.0000 -55.0236

    0 0 3.0000

    The eigenvalues, which in this case are 1, 2, and 3, are on the diagonal. The fact that the off-

    diagonal elements are so large indicates that this matrix has poorly conditioned eigenvalues;

    small changes in the matrix elements produce relatively large changes in its eigenvalues.

    cond:

    .

    . 1 .

    c = cond(X) returns the 2-norm condition number, the ratio of the largest singular value of X

    to the smallest.

    c = cond(X,p) returns the matrix condition number in p-norm:

    norm(X,p) * norm(inv(X),p

    If p is... Then cond(X,p) returns the...

    1 1-norm condition number

    2 2-norm condition number

    'fro' Frobenius norm condition number

  • 28

    ...eht snruter )p,X(dnoc nehT ...si p fI

    rebmun noitidnoc mron ytinifnI fni

    :dnocr

    . k )y(

    1

    1 1

    1 1y

    A A k

    y 1 y

    .

    :leknah

    . leknaH

    )c(leknah = H

    )r,c(leknah = H

    .

    . dne c Pr] [: 2 j i P j i H)1 ( ) , (

    esohw dna c si nmuloc tsrif esohw xirtam leknaH erauqs eht snruter )c(leknah = H

    .lanogaid-itna tsrif eht woleb orez era stnemele

    fI .r si wor tsal esohw dna c si nmuloc tsrif esohw xirtam leknaH a snruter )r,c(leknah = H

    .sliaverp c fo tnemele tsal eht ,r fo tnemele tsrif eht morf sreffid c fo tnemele tsal eht

    :

    ;01:7 = r ;3:1 = c

    )r,c(leknah = h

    = h

    8 3 2 1

    9 8 3 2

    01 9 8 3

    ]01 9 8 3 2 1[ = p

  • 38

    :nork

    .

    )Y,X(nork = K

    . Kq n p m, Yq p, Xn m,

    :

    )3(dnar=A >>

    = A

    5872.0 4319.0 7418.0

    9645.0 4236.0 8509.0

    5759.0 5790.0 0721.0

    )2(dnar=B >>

    = B

    6079.0 9469.0

    2759.0 6751.0

    )B,A(nork >>

    = sna

    3072.0 7862.0 5688.0 3188.0 8097.0 1687.0

    6662.0 9340.0 3478.0 0441.0 8977.0 4821.0

    8035.0 7725.0 8316.0 2016.0 2978.0 0478.0

    5325.0 2680.0 3506.0 7990.0 0768.0 8241.0

    3929.0 9329.0 7490.0 1490.0 3321.0 5221.0

    5619.0 9051.0 4390.0 4510.0 5121.0 0020.0

    :evlos

    .

    )qe(evlos

    )rav,qe(evlos

    )nqe,...,2qe,1qe(evlos

    )nrav,...,2rav,1rav,nqe,...,2qe,1qe(evlos = g

  • 84

    :

    A = solve('3*x+5*y-6*z=4', '5*x-3*z=4', '2*x+6*y-2*z=0)'

    A =

    x: [1x1 sym]

    y: [1x1 sym]

    z: [1x1 sym]

    >>A.x

    ans =

    16/53

    >>A.y

    ans =

    -20/53

    >>A.z

    ans =

    -44/53

    gallery:

    .

    [A,B,C,...] = gallery(matname,P1,P2,...) returns the test matrices specified by the

    quoted string matname. The matname input is the name of a matrix family selected from the table

    below. P1,P2,... are input parameters required by the individual matrix family. The number of

    optional parameters P1,P2,... used in the calling syntax varies from matrix to matrix..

    :

  • 85

    :

    tridiag Tridiagonal matrix (sparse)

    A = gallery('tridiag',c,d,e) returns the tridiagonal matrix with subdiagonal c,

    diagonal d, and superdiagonal e. Vectors c and e must have length(d)-1.

    A = gallery('tridiag',n,c,d,e), where c, d, and e are all scalars, yields the Toeplitz

    tridiagonal matrix of order n with subdiagonal elements c, diagonal elements d, and

    superdiagonal elements e. This matrix has eigenvalues

    d + 2*sqrt(c*e)*cos(k*pi/(n+1))

    where k = 1:n.

    A = gallery('tridiag',n) is the same as A = gallery('tridiag',n,-1,2,-1), which

    is a symmetric positive definite M-matrix (the negative of the second difference matrix).

  • 68

    :

    )3,2,1,6,'gaidirt'(yrellag = A

    = A

    2 )1,1(

    1 )1,2(

    3 )2,1(

    2 )2,2(

    1 )2,3(

    3 )3,2(

    2 )3,3(

    1 )3,4(

    3 )4,3(

    2 )4,4(

    1 )4,5(

    3 )5,4(

    2 )5,5(

    1 )5,6(

    3 )6,5(

    2 )6,6(

  • 87

    : 1. A. Quarteroni, R. Sacco, F. Saleri, Numerical Mathematics (Second Edition), Springer 2007.

    2. B.N. Datta, Numerical Linear Algebra And Applications, Northern Illinois University.

    3. G.H. Golub, C.F. Van Loan, Matrix Computations (Third Edition), The Johns Hopkins University

    Press, 1996.

    4. T.J.Chung, Computational Fluid Dynamics (Second Edition), Cambridge University Press, 2010.