-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaussQuad.f90
110 lines (80 loc) · 2.23 KB
/
gaussQuad.f90
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
module gaussquad
contains
function func(x) result(y)
use types
real(kind=rkind), intent(in) :: x
real(kind=rkind) :: y
y = (x)/(sin(x) + 2)
!(x)/(sin(x) + 2)
!exp(-x*x)
end function func
subroutine gauss(integ,a,b,ndeg,func)
use types
real(kind=rkind), intent(in) :: a, b
real(kind=rkind), intent(out) :: integ
real(kind=rkind) :: j
integer(kind=ikind) :: ndeg,n=1
real(kind=rkind), dimension(:,:), allocatable :: c,d
real(kind=rkind), dimension(:), allocatable :: x
integer :: i,k,datafile, datfile,io,dfile
interface
function func(x) result(y)
use types
real(kind=rkind), intent(in) :: x
real(kind=rkind) :: y
end function func
end interface
open(newunit=datfile, file="nodes.mtx")
open(newunit=datafile, file="weights.mtx")
do
read(datfile,*,iostat=io)
if (io/=0) exit
end do
print*, ndeg
allocate(c(ndeg,ndeg))
rewind(datfile)
do
read(datafile,*,iostat=io)
if (io/=0) exit
end do
print*, ndeg
allocate(d(ndeg,ndeg))
rewind(datafile)
allocate(x(ndeg))
print*, " "
print*, "---------------------nodes-----------------------"
do i=1,ndeg
read(unit=datfile, fmt=*) c(i,:)
write(*,*) c(i,:)
end do
print*, " "
print*, "---------------------weights-----------------------"
do i=1,ndeg
read(unit=datafile, fmt=*) d(i,:)
print*, d(i,:)
end do
print*, " "
print*, "-----------------------"
integ = 0.0_rkind
j = 0.0_rkind
open(newunit=dfile, file="gaussconverg.mtx")
do k=1,ndeg
do i=1,ndeg
x(k) = (b+a)/2.0_rkind + (b-a)*c(k,i)/2.0_rkind
j = j+ d(k,i) * func(x(k))
end do
integ = j * (b-a)/2.0_rkind
print *, "n degree: ", n, "integral aproximation: ", integ
write(unit=dfile, fmt=*)integ, abs((1.0_rkind)-integ), log(abs((1.0_rkind)-integ))
write(*,*)
if(n >= ndeg) then
print*, "end"
exit
else
n=n+1
end if
end do
close(datfile)
close(datafile)
end subroutine gauss
end module gaussquad