我几周前发了一篇关于这个问题的帖子并对其进行了编辑,但我不确定它是否仍然有效。所以,既然它仍然困扰着我,我会创造一个新的,但希望能有更好的形式,我希望。
对于赋值,我应该以牛顿形式插值函数值和导数值。我可以在纸上完成,但是当我把它变成一个fortran代码时,我完全迷失了。我查看了不同的源代码,但是他们要么打开文件要么输入要求,我给了5组值并且必须使用它们。我能够编写一个小程序,在没有衍生物的情况下进行插值。然后我尝试将衍生物添加到它。 在下面的代码中" yp"是衍生物
program main
implicit none
double precision , allocatable , dimension (:,:) :: nt
double precision , allocatable , dimension (:) :: xnodes, yval, yp
double precision :: x, evalnewton
integer :: i,n,k
n = 7
allocate ( xnodes (0:n), yval (0:n), yp (0:n), nt (0:n, 0:n) )
xnodes = (/ 1.32d0, 2.47d0, 5.81d0, 6.83d0, 7.0d0 /)
yval = (/ 5.63d0, 6.11d0, 8.12d0, 4.33d0, 6.15d0 /)
yp = (/ 1.29d0, 2.21d0, 1.48d0, -3.61d0, 3.11d0 /)
call computent (n, xnodes, yval, yp, nt)
do i = 0,n
x = xnodes(i)
print*, xnodes(i), yval(i), yp(i), evalnewton (n, xnodes, nt, x)
end do
open (unit = 4, file = 'z', status = 'replace')
do i = 0,100
x = dble(i) * 1.0d-1
write (4,*) x, evalnewton (n, xnodes, nt, x)
end do
close (4)
deallocate (xnodes, yval, yp)
stop
end
subroutine computent (n, xnodes, yval, yp, nt)
implicit none
integer :: i, j, n, k, top, bot
double precision :: nt (0:n, 0:n)
double precision xnodes (0:n), yval (0:n), yp(0:n)
double precision :: x, evalnewton
nt = 0.0d0
do i = 0,n
nt (i,0) = yval (i)
end do
do k = 1,n
top = k
bot = 0
do i = k,n
nt (i,k) = (nt (i,k-1) - nt (i-1,k-1))/(xnodes(top) - xnodes(bot))
top = top + 1
bot = bot + 1
end do
print*, 'Column number', k
do j = 0,n
print*, nt (j,k)
end do
end do
return
end
double precision function evalnewton (n, xnodes, nt, x)
implicit none
integer :: i, j, n, k, top, bot
double precision :: nt(0:n, 0:n)
double precision xnodes (0:n), yval(0:n), yp(0:n)
double precision :: x
evalnewton = nt(n,n)
do i = n, 1, -1
evalnewton = evalnewton * (x - xnodes (i-1)) + nt (i-1, i-1)
end do
return
end
说实话,我刚刚添加了#34; yp"无处不在,我在哪里" yval"希望神秘的解决者能够发挥它的魔力。我还没想出要添加什么,所以程序会在计算中包含衍生物。如果有人能解释我接下来要采取的步骤,那就太好了。
编辑: 我添加了一些对computent的小改动,我认为可以解决这个问题: 我i = 0,n nt(i * 2,0)= yval(i) nt(i * 2 + 1,0)= yval(i) 结束了
do k = 1,n
top = k
bot = 0
do i = k,n
nt (i,k) = (nt (i,k-1) - nt (i-1,k-1))/(xnodes(top) - xnodes(bot))
if (nt(i,k) == 0) then
nt(i,k) = yp(i/2)
end if
top = top + 2
bot = bot + 2
end do
print*, 'Column number', k
do j = 0,n
print*, nt (j,k)
end do
现在作为第一个数字我得到2.420 ... e ^ -322,这个数字搞乱了其余的计算。任何想法如何摆脱它?
答案 0 :(得分:0)
我很漂亮,我发现了一个小问题。我把它作为答案以防万一其他人需要它。
program main
implicit none
double precision , allocatable , dimension (:,:) :: yt
double precision , allocatable , dimension (:) :: xnodes, yval, yp, xt
double precision :: x, evalnewton
integer :: i,n,k
n = 9
allocate ( xnodes (0:n), yval (0:n), yp (0:n), yt (0:n, 0:n), xt (0:n) )
xnodes = (/ 1.32d0, 2.47d0, 5.81d0, 6.83d0, 7.0d0 /)
yval = (/ 5.63d0, 6.11d0, 8.12d0, 4.33d0, 6.15d0 /)
yp = (/ 1.29d0, 2.21d0, 1.48d0, -3.61d0, 3.11d0 /)
call computent (n, xnodes, yval, yp, yt, xt)
do i = 0,n
x = xnodes(i)
print*, xnodes(i), yval(i), yp(i), evalnewton (n, xnodes, yt, x)
end do
open (unit = 4, file = 'z', status = 'replace')
do i = 0,100
x = dble(i) * 1.0d-1
write (4,*) x, evalnewton (n, xnodes, yt, x)
end do
close (4)
deallocate (xnodes, yval, yp, yt, xt)
stop
end
subroutine computent (n, xnodes, yval, yp, yt, xt)
implicit none
integer :: i, j, n, k, top, bot
double precision :: yt (0:n, 0:n)
double precision xnodes (0:n), yval (0:n), yp(0:n), xt (0:n)
double precision :: x, evalnewton
xt = 0.0d0
yt = 0.0d0
do i = 0,n
xt(i*2) = xnodes (i)
xt(i*2+1) = xnodes (i)
yt(i*2,0) = yval (i)
yt(i*2+1,0) = yval (i)
end do
do k = 1,n
do i = k,n
yt(i,k) = (yt(i,k-1) - yt(i-1,k-1))/(xt(i) - xt(i-1))
if (xt(i) - xt(i-1) == 0) then
yt(i,k) = yp(i/2)
end if
end do
print*, 'Column number', k
do j = 0,n
print*, yt (j,k)
end do
end do
return
end
double precision function evalnewton (n, xnodes, yt, x)
implicit none
integer :: i, j, n, k, top, bot
double precision :: yt(0:n, 0:n)
double precision xnodes (0:n), yval(0:n), yp(0:n)
double precision :: x
evalnewton = yt(n,n)
do i = n, 1, -1
evalnewton = evalnewton * (x - xnodes (i-1)) + yt (i-1, i-1)
end do
return
end
小问题是,当我运行程序并且未创建文件时,我收到错误消息
错误讯息: a.out:malloc.c:2372:sysmalloc:断言`(old_top ==(((mbinptr)(((char *)&((av) - > bins [((1) - 1)* 2] )) - __builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((unsigned long)(old_size)> =(unsigned long)(((__ builtin_offsetof(struct malloc_chunk,fd_nextsize))+((2 *(sizeof(size_t))) - 1))&〜((2 * (sizeof(size_t))) - 1)))&&((old_top) - > size& 0x1)&&((unsigned long)old_end& pagemask)== 0)'失败。
编程接收信号SIGABRT:处理中止信号。
此错误的Backtrace: