R: Legendre Functions (Matlab Style)

legendre {pracma} | R Documentation

## Legendre Functions (Matlab Style)

### Description

Calculate the values of (associated) Legendre functions.

### Usage

    legendre(n, x)

### Arguments

`n` | degree of the Legendre polynomial involved.
`x` | real points to evaluate Legendre's functions at.

### Details

`legendre(n,x)` computes the associated Legendre functions of degree `n` and order `m=0,1,...,n`, evaluated for each element of `x` where `x` must contain real values in `[-1,1]`.

If `x` is a vector, then `L=legendre(n,x)` is an `(n+1)`-by-`N` matrix, where `N=length(x)`. Each element `L[m+1,i]` corresponds to the associated Legendre function of degree `legendre(n,x)` and order `m` evaluated at `x[i]`.

Note that the first row of `L` is the Legendre polynomial evaluated at `x`.

### Value

Returns a matrix of size `(n+1)`-by-`N` where `N=length(x)`.

### Note

Legendre functions are solutions to Legendre's differential equation (it occurs when solving Laplace's equation in spherical coordinates).

### See Also

`chebPoly`

### Examples

    x <- c(0.0, 0.1, 0.2)
    legendre(2, x)
    #      [,1]       [,2]       [,3]
    # [1,] -0.5 -0.4850000 -0.4400000
    # [2,]  0.0 -0.2984962 -0.5878775
    # [3,]  3.0  2.9700000  2.8800000

    ## Not run:
    x <- seq(0, 1, len = 50)
    L <- legendre(2, x)
    plot(x, L[1, ], type = "l", col = 1, ylim = c(-2, 3), ylab = "y",
                    main = "Legendre Functions of degree 2")
    lines(x, L[2, ], col = 2)
    lines(x, L[3, ], col = 3)
    grid()
    ## End(Not run)
    ## Generate Legendre's Polynomial as function
    # legendre_P <- function(n, x) {
    #     L <- legendre(n, x)
    #     return(L[1, ])
    # }

[Package pracma version 2.0.4 Index]