Skip to content

Commit ebe70c9

Browse files
Add spline interpolation
1 parent 13f534d commit ebe70c9

File tree

4 files changed

+453
-0
lines changed

4 files changed

+453
-0
lines changed

examples/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ add_executable(distribution_example distribution_example.f90)
7777
target_link_libraries(distribution_example fstats)
7878
target_link_libraries(distribution_example ${fplot_LIBRARY})
7979
target_include_directories(distribution_example PUBLIC ${fplot_INCLUDE_DIR})
80+
81+
# Interpolation Example
82+
add_executable(interpolation_example interpolation_example.f90)
83+
target_link_libraries(interpolation_example fstats)
84+
target_link_libraries(interpolation_example ${fplot_LIBRARY})
85+
target_include_directories(interpolation_example PUBLIC ${fplot_INCLUDE_DIR})

examples/interpolation_example.f90

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
program example
2+
use iso_fortran_env
3+
use fstats
4+
use fplot_core
5+
implicit none
6+
7+
! Parameters
8+
integer(int32), parameter :: n = 9
9+
integer(int32), parameter :: m = 100
10+
11+
! Local Variables
12+
type(spline_interpolator) :: interp
13+
real(real64) :: x(n), y(n), xi(m), yi1(m), yi2(m)
14+
15+
! Plot Variables
16+
type(plot_2d) :: plt
17+
type(plot_data_2d) :: pd1, pd2, pd3
18+
class(legend), pointer :: lgnd
19+
20+
! Initialization
21+
x = [-4.0d0, -3.0d0, -2.0d0, -1.0d0, 0.0d0, 1.0d0, 2.0d0, 3.0d0, 4.0d0]
22+
y = [0.0d0, 0.15d0, 1.12d0, 2.36d0, 2.36d0, 1.46d0, 0.49d0, 0.06d0, &
23+
0.0d0]
24+
xi = linspace(minval(x), maxval(x), m)
25+
26+
! Interpolation - Default
27+
call interp%initialize(x, y)
28+
call interp%interpolate(xi, yi1)
29+
30+
! Interpolation - defined slope at both ends
31+
call interp%initialize(x, y, &
32+
ibcbeg = SPLINE_KNOWN_FIRST_DERIVATIVE, ybcbeg = 0.0d0, &
33+
ibcend = SPLINE_KNOWN_FIRST_DERIVATIVE, ybcend = 0.0d0)
34+
call interp%interpolate(xi, yi2)
35+
36+
! Plot the results
37+
call plt%initialize()
38+
lgnd => plt%get_legend()
39+
call lgnd%set_is_visible(.true.)
40+
41+
call pd1%define_data(x, y)
42+
call pd1%set_name("Data")
43+
call pd1%set_draw_line(.false.)
44+
call pd1%set_draw_markers(.true.)
45+
call pd1%set_marker_style(MARKER_X)
46+
call pd1%set_marker_scaling(2.0)
47+
call pd1%set_line_width(2.0)
48+
call pd1%set_line_color(CLR_BLACK)
49+
call plt%push(pd1)
50+
51+
call pd2%define_data(xi, yi1)
52+
call pd2%set_name("Default")
53+
call pd2%set_line_width(2.0)
54+
call pd2%set_line_color(CLR_RED)
55+
call plt%push(pd2)
56+
57+
call pd3%define_data(xi, yi2)
58+
call pd3%set_name("Enforced End Slope")
59+
call pd3%set_line_style(LINE_DASHED)
60+
call pd3%set_line_width(2.0)
61+
call pd3%set_line_color(CLR_BLUE)
62+
call plt%push(pd3)
63+
64+
call plt%draw()
65+
end program

src/fstats.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ module fstats
132132
public :: base_interpolator
133133
public :: linear_interpolator
134134
public :: polynomial_interpolator
135+
public :: spline_interpolator
136+
public :: SPLINE_QUADRATIC_OVER_INTERVAL
137+
public :: SPLINE_KNOWN_FIRST_DERIVATIVE
138+
public :: SPLINE_KNOWN_SECOND_DERIVATIVE
139+
public :: SPLINE_CONTINUOUS_THIRD_DERIVATIVE
135140
end module

0 commit comments

Comments
 (0)