-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJACOBI_METHOD.f95
124 lines (124 loc) · 2.26 KB
/
JACOBI_METHOD.f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
!DIAGONALIZATION OF A SYMMETRIC MATRIX: JACOBI METHOD
PROGRAM JACOBI_METHOD
IMPLICIT NONE
!A:ORIGINAL MATRIX; D:DIAGONAL MATRIX;
INTEGER I,J,K,P,Q,N,NCOUNT,IT,MAX
REAL A(100,100),D(100,100),HIGH,PHI,C,S,T(100,100),XPJ
REAL XQJ,TEMP,B(100)
!N:NO. OF ROWS AND COLUMNS
PRINT *, 'ENTER THE DIMENSION OF THE MATRIX'
READ(*,*)N
MAX=((N**2)-N)
!INITIALIZE THE TRANSFORMING MATRIX(T)
DO I=1,N
DO J=1,N
IF(I.EQ.J)THEN
T(I,J)=1.0
ELSE
T(I,J)=0.0
ENDIF
ENDDO
ENDDO
PRINT *, 'ENTER THE VALUES IN MATRIX A'
DO I=1,N
READ *, (A(I,J),J=1,N)
ENDDO
!INITIALISING THE DIAGONAL MATRIX
DO I=1,N
DO J=1,N
D(I,J)=0.0D0
ENDDO
ENDDO
IT=0
PRINT *, 'THE DIAGONALISED MATRIX IS:'
!SEARCH FOR THE HIGHEST OFF-DIAGONAL ELEMENT
12 HIGH=0.0D0
P=0
Q=0
IT=IT+1
DO I=1,N
DO J=1,N
IF((I.NE.J).AND.(ABS(A(I,J)).GT.HIGH))THEN
HIGH=ABS(A(I,J))
P=I
Q=J
ENDIF
ENDDO
ENDDO
!FINDING THE ANGLE O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ENDDO
WRITE(*,*)'THE EIGEN VALUES ARRANGED IN ASCENDING ORDER ARE:'
DO I=1,N
WRITE(*,*)(D(I,J),J=1,N)
ENDDO
WRITE(*,*)'THE EIGEN VECTOR MATRIX IS:'
DO I=1,N
WRITE(*,*)(T(I,J),J=1,N)
ENDDO
END PROGRAM JACOBI_METHOD