# Matlab symbolic differentiation of Legendre polynomials

Consider $f(x)= \sum\limits_{n=0}^N a_n p_n (x)$, where $p_n$ are the Legendre polynomials. If one wants to differentiate $f'$ symbolically, i.e. to compute $$f'(x) = \sum\limits_{n=0}^{N-1} b_n p_n (x)= \sum\limits_{n=0}^N a_n p_n '(x)$$, he should be able to use the fact that the $p_n'(x)$ are well known for the Legendre polynomials.

My Question: Given a vector a_n that represents the coefficients $a_n$, how can I get, in Matlab, the coefficients $b_n$ of the above derivative? Is there a built in function for that?

My Second question Can I compute symbolically the following coefficients: $$f'(x) =\sum\limits_{n=0}^N c_n x^n$$

• Possibly related : in.mathworks.com/matlabcentral/answers/… Jan 23 '17 at 12:50
• Why would you want to go to the monomial base? Calculation of the $c_n$ will give rise to numerical problems and the evaluation of $f'(x)$ using $\sum_{n=0}^{N}c_{n}x^{n}$ (using Horner) is much worse conditioned than the evaluation of $\sum_{n=0}^{N} b_{n} P_{n}(x)$ using the Clenshaw-Smith algorithm. Jan 23 '17 at 18:31
• @GertVdE What is the Clenshaw-Smith algorithm? Jan 24 '17 at 7:08

## 1 Answer

Philips in 1988 proved the following relationship:

If $f(x)$ is an infinitely differentiable function defined on the interval $[-1,+1]$ and its Legendre expansion is given by $f(x) = \sum_{n=0}^{\infty}a_{n}P_{n}(x)$, then the Legendre coefficients $a_{n}^{(q)}$ of the $q$-th derivative of $f(x)$ are given by $$a_{n}^{(q)} = \frac{(2n+1)}{2^{q-2}(q-1)!} \sum_{k=1}^{\infty} \frac{(k+q-2)!}{(k-1)!}\frac{(2n+2k+2q-3)!(n+k)!}{(2n+2k)!(n+q+k-2)!} a_{n+2k+q-2}$$.

Simplifying this for $q=1$, one finds

$$a_{n}^{(1)} = (2n+1) \sum_{k=1}^{\infty} a_{n+2k-1}$$

Please note that

1. You need a large number of coefficients (in theory an infinite amount) to calculate the coefficients of the derivative. This means that the order at which you cut-off the series will have an impact on the accuracy. If the original approximation is cut-off at order $M$, then clearly all $a_n$ for $n>M$ are identical to zero and the summation is finite.

2. Care must be taken in the summation if the $a_n$ have different signs. You'll quickly loose correct significant digits in IEEE double precision arithmetic.

3. The evaluation of $\sum_{n=0}^{N}a_{n}^{(q)}P_{n}(x)$ is best done using the Clenshaw-Smith algorithm (see Smith's paper). This is much more stable than converting to the monomial base and applying Horner's rule.

Reference

T.N. Philips, "On the Legendre Coefficients of a General-Order Derivative of an Infinitely Differential Function", IMA Journal of Numerical Analysis, 1988, Volume 8, p. 455--459

• Great, thanks. However, there must be a simpler formula. $f$ is an $N$ degree polynomial , and $f'$ is of degree $N-1$. All the polynomials of this degree are spanned by $p_0,\ldots,p_{N-1}$. Jan 23 '17 at 14:58
• If $f$ is of finite degree $N$, then the coefficients $a_j$ will simply be zero for $j>N$. That means that also the infinite sum for the coefficients $a_n^{(1)}$ really is finite. Jan 23 '17 at 17:23