fortran - Polynomial Interpolation with derivatives -
i made post issue few weeks ago , edited i'm not sure if it's still active. since still bothering me i'm making new 1 in better form, hope.
for assignment supposed interpolate function values , derivative values in newton form. can on paper i'm lost when comes turning fortran code. have looked @ different source codes either opened file or asked input, given 5 sets of values , have use those. able write small program interpolation without derivatives. tried add derivatives it. in code below "yp" derivatives
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) = 0,n x = xnodes(i) print*, xnodes(i), yval(i), yp(i), evalnewton (n, xnodes, nt, x) end open (unit = 4, file = 'z', status = 'replace') = 0,100 x = dble(i) * 1.0d-1 write (4,*) x, evalnewton (n, xnodes, nt, x) end 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 = 0,n nt (i,0) = yval (i) end k = 1,n top = k bot = 0 = 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 print*, 'column number', k j = 0,n print*, nt (j,k) end end 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) = n, 1, -1 evalnewton = evalnewton * (x - xnodes (i-1)) + nt (i-1, i-1) end return end
to honest added "yp" everywhere, had "yval" , hoped mystery solver magic. have not figured out yet add program include derivatives in calculations. nice if explain me next steps take.
edit: added minor changes computent thought solve issue: = 0,n nt (i*2,0) = yval (i) nt (i*2+1,0) = yval (i) end do
do k = 1,n top = k bot = 0 = k,n nt (i,k) = (nt (i,k-1) - nt (i-1,k-1))/(xnodes(top) - xnodes(bot)) if (nt(i,k) == 0) nt(i,k) = yp(i/2) end if top = top + 2 bot = bot + 2 end print*, 'column number', k j = 0,n print*, nt (j,k) end
now first number 2.420...e^-322, , number messing rest of calculations. ideas how rid of it?
i'm pretty figured out small issue. have answer in case else needs it.
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) = 0,n x = xnodes(i) print*, xnodes(i), yval(i), yp(i), evalnewton (n, xnodes, yt, x) end open (unit = 4, file = 'z', status = 'replace') = 0,100 x = dble(i) * 1.0d-1 write (4,*) x, evalnewton (n, xnodes, yt, x) end 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 = 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 k = 1,n = 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) yt(i,k) = yp(i/2) end if end print*, 'column number', k j = 0,n print*, yt (j,k) end end 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) = n, 1, -1 evalnewton = evalnewton * (x - xnodes (i-1)) + yt (i-1, i-1) end return end
the small issue error message when run program , file not created
error message: a.out: malloc.c:2372: sysmalloc: assertion `(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)' failed.
program received signal sigabrt: process abort signal.
backtrace error:
Comments
Post a Comment