是否有一个 Fortran 子程序在一维数据中执行线性插值?我需要类似于 MATLAB 函数 interp1 的东西。
Fortran 中的线性插值
计算科学
正则
插值
2021-12-03 13:36:37
2个回答
没有内置的 Fortran 功能可以进行线性插值。您可以使用库或编写自己的例程。我没有尝试编译或测试,我的 fortran 可能有点生锈,但类似下面的东西应该可以工作。
subroutine interp1( xData, yData, xVal, yVal )
! Inputs: xData = a vector of the x-values of the data to be interpolated
! yData = a vector of the y-values of the data to be interpolated
! xVal = a vector of the x-values where interpolation should be performed
! Output: yVal = a vector of the resulting interpolated values
implicit none
real, intent(in) :: xData(:), yData(:), xVal(:)
real, intent(out) :: yVal(:)
integer :: inputIndex, dataIndex
real :: minXdata, minYdata, xRange, weight
! Possible checks on inputs could go here
! Things you may want to check:
! monotonically increasing xData
! size(xData) == size(yData)
! size(xVal) == size(yVal)
minXData = xData(1)
maxXData = xData(size(xData))
xRange = maxXData - minXData
do inputIndex = 1, size(xVal)
! possible checks for out of range xVal could go here
! this will work if x is uniformly spaced, otherwise increment
! dataIndex until xData(dataIndex+1)>xVal(inputIndex)
dataIndex = floor((xVal(inputIndex)-minXData)/xRange);
weight = (xVal - xData(dataIndex))/(xData(dataIndex+1)-xData(dataIndex));
yVal(inputIndex) = (1.0-weight)*yData(dataIndex) + ...
weight*yData(dataIndex+1);
end do
end subroutine
其它你可能感兴趣的问题