# Taylor Series

The nth-degree Taylor Polynomial for a function $$f$$ at a point $$x = x_0$$ is $$T_n(x) = \displaystyle \sum_{k=0}^{n} \frac{f^{(k)}(x_0)}{k!}(x-x_0)^k$$.

If $$f$$ has a power series representation, then the Taylor Series is the power series and you get $$f(x) = \displaystyle \sum_{k=0}^{\infty} \frac{f^{(k)}(x_0)}{k!}(x-x_0)^k$$.

A Maclaurin series is a special case of a Taylor series where $$x_0 = 0$$. In this case, the Maclaurin series becomes $$f(x) = \displaystyle \sum_{k=0}^{\infty} \frac{f^{(k)}(0)}{k!}x^k$$

## Maxima Example

Let's find 6th degree Taylor Polynomial for $$f(x) = \sin 2x$$ about the point $$x = \frac{\pi}{6}$$.

### Finding the Derivatives

f:sin(2*x)$makelist(diff(f,x,k),k,0,6)$
subst(x=%pi/6,%);

Here are the values returned by Maxima: $$\displaystyle \left [ \frac{\sqrt{3}}{2},1,-2\,\sqrt{3},-4,8\,\sqrt{3},16,-32\,\sqrt{3} \right ]$$

### Finding the Coefficients

Rather than just finding the numerators, we could go ahead and divide by $$k!$$ and find the entire coefficient. While we're at it, we will combine the subst() command with the diff() command. This is not necessary here, but for finding the terms in the partial sum, we only want the substitution to apply to the derivative portion, not everywhere there is an x.

The spacing below is not necessary, I just put it in there to make things easier to follow.

f:sin(2*x)$makelist( subst(x=%pi/6, diff(f,x,k) ) /k!,k,0,6); Here are the values returned by Maxima: $$\displaystyle \left [ \frac{\sqrt{3}}{2},1,-\sqrt{3},-\frac{2}{3},\frac{1}{\sqrt{3}},\frac{2}{15},-\frac{2}{5\,{3}^{3/2}} \right ]$$. Note that the $$5\,3^{3/2}$$ is Maxima's way of writing $$5 \cdot 3 \sqrt 3 = 15\sqrt 3$$. ### Finding the Taylor Polynomial To create the polynomial from the terms, we need to change it from a sequence into a sum. That means switching from makelist() to sum(). We also need to multiply by $$(x-x_0)^k$$, which in this case would be $$(x - \pi/6 )^k$$. f:sin(2*x)$
sum(subst(x=%pi/6,diff(f,x,k))/k!*(x-%pi/6)^k,k,0,6);

$$\displaystyle -\frac{2\,{\left( x-\frac{\pi }{6}\right) }^{6}}{5\,{3}^{3/2}}+\frac{2\,{\left( x-\frac{\pi }{6}\right) }^{5}}{15}+\frac{{\left( x-\frac{\pi }{6}\right) }^{4}}{\sqrt{3}}-\frac{2\,{\left( x-\frac{\pi }{6}\right) }^{3}}{3}-\sqrt{3}\,{\left( x-\frac{\pi }{6}\right) }^{2}+x-\frac{\pi }{6}+\frac{\sqrt{3}}{2}$$

## taylor() command

The above was good for understanding the process, but not useful if you need to do any real work with Taylor series. Thankfully, Maxima has a taylor() command built into it. The syntax of the command is "taylor(function, variable, point, degree)".

taylor(sin(2*x),x,%pi/6,6);

$$\displaystyle \frac{\sqrt{3}}{2}+\left( x-\frac{\pi }{6}\right) -\sqrt{3}\,{\left( x-\frac{\pi }{6}\right) }^{2}-\frac{2\,{\left( x-\frac{\pi }{6}\right) }^{3}}{3}+\frac{\sqrt{3}\,{\left( x-\frac{\pi }{6}\right) }^{4}}{3}+\frac{2\,{\left( x-\frac{\pi }{6}\right) }^{5}}{15}-\frac{2\,\sqrt{3}\,{\left( x-\frac{\pi }{6}\right) }^{6}}{45}+...$$

The output is also nicer looking than what we came up with.

You may notice that there is a $$+ \dots$$ at the end. This is supposed to be a 6th degree approximation, so why is there anything after that? Maxima has an internal way of representing Taylor series. In fact, it wasn't shown here, but there is a /T/ at the beginning of the output as well to indicate this. If you substitute into or graph the results of the taylor() command, you will see that it uses only the indicated terms.

f:sin(2*x)$g:taylor(sin(2*x),x,%pi/6,6); draw2d( dimensions = [800,500], xrange = [-1,2.5], yrange = [-1.35,1.15], xaxis = true, xaxis_type = solid, xaxis_width = 1, xaxis_color = orange, xtics = 1, yaxis = true, yaxis_type = solid, yaxis_width = 1, yaxis_color = orange, ytics = 1, font = Arial, font_size = 14, nticks = 250, color = blue, line_width = 3, line_type = solid, explicit(f,x,-1,2.5), label(["f",-0.75,-0.9]), color = red, line_width = 2, explicit(g,x,-1,2.5), label(["T_6",-0.7,-1.2]) )$

### Remainders

A good way to see how close your approximation is to the actual value is to graph the remainder $$R_6(x) = f(x) - T_6(x)$$.

However, if you graph the remainder, you don't get what you expect. You get 0.No matter how much you zoom in vertically, you still get that the two are equal to each other. To get around this, you can use the trunc() function, which will truncate or drop the remainder on the Taylor series approximation.

f:sin(2*x)$g:taylor(sin(2*x),x,%pi/6,6); draw2d( dimensions = [800,500], xrange = [-1,2.5], xaxis = true, xaxis_type = solid, xaxis_width = 1, xaxis_color = orange, xtics = 1, yaxis = true, yaxis_type = solid, yaxis_width = 1, yaxis_color = orange, font = Arial, font_size = 14, nticks = 250, color = blue, line_width = 3, line_type = solid, explicit(f-trunc(g),x,-1,2.5) )$