Search code examples
matrixoctavediscretization

Octave Matrix of discretized Legendre polynomials


I need to get N x columns(L) matrix of legendre polynomials evaluated over L for arbitrary N.

Is there a better way of computing the matrix than just explicitly evaluating the polynomial vector for each row? The code snippet for this approach (N = 4) is here:

L = linspace(-1,1,800);

# How to do this in a better way?
G = [legendre_Pl(0,L); legendre_Pl(1,L); legendre_Pl(2,L); legendre_Pl(3,L)];

Thanks, Vojta


Solution

  • Create an anonymous function. Documentation at http://www.gnu.org/software/octave/doc/interpreter/Anonymous-Functions.html

    f = @(x) legendre_Pl(x,L);
    

    Then use arrayfun to apply the function, f to an array [1:N] Documentation at http://www.gnu.org/software/octave/doc/interpreter/Function-Application.html

    CellArray = arrayfun(f, [1:N], "UniformOutput", false);
    

    That gives you a cell array. If you want the answer in a matrix, use cell2mat

    G = cell2mat(CellArray);