vb_legendre_grad - Evaluate the first N Legendre polynomials [P,dP] = vb_legendre_grad(N,x) Evaluates the first N Legendre polynomials and the first derivatives at the vector x. Input: N (scalar) : Polynomial order to evaluate x (M x 1) : Input vector of values for evaluation Outputs: P (N x M) : Legendre Polynomial = [ P1(x1) P1(x2) ... P1(xM); P2(x1) P2(x2) ... P2(xM); . . PN(x1) PN(x2) ... PN(xM)] dP = dP/dx : Derivative of P (N x M) ---------------------- --------- Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [P,dP] = vb_legendre_grad(N,x) 0002 % vb_legendre_grad - Evaluate the first N Legendre polynomials 0003 % [P,dP] = vb_legendre_grad(N,x) 0004 % 0005 % Evaluates the first N Legendre polynomials and 0006 % the first derivatives at the vector x. 0007 % 0008 % Input: 0009 % N (scalar) : Polynomial order to evaluate 0010 % x (M x 1) : Input vector of values for evaluation 0011 % 0012 % Outputs: 0013 % P (N x M) : Legendre Polynomial 0014 % = [ P1(x1) P1(x2) ... P1(xM); 0015 % P2(x1) P2(x2) ... P2(xM); 0016 % . 0017 % . 0018 % PN(x1) PN(x2) ... PN(xM)] 0019 % 0020 % dP = dP/dx : Derivative of P (N x M) 0021 % ---------------------- --------- 0022 % 0023 % Copyright (C) 2011, ATR All Rights Reserved. 0024 % License : New BSD License(see VBMEG_LICENSE.txt) 0025 0026 if size(x,1)~=1, x=x'; end; 0027 0028 M = size(x,2); % # of evaluation points 0029 0030 P = zeros(N,M); 0031 dP = zeros(N,M); 0032 0033 P(1,:) = x; % initial of P 0034 P(2,:) = 1.5*x.^2-0.5; 0035 dP(1,:) = ones(1,M); % initial derivative 0036 0037 % recursively compute P,dP 0038 for n=3:N, 0039 P(n,:) = ( (2*n-1).*x.*P(n-1,:) - (n-1)*P(n-2,:) )/n; 0040 end 0041 0042 for n=2:N 0043 dP(n,:) = x.*dP(n-1,:) + n.*P(n-1,:); 0044 end