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

  • Thread starter quantumfireball
  • Start date
  • Tags
    Legendre
In summary, in Fortran 90 it is 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). This can be done efficiently without recursion by using initial conditions P(0) and P(1). An example in F77 for N up to 100 is provided, but a more computationally efficient version is also possible.
  • #1
quantumfireball
91
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
  • #2
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
 
  • #3
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
 

What is a Legendre polynomial?

A Legendre polynomial is a type of mathematical function used in various fields of science and engineering, particularly in physics and statistics. It is named after the French mathematician Adrien-Marie Legendre, who first studied them in the late 18th and early 19th centuries.

What is the significance of Legendre polynomials in Fortran 90?

In Fortran 90, Legendre polynomials are commonly used to approximate a wide range of functions. They are particularly useful for solving differential equations and performing numerical integration. Fortran 90 provides built-in functions for calculating Legendre polynomials, making it easier for scientists to use them in their research.

How are Legendre polynomials calculated in Fortran 90?

In Fortran 90, Legendre polynomials can be calculated using the function legendre(n,x), where n is the degree of the polynomial and x is the input value. This function returns the value of the Legendre polynomial of degree n at the point x. Fortran 90 also provides other functions for calculating the derivatives, integrals, and roots of Legendre polynomials.

What are the applications of Legendre polynomials in science?

Legendre polynomials have numerous applications in science, including solving differential equations, representing functions and data, and performing numerical integration. They are also used in fields such as quantum mechanics, electromagnetics, and signal processing. In addition, Legendre polynomials are often used in statistical analysis, particularly in the method of least squares.

Are there any limitations to using Legendre polynomials in Fortran 90?

While Legendre polynomials are a powerful tool in Fortran 90, they do have some limitations. For example, they may not be the most efficient method for approximating certain functions, and they may not provide accurate results for highly oscillatory functions. It is important for scientists to understand the strengths and weaknesses of Legendre polynomials and to choose the appropriate method for their specific research needs.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Calculus
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
953
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • General Math
Replies
1
Views
837
  • Introductory Physics Homework Help
Replies
2
Views
807
  • Programming and Computer Science
Replies
1
Views
4K
  • Advanced Physics Homework Help
Replies
16
Views
2K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
Back
Top