-
Notifications
You must be signed in to change notification settings - Fork 0
/
1a_2016.f95
194 lines (150 loc) · 3.09 KB
/
1a_2016.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
program Fitting
implicit none
integer, parameter :: N=10,M=5
real*8 :: x(N),f(N),C(M+1,M+1),A(M+1),B(M+1)
integer :: i,j,k,Np
character*50 :: datafile,outputfile,datafile2
real*8 :: dummy,xmin,xmax,xx,dx,p,sums
datafile="data_2016.txt"
datafile2="data_SI_2016.txt"
outputfile="hasil_fitting.txt"
Np=100
! Baca data
open(unit=10, file=datafile, status="old")
do i=1,N
read(10,*) x(i), f(i)
end do
do i=1,N
write(*,*) x(i), f(i)
end do
open(unit=30, file=datafile2, status="unknown")
do i=1,N
write(30,*) x(i), f(i)/3.6
end do
f(:) = f(:)/3.6
! Menghitung koefisien-koefisien C(k,j)
do k=0,M
do j=0,M
dummy=0.d0
do i=1,N
dummy=dummy+x(i)**(j+k)
end do
C(k+1,j+1)=dummy
end do
end do
! Menghitung koefisien-koefisien B(k)
do k=0,M
dummy=0.d0
do i=1,N
dummy=dummy+f(i)*x(i)**k
end do
B(k+1)=dummy
end do
! Menampilkan hasil penghitungan matriks C dan B
write(*,*)
write(*,*) "Matriks C :"
do i=0,M
write(*,*) (C(i,j), j = 0,m)
end do
write(*,*)
write(*,*) "Matriks B :"
do i=0,M
write(*,*) B(i)
end do
call LUdcmp(M+1,C,B,A)
write(*,*) 'nilai koef poliomial :',A
! Konstruksi polinomial p(x)
open(unit=20, file=outputfile, status="unknown")
xmin=X(1)
xmax=X(N)
dx=(xmax-xmin)/(Np-1)
do i=1,Np
xx=xmin+(i-1)*dx
p=0.d0
do j=1,M+1
p=p+A(j)*xx**(j-1)
end do
write(20,*) xx, p
end do
write(*,*) "Perhitungan fitting selesai. Hasil disimpan dalam file ",outputfile
! sums=0.!
! do j=1,M+1
! sums=sums+A(j)*47**(j-1)
! end do
! write(*,*) 'ini perkiraannya:', sums
! stop
contains
subroutine LUdcmp(N,A,B,X)
implicit none
integer :: i,j,N,p,k
real*8 :: A(N,N),B(N),X(N)
real*8 :: L(N,N),U(N,N),Y(N),Ls(N),As(N)
real*8 :: Bs, dummy
! Dekomposisi A menjadi LU
L(:,:)=0.d0
U(:,:)=0.d0
do i=1,N
U(i,i)=1.d0
end do
do p=1,N
! Kerjakan kolom-kolom L
j=p
do i=j,N
if (j==1) then
L(i,1)=A(i,1)
else
dummy=0.0
do k=1,j-1
dummy=dummy+L(i,k)*U(k,j)
end do
L(i,j)=A(i,j)-dummy
end if
end do
! Jika ada elemen diagonal L yang berharga nol, maka tukar baris L tsb
! dengan baris dibawahnya.
if (L(p,p)==0.0) then
Ls(:)=L(p,:)
L(p,:)=L(p+1,:)
L(p+1,:)=Ls(:)
As(:)=A(p,:)
A(p,:)=A(p+1,:)
A(p+1,:)=As(:)
Bs=B(p)
B(p)=B(p+1)
B(p+1)=Bs
end if
! Kerjakan baris-baris matriks U
i=p
do j=i,N
if (i==1) then
U(1,j)=A(1,j)/L(1,1)
else
dummy=0.0
do k=1,i-1
dummy=dummy+L(i,k)*U(k,j)
end do
U(i,j)=(A(i,j)-dummy)/L(i,i)
end if
end do
end do ! p
! Mencari vektor Y dengan substitusi maju
Y(1)=B(1)/L(1,1)
do i=2,N
dummy=0.d0
do j=1,i-1
dummy=dummy+L(i,j)*Y(j)
end do
Y(i)=(B(i)-dummy)/L(i,i)
end do
! Mencari vektor X dengan substitusi mundur
X(N)=Y(N)
do i=1,N-1
dummy=0.d0
do j=N-i+1,N
dummy=dummy+U(N-i,j)*X(j)
end do
X(N-i)=Y(N-i)-dummy
end do
return
end subroutine
end program