[Fortran] 纯文本查看 复制代码
Program lagrange
Implicit none
integer::t,n,i,j,k
real::tmp
real,dimension(1)::x,y
real,dimension(2)::p(17,2)
print*,"请输入气压值"
read*,t
open(10,file='d:\my fortran\air_tmp.txt')
read(10,*)((p(k,j),j=1,2),k=1,17)!17行2列
close(10)
do i=1,17
x(i)=p(i,1)
y(i)=p(i,2)
enddo
print*,x,y
tmp=fun(t)
print*,tmp
contains
function fun(t) result(tmp)
implicit none
integer::t,n,i,j
real::tmp
real,dimension(1)::x,y,l
do i=1,n
do j=1,n
if(i/=j) then
l(i)=l(i)*(t-x(j))/(x(i)-x(j))
else
return
endif
enddo
tmp=l(i)*y(i)+tmp
enddo
endfunction fun
end program
[Fortran] 纯文本查看 复制代码
Program lagrange
Implicit none
Integer , parameter :: S = 17
real :: tmp , t , x(S) , y(S)
integer :: i
write(*,*) "请输入气压值"
read(*,*) t
open(10,file='a.txt')
do i = 1 , size(x)
read(10,*) x(i),y(i)
write(*,*) x(i),y(i)
end do
close(10)
tmp=fun(t)
write(*,*) tmp
contains
function fun(t) result(tmp)
integer :: i , j
real :: tmp , t , l
tmp = 0.0
do i = 1 , size(x)
l = 1.0
do j = 1 , size(x)
if( i /= j ) then
l = l * (t-x(j))/(x(i)-x(j))
end if
end do
tmp= l * y(i) + tmp
end do
endfunction fun
end program lagrange