主题:[讨论]矩阵输出问题
问题: 1.输出矩阵interpolate(10,2)时为什么从第六行开始全部为零?
2.怎样按照10*2的矩阵方式规则输出数据?
以下是输出数据: -5.0000000 -3.8888888 -2.7777777 -1.6666665 -0.55555534 0.55555582 1.6666670 2.7777781 3.8888893 5.0000005 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
下面是程序:
module INTERPOLATE_UTILITY
implicit none
real, parameter :: PI=3.14159
real, parameter :: xmin = -5.0, xmax = 5.0
integer, parameter :: m = 3,n=2, NP1 = 10, NP2 =2
real :: datas(m,n)
real :: interpolate(NP1,NP2)
contains
subroutine GenerateData(func)
real, external :: func
real r, width
integer l
width = (xmax-xmin)/(m-1)
r = xmin
do l=1,m
datas(l,1) = r
datas(l,2) = func(datas(l,1))
r = r+width
end do
end subroutine
real function lagrange(x)
real x
real :: coeff(m)
integer i,j,k,ans
ans = 0
do i=1,m!using i to control the number of points,m
do j=1,m!using j to control the number of lj(x)
coeff(m)=1.0
if ( i==j )then
coeff(j)=coeff(j)
else
coeff(j) = coeff(j)* (x-datas(i,1))/(datas(j,1)-datas(i,1))
end if
end do
do k=1,m
ans = ans + coeff(k)*datas(k,2)!computing pi(x)=yi*li
end do
end do
ans=ans*funk(x)!compute pi(x)*f(x),that is the goal function
lagrange=ans
end function
real function funk(x)
implicit none
real :: x
funk=1.0/(1.0+x*x)
return
end function
end module
program main
use INTERPOLATE_UTILITY
implicit none
real,external :: func
real xinc,x
integer k
call GenerateData(funk)!put m points(xi,yi) into the matrix of datas(m)
x=-5.0
xinc = (xmax-xmin)/(NP1-1)
do k=1,NP1
interpolate(k,1) = x!segmenting the domain of (-5,5) into np1 fragments
interpolate(k,2)= lagrange(x)
x = x+xinc
end do
OPEN(10,file='test0.dat')
WRITE(10,*)interpolate !exporting the value of matrix interpolate
CLOSE(10)
end program
2.怎样按照10*2的矩阵方式规则输出数据?
以下是输出数据: -5.0000000 -3.8888888 -2.7777777 -1.6666665 -0.55555534 0.55555582 1.6666670 2.7777781 3.8888893 5.0000005 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
下面是程序:
module INTERPOLATE_UTILITY
implicit none
real, parameter :: PI=3.14159
real, parameter :: xmin = -5.0, xmax = 5.0
integer, parameter :: m = 3,n=2, NP1 = 10, NP2 =2
real :: datas(m,n)
real :: interpolate(NP1,NP2)
contains
subroutine GenerateData(func)
real, external :: func
real r, width
integer l
width = (xmax-xmin)/(m-1)
r = xmin
do l=1,m
datas(l,1) = r
datas(l,2) = func(datas(l,1))
r = r+width
end do
end subroutine
real function lagrange(x)
real x
real :: coeff(m)
integer i,j,k,ans
ans = 0
do i=1,m!using i to control the number of points,m
do j=1,m!using j to control the number of lj(x)
coeff(m)=1.0
if ( i==j )then
coeff(j)=coeff(j)
else
coeff(j) = coeff(j)* (x-datas(i,1))/(datas(j,1)-datas(i,1))
end if
end do
do k=1,m
ans = ans + coeff(k)*datas(k,2)!computing pi(x)=yi*li
end do
end do
ans=ans*funk(x)!compute pi(x)*f(x),that is the goal function
lagrange=ans
end function
real function funk(x)
implicit none
real :: x
funk=1.0/(1.0+x*x)
return
end function
end module
program main
use INTERPOLATE_UTILITY
implicit none
real,external :: func
real xinc,x
integer k
call GenerateData(funk)!put m points(xi,yi) into the matrix of datas(m)
x=-5.0
xinc = (xmax-xmin)/(NP1-1)
do k=1,NP1
interpolate(k,1) = x!segmenting the domain of (-5,5) into np1 fragments
interpolate(k,2)= lagrange(x)
x = x+xinc
end do
OPEN(10,file='test0.dat')
WRITE(10,*)interpolate !exporting the value of matrix interpolate
CLOSE(10)
end program