P3
P3
implicit none
integer :: i, j, row
real :: p, x(1:5), f(1:5), d(1:4, 1:5)
real :: fun
do i=1, 4
d(1, i) = (f(i+1) - f(i)) / (x(i+1) - x(i))
end do
do i = 2, 4
do j = 1, 4-i+1
d(i, j) = ( d(i-1, j+1) - d(i-1, j) ) / (x(i+j) - x(j))
end do
end do
12 format(6x, "x", 20x, "f(x)", 26x, "D1", 30x, "D2", 28x, "D3", 30x, "D4")
13 format(160('_'))
write(*, 12)
write(*, 13)
do i = 1, 2*5 - 1
row = i;
if(i > 5) then
row = 2*5-i;
end if
do j = 1, row
if(mod(row, 2) .ne. 0) then
if(j == 1) then
write(*, '( 2(f10.5, a12) )', advance="no") x( (i+1) / 2 ), "
", f( (i+1) / 2 )
else if(mod(j, 2) == 0) then
write(*, '(a31)', advance="no") " "
else
write(*, '(f31.5)', advance="no") d(j-1, (i-j+2)/2)
end if
else if(mod(row, 2) == 0) then
if(mod(j, 2) .ne. 0) then
write(*, '(a31)', advance="no") " "
else
write(*, '(f31.5)', advance="no") d(j-1, (i-j+2)/2)
end if
end if
end do
write(*,*)
end do
write(*,'(/)')
write(*, *) "The value of f(0.8) is ", fun(0.8, x, f, d)
end program
function fun(v, x, f, d)
implicit none
integer :: i
real :: x(1:5), f(1:5), d(1:4, 1:5)
real :: v, res, fun
res = 1
fun = f(1)
do i=2, 5
res = res * (v - x(i-1))
fun = fun + res*d(i-1, 1)
end do
end function