-
Notifications
You must be signed in to change notification settings - Fork 2
/
horner_polynomial.f
36 lines (33 loc) · 988 Bytes
/
horner_polynomial.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
PROGRAM MAIN
INTEGER I,N,NMAX
PARAMETER(NMAX=10)
REAL COEF(0:NMAX),HORNER,X
10 CONTINUE
WRITE(*,*)'Enter the degree of the polynomial'
READ(*,*)N
IF (N .GT. NMAX) THEN
WRITE(*,*)'Degree too large. Choose smaller value.'
GO TO 10
END IF
WRITE(*,*)'Enter the coefficients in ascending order'
DO 20, I = 0,N
WRITE(*,*)'Enter the value for coefficient ',I
READ(*,*)COEF(I)
20 CONTINUE
WRITE(*,*)'Enter the value of X'
READ(*,*)X
WRITE(*,*)'The value of the polynomial at ',X,' is ',
& HORNER(COEF,N,X)
STOP 'End of program'
END
REAL FUNCTION HORNER(A,N,X)
C This function returns the value of the polynomial
C y = a_0 + a_1 x + a_2 x^2 + … + a_n x^n
C using Horner's method.
INTEGER I,N
REAL A(0:N),X
HORNER = A(N)
DO 10 I = N-1,0,-1
HORNER = A(I) + HORNER*X
10 CONTINUE
END