Tuesday, July 26, 2016

Legendre polynomial (Legendre function of the first kind)



All the elements presented are not warranted to be correct or free from defects. 
Please report any errors found to  afstblogs@gmail.com

See also Legendre polynomials with Xnumbers add-in 

Excel formulas and VBA functions




D3: =IF(ISEVEN(B3),(-1)^(B3/2)*FACTDOUBLE(B3-1)/FACTDOUBLE(B3),0)
D4: =xlLegendrePn(B3,B4)




Leave cell I4 blank
Ranges I5:K5 and N5:P5 are merged

I6: =IF(H6=0,1,IF(H6=1,$I$2,(2*H6-1)/H6*$I$2*I5-(H6-1)/H6*I4))
J6: =xlLegendrePn(H6,$I$2)
K6: =Poly_Legendre($I$2,H6)

M6: =H6
N6: =(-1)^M6*I6
O6: =xlLegendrePn(M6,-$I$2)
P6: =Poly_Legendre(-$I$2,M6)



MyExcelRoutines

This function returns only the value of the polynomial. The computation of the first derivative will be the object of a separate function.

Function xlLegendrePn(n, x)

    Dim i
    Dim P0, P1, Pn, Pn_1, Pn_2
   
    P0 = 1: P1 = x
    Pn_1 = P1: Pn_2 = P0
   
    If (n <> Int(n) Or n < 0) Then
        xlLegendrePn = "** n **"
    ElseIf n = 0 Then
        xlLegendrePn = 1
    ElseIf n = 1 Then
        xlLegendrePn = x
    ElseIf x = 1 Then
        xlLegendrePn = 1
    ElseIf x = -1 Then
        xlLegendrePn = (-1) ^ n
    Else
        For i = 2 To n
            Pn = (2 * i - 1) / i * x * Pn_1 _
                    - (i - 1) / i * Pn_2
            Pn_2 = Pn_1
            Pn_1 = Pn
        Next i
         xlLegendrePn = Pn
    End If

End Function


Xnumbers add-in

Poly_Legendre is an array function that returns both the values of the polynomial and its first derivative calculated by the subroutine EvalLegendre.


Function Poly_Legendre(x, Optional n)
Dim Pol#, Dpol#, k&, z#
If IsMissing(n) Then k = 1 Else k = n
z = x
Call EvalLegendre(k, z, Pol, Dpol)
Poly_Legendre = PasteVector_(Array(Pol, Dpol))
End Function
Sub EvalLegendre(n&, x#, Pol#, Dpol#)
' Rutina para calcular el polinomio ortonormal de Legendre de orden n y su derivada en x
' Los polinomios de Legendre son un caso especial de los de Jacobi con a = b = 0
' Pol valor del polinomio en x; DPol valor de la derivada del polinomio en x
' Bibliografia: Abramowitz M et al.; "Handbook of Mathematical Functions...",Dover
'               Press et al.; "Numerical recipies in fotran77", Cambridge U Press
'mod. 12.4.04 VL
    Dim k&, p#(0 To 2), dp#(0 To 2)
   
    If n = 0 Then
        Pol = 1
        Dpol = 0
    ElseIf n = 1 Then
        Pol = x
        Dpol = 1
    Else
        p(0) = 1
        p(1) = x
        If Abs(x - 1) < 0.1 Or Abs(x + 1) < 0.1 Then
            dp(0) = 0
            dp(1) = 1
            For k = 1 To n - 1
                p(2) = ((2 * k + 1) * x * p(1) - k * p(0)) / (k + 1)             'Polinomio de orden k+1 en x
                dp(2) = ((2 * k + 1) * (p(1) + x * dp(1)) - k * dp(0)) / (k + 1) 'Derivata del polinomio di ' ordine k+1 in x .VL
                p(0) = p(1)
                p(1) = p(2)
                dp(0) = dp(1)
                dp(1) = dp(2)
            Next
            Pol = p(2)
            Dpol = dp(2)
        Else
            For k = 1 To n - 1
                p(2) = ((2 * k + 1) * x * p(1) - k * p(0)) / (k + 1)                    ' Polinomio de orden k+1 ' ' en x
                p(0) = p(1)                                                             ' (***)
                p(1) = p(2)
            Next
            Pol = p(2)
            Dpol = n * (x * p(2) - p(0)) / (x ^ 2 - 1)      ' Derivada del polinomio de orden k+1 en x
        End If
    End If
End Sub

From the Tutorial -1:









Matlab

Values of Pn(x)
Polynomials