Can Legendre polynomials be evaluated using a recurrence relation in Fotran 90?

  • Thread starter Thread starter quantumfireball
  • Start date Start date
  • Tags Tags
    Legendre
Click For Summary
SUMMARY

Legendre polynomials can be effectively evaluated using a recurrence relation in Fortran 90. The recurrence relation is defined as p(n,x) = p(n-1,x) * x - p(n-2,x), with initial conditions P(0) = 1 and P(1) = x. Two implementations are provided: one using an array to store intermediate values for N up to 100, and a more efficient version that eliminates the need for arrays, allowing for larger N values while minimizing round-off errors.

PREREQUISITES
  • Fortran 90 programming language
  • Understanding of Legendre polynomials
  • Recursion and iterative algorithms
  • Numerical methods for polynomial evaluation
NEXT STEPS
  • Implement Legendre polynomial evaluation using the recurrence relation in Fortran 90
  • Explore numerical stability and round-off error in polynomial computations
  • Learn about alternative methods for evaluating special functions in Fortran
  • Investigate performance optimization techniques for recursive algorithms
USEFUL FOR

Mathematicians, physicists, and software developers working with numerical methods, particularly those involved in computational simulations and mathematical modeling using Fortran.

quantumfireball
Messages
90
Reaction score
0
Is it possible to evaluate a legendre polynomial p(n,x) using the recurrence relation

p(n,x)=p(n-1,x)*x-p(n-2,x) in fotran 90


{there are some other terms which i left out for brevity]
 
Technology news on Phys.org
Here is an example in F77 for N up to 100.
Recursion is not necessary when the initial conditions are known, i.e. P(0) and P(1).
P(2) was defined to avoid going back to P(0) which is not permitted in F77.

Code:
      FUNCTION FLEGENDRE(N,X)
      REAL*8 P(100)
      REAL*8 FI
      INTEGER I
C     CHECK FOR VALID VALUES OF N AND X HERE BEFORE PROCEEDING
C
C     PROCEEDING WITH CALCULATIONS
      IF (N.EQ.0) THEN
        FLEGENDRE=1
      ELSEIF (N.EQ.1) THEN
        FLEGENDRE=X
      ELSEIF (N.EQ.2) THEN
        FLEGENDRE=(3*X*X-1)/2.0
      ELSE
C       SYNTHETIC CALCULATIONS 
        P(1)=X
        P(2)=(3*X*X-1)/2.0
        DO 20 I=3,N
        FI=I
        P(I)=((I+I-1)*X*P(I-1)-(I-1)*P(I-2))/FI
   20   CONTINUE
        FLEGENDRE=P(N)
      ENDIF
      END
C   
C     TEST PROGRAM
C
      REAL*8 OUT
      DO 30 I=1,10
      OUT=FLEGENDRE(I,5.425)
      WRITE(6,999)I,OUT
   30 CONTINUE
  999 FORMAT(I3,F20.2)
      STOP
      END
 
Here a computationally more efficient version that obviates the use of arrays and hence no limit on the size of N except for round-off errors.

Code:
      FUNCTION FLEGENDRE(N,X)
      REAL*8 PI,PIM1,PIM2
      REAL*8 FI
      INTEGER I
C     CHECK FOR VALID VALUES OF N AND X HERE BEFORE PROCEEDING
C
C     PROCEEDING WITH CALCULATIONS
      IF (N.EQ.0) THEN
        FLEGENDRE=1
      ELSEIF (N.EQ.1) THEN
        FLEGENDRE=X
      ELSE
C       SYNTHETIC CALCULATIONS 
        PIM1=1
        PI=X
        DO 20 I=2,N
        FI=I
        PIM2=PIM1
        PIM1=PI
        PI=((I+I-1)*X*PIM1-(I-1)*PIM2)/FI
   20   CONTINUE
        FLEGENDRE=PI
      ENDIF
      END
C   
C     TEST PROGRAM
C
      REAL*8 OUT
      DO 30 I=1,10
      OUT=FLEGENDRE(I,5.425)
      WRITE(6,999)I,OUT
   30 CONTINUE
  999 FORMAT(I3,F20.2)
      STOP
      END
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
948
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
6K