- #1
fluidistic
Gold Member
- 3,935
- 263
I want to interpolate a set of points (x(i),y(i)) for i=0, n; using Newton's polynomial of degree n.
I've Kincaid's book of numerical analysis. He gives an algorithm that doesn't make entirely sense to me.
He says for
Basically I'm stuck in Fortran for the di <- f(xi) part.
I'll show you my whole program. What I did doesn't make sense to gfortran (the compiler), but I do not understand why!
Here are the errors I'm getting:
Some more details about the program: I have to write the name of the file I read the points from, say "kincaid.dat" and the degree of the polynomial. For example if I have 10 points to interpolate, I'd enter n=9.
I want my program to write in a file (polynomial.dat) the values for p(x_m). If I enter a small h, I'll get lots of values for p(x_m) so that if I graph p(x_m) in gnuplot I can have a good idea of the theoretical graph of p(x), the theoretical interpolating polynomial of the data.
Any help is greatly appreciated. I feel like I'm turning crazy passing hours in a row on this program and now I'm stuck.
I've Kincaid's book of numerical analysis. He gives an algorithm that doesn't make entirely sense to me.
He says for
Code:
i=0, n do
di <- f(xi)
end
for j=1, n do
etc.
I'll show you my whole program. What I did doesn't make sense to gfortran (the compiler), but I do not understand why!
Code:
program div
implicit none
character(10000000) :: datos
real, dimension(:), allocatable :: x,y,d,p
integer::n ,i
real :: h, x_m
write(*,*)"Enter the degree of the polynomial n and a step-size h"
read(*,*)n,h
write(*,*)"Enter the name of the file to read the data from"
read(*,'(x)')datos
open(unit=1,file="datos", status="old")
allocate(x(0:n),y(0:n), d(1:n), p(1:n))
do i=0, n
read(1,*)x(i),y(i)
end do
close(1)
call Newton(x_m,h,x,d,y)
contains
subroutine Newton(x_m,h,x,d,y)
implicit none
integer:: i, j
real, intent(inout)::x_m,h,d
real:: prod, p
real, dimension(:), intent(inout) :: x,y
open(unit=120, file="polynomial.dat")
do while (x_m <= x(n))
[b]do i=0,n
d(i)=y(i)
end do[/b]
do j=1, n
if (j>= i) then
[b]d(i)=(d(i)-d(i-1))/(x(i)-x(i-j))[/b]
end if
end do
prod=x_m-x(0)
do i=1, n
do j=0, i-1
prod=prod*x_m-x(j)
end do
p=d(i)*prod
write(120,*)p
end do
x_m=x_m+h
end do
close(120)
end subroutine
end program
Here are the errors I'm getting:
Code:
Newton.f90:34.2:
d(i)=y(i)
1
Error: Unclassifiable statement at (1)
Newton.f90:38.12:
d(i)=(d(i)-d(i-1))/(x(i)-x(i-j))
1
Error: Unclassifiable statement at (1)
Newton.f90:24.27:
subroutine Newton(x_m,h,x,d,y)
1
Error: PROCEDURE attribute conflicts with INTENT attribute in 'd' at (1)
Newton.f90:41.11:
prod=x_m-x(0)
1
Warning: Array reference at (1) is out of bounds (0 < 1) in dimension 1
Newton.f90:19.20:
call Newton(x_m,h,x,d,y)
1
Error: Rank mismatch in argument 'd' at (1) (0 and 1)
I want my program to write in a file (polynomial.dat) the values for p(x_m). If I enter a small h, I'll get lots of values for p(x_m) so that if I graph p(x_m) in gnuplot I can have a good idea of the theoretical graph of p(x), the theoretical interpolating polynomial of the data.
Any help is greatly appreciated. I feel like I'm turning crazy passing hours in a row on this program and now I'm stuck.