3

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

  • First, how do you define the "best" and second are any inaccuracies acceptable? You can for example simply compute L in every second point and interpolate. – Moonwalker Aug 02 '13 at 14:50
  • I only need to find the rows of legendre_Pl(n, L) over L for n from 1 to N (N is the number of rows of resulting matrix) and keep them as a matrix of Nxcols(L). Using n = 1:N as an argument of legendre_Pl does not work here. By "best" I mean making N variable, so I do not have to type out all legendre_Pl manually. Also, preferably not using for loop. Innacuracies are not topic of this question – vojta havlíček Aug 03 '13 at 15:45

1 Answers1

2

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);
Charity Leschinski
  • 2,886
  • 2
  • 23
  • 40