fluidistic

Gold Member

- 3,626

- 97

## Main Question or Discussion Point

It's about Numerical Analysis and especially Newton's form of interpolation. That is, the program must ask us to input the nodes where the function (cos(x) in this case) is equal to it's interpolating polynomial.

From that [tex]P(x)=a_0+a_1(x-x_0)+...+a_n(x-x_o)...(x-x_{n-1})[/tex].

And then it must output the value of the interpolating polynomial evaluated m times between [tex]x_0[/tex] and [tex]x_n[/tex]. m is a value written in the program (that you can change), of about 100 for example. It means the output of the program will give 100 values of P(x) in the interval [tex]x_0, x_n[/tex]. m divided the interval [tex]x_0, x_n[/tex] properly (all the subintervals have same length).

So I worked out my program... But when I graph the output via Gnuplot.... It doesn't look like the cos(x) function at all! I would like to see where is my error(s).

If someone could do this, I would be very glad. If you have some doubts, just ask.

Here comes the program... :

Program Interp

implicit none

Integer, Parameter :: n=5

Real,Dimension(0:n-1) :: x,y

Integer :: K,J,m

Real :: z,p,dx,s

m=100

do k=0,n-1

Write(*,*)'Enter x sub',K

Read(*,*)x(K)

y(K)=f(x(K))

end do

dx=(x(n-1)-x(0))/m

z=x(0)

DO WHILE (z<=x(n-1))

CALL NEWTON(z,p,x,y,s)

WRITE(*,*)z,s

z=z+dx

end do

Contains

Subroutine Newton(z,p,x,y,s)

implicit none

Real, Dimension(0:n-1),Intent(in) ::x,y

Real, Intent(out) :: S

Real, Intent(in) :: z

Real, Dimension(0:n-1) :: a

Integer :: K,J,L

Real :: D,P

a(0)=y(0)

a(1)=(y(1)-a(0))/(x(1)-x(0))

Do K=2,n-1,1

D=1

Do J=0,K-1

D=D*(x(k)-x(J))

end do

P=A(k-1)

Do L=K-1,0,-1

P=A(L)+P*(x(K)-x(L))

end do

A(K)=(Y(K)-P)/D

end do

S=A(n-1)

DO K=n-1,0,-1

S=A(K)+S*(z-x(K))

end do

end subroutine

Real Function f(x)

Real, intent(in) :: x

f=cos(x)

end function

end program

From that [tex]P(x)=a_0+a_1(x-x_0)+...+a_n(x-x_o)...(x-x_{n-1})[/tex].

And then it must output the value of the interpolating polynomial evaluated m times between [tex]x_0[/tex] and [tex]x_n[/tex]. m is a value written in the program (that you can change), of about 100 for example. It means the output of the program will give 100 values of P(x) in the interval [tex]x_0, x_n[/tex]. m divided the interval [tex]x_0, x_n[/tex] properly (all the subintervals have same length).

So I worked out my program... But when I graph the output via Gnuplot.... It doesn't look like the cos(x) function at all! I would like to see where is my error(s).

If someone could do this, I would be very glad. If you have some doubts, just ask.

Here comes the program... :

Program Interp

implicit none

Integer, Parameter :: n=5

Real,Dimension(0:n-1) :: x,y

Integer :: K,J,m

Real :: z,p,dx,s

m=100

do k=0,n-1

Write(*,*)'Enter x sub',K

Read(*,*)x(K)

y(K)=f(x(K))

end do

dx=(x(n-1)-x(0))/m

z=x(0)

DO WHILE (z<=x(n-1))

CALL NEWTON(z,p,x,y,s)

WRITE(*,*)z,s

z=z+dx

end do

Contains

Subroutine Newton(z,p,x,y,s)

implicit none

Real, Dimension(0:n-1),Intent(in) ::x,y

Real, Intent(out) :: S

Real, Intent(in) :: z

Real, Dimension(0:n-1) :: a

Integer :: K,J,L

Real :: D,P

a(0)=y(0)

a(1)=(y(1)-a(0))/(x(1)-x(0))

Do K=2,n-1,1

D=1

Do J=0,K-1

D=D*(x(k)-x(J))

end do

P=A(k-1)

Do L=K-1,0,-1

P=A(L)+P*(x(K)-x(L))

end do

A(K)=(Y(K)-P)/D

end do

S=A(n-1)

DO K=n-1,0,-1

S=A(K)+S*(z-x(K))

end do

end subroutine

Real Function f(x)

Real, intent(in) :: x

f=cos(x)

end function

end program

Last edited: