拉格朗日插值fortran程序

Program LagrangianInterpolation
  Implicit none
  Integer :: i, fileid 
  Integer, parameter :: n1 = 51  !// 插值基节点数
  Real(kind=8), parameter :: t0 = 0.d0, t1 = 1.d0  !// 插值区间
  Real(kind=8) :: x(n1), y(n1)
  
  !// 获取插值基节点数据
  Open ( newunit = fileid, file = 'origin.dat' )
  Do i = 1, n1
    x(i) = t0 + ( i-1 ) * ( t1-t0 ) / ( n1-1 )
    y(i) = exp( x(i) )
    Write ( fileid,'(2es)' ) x(i), y(i)
  End do 
  Close ( fileid )
  !// 求插值
  call GetInterpolation( x, y, n1, t0, t1 )
End program LagrangianInterpolation
  
Subroutine GetInterpolation( x, y, n1, t0, t1 )
  Implicit none
  Integer :: i, j, k, fileid
  Integer, intent(in) :: n1
  Integer, parameter :: n = 101
  Real(kind=8), intent(in) :: x(n1), y(n1), t0, t1 
  Real(kind=8) :: xx(n), yy(n), L1(n1), L2(n1), tmp
  
  Open ( newunit = fileid, file = 'chazhi.dat' )
  Do k = 1, n
    xx(k) = t0 + ( k-1 ) * ( t1-t0 ) / ( n-1 )
    Do i = 1, n1
      L1(i) = 1.d0
      Do j = 1, n1
        If ( j /= i ) then
          L1(i) = ( x(i) - x(j) ) * L1(i)  !// 求分母
        End if 
      End do
      
      L2(i) = 1.d0
      Do j = 1, n1
        If ( j /= i ) then
          L2(i) = ( xx(k) - x(j) ) * L2(i)  !// 求分子
        End if
      End do
    End do
    
    tmp = 0.d0
    Do i = 1, n1
      tmp = y(i) * L2(i) / L1(i) + tmp  !// 求插值项
    End do
    yy(k) = tmp
    Write ( fileid,'(2es)' ) xx(k), yy(k)
  End do
  Close ( fileid )
End subroutine GetInterpolation

 

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值