-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.f90
65 lines (48 loc) · 1.47 KB
/
interp.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
! interp.f90 --
!
! Example belonging to "Modern Fortran in Practice" by Arjen Markus
!
! This work is licensed under the Creative Commons Attribution 3.0 Unported License.
! To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/
! or send a letter to:
! Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
!
! Straightforward version of interpolation
!
module interpolation
implicit none
contains
real function interpolate( x, y, xp )
real, dimension(:), intent(in) :: x
real, dimension(:), intent(in) :: y
real, intent(in) :: xp
integer :: i
integer :: idx
!
! Search for the interval that contains xp
!
idx = size(x) - 1
do i = 2,size(x)-1
if ( xp < x(i) ) then
idx = i - 1
exit
endif
enddo
interpolate = y(idx) + (xp - x(idx)) * (y(idx+1) - y(idx)) / &
(x(idx+1) - x(idx))
end function interpolate
end module interpolation
program test_interpolation
use interpolation
implicit none
real, dimension(6) :: x = (/ 0.0, 1.0, 10.0, 10.0, 20.0, 120.0 /)
real, dimension(6) :: y = (/ 0.0, 2.0, 20.0, 20.0, 10.0, 10.0 /)
integer :: i
real :: xp
do i = 1,20
xp = -4.0 + 1.0 * i
write(*,'(2g12.4)') xp, interpolate( x, y, xp )
enddo
xp = 9.8
write(*,'(2g12.4)') xp, interpolate( x, y, xp )
end program