Taylor Series Convergence¶

This notebook explores Taylor series through Maxima's symbolic-numeric bridge: we derive series expansions symbolically with taylor(), then evaluate and plot them numerically to study convergence, accuracy, and the effect of truncation order.

Key Maxima functions: taylor, ratdisrep, diff, subst, float.

In [97]:
load("numerics")$
load("ax-plots")$

Basic Taylor Expansion¶

Start with $f(x) = e^x$ and compute its 5th-order Taylor polynomial around $x = 0$. The symbolic CAS gives us the exact coefficients; then we plot the polynomial alongside the true function.

In [98]:
/* Symbolic Taylor expansion of exp(x) to order 5 */
f : exp(x)$
T5 : taylor(f, x, 0, 5);
T5_poly : ratdisrep(T5);
1+x+x^2/2+x^3/6+x^4/24+x^5/120
Out[98]:
In [99]:
/* Compare exp(x) and its 5th-order Taylor polynomial */
ax_draw2d(
  color=blue, name="exp(x)",
  explicit(exp(x), x, -4, 4),
  color=red, dash="dash", name="Taylor O(5)",
  explicit(T5_poly, x, -4, 4),
  title="exp(x) vs Taylor Polynomial (order 5)",
  yrange=[-1, 20], grid=true
)$
No description has been provided for this image

Successive Approximations of sin(x)¶

Taylor polynomials of increasing order converge to the true function. For $\sin(x)$ (an odd function), only odd-order terms appear, so we use orders 1, 3, 5, 7, 9.

Maxima computes each expansion symbolically, and we overlay all of them on a single plot.

In [100]:
/* Compute Taylor polynomials of sin(x) at several orders */
orders : [1, 3, 5, 7, 9]$
polys : makelist(
  ratdisrep(taylor(sin(x), x, 0, orders[i])),
  i, 1, length(orders)
)$

/* Display each polynomial */
for i: 1 thru length(orders) do
  print(sconcat("Order ", orders[i], ":"), polys[i])$
Order 1: x
Order 3: x-x^3/6
Order 5: x^5/120-x^3/6+x
Order 7: -(x^7/5040)+x^5/120-x^3/6+x
Order 9: x^9/362880-x^7/5040+x^5/120-x^3/6+x
In [101]:
/* Plot sin(x) and all Taylor approximations */
colors : ["#e41a1c", "#ff7f00", "#4daf4a", "#377eb8", "#984ea3"]$

ax_draw2d(
  color=black, line_width=3, name="sin(x)",
  explicit(sin(x), x, -2*%pi, 2*%pi),
  color=colors[1], name="Order 1",
  explicit(polys[1], x, -2*%pi, 2*%pi),
  color=colors[2], name="Order 3",
  explicit(polys[2], x, -2*%pi, 2*%pi),
  color=colors[3], name="Order 5",
  explicit(polys[3], x, -2*%pi, 2*%pi),
  color=colors[4], name="Order 7",
  explicit(polys[4], x, -2*%pi, 2*%pi),
  color=colors[5], name="Order 9",
  explicit(polys[5], x, -2*%pi, 2*%pi),
  title="Successive Taylor Approximations of sin(x)",
  yrange=[-2, 2], grid=true
)$
No description has been provided for this image

Error Analysis [S+N]¶

Define the approximation error $E_n(x) = |e^x - T_n(x)|$ for the exponential function. We compute $T_n$ symbolically for several orders, then plot $\log_{10}(E_n)$ to see how quickly each approximation improves.

Near $x=0$ the error drops dramatically with increasing order, following the theoretical $O(x^{n+1}/(n+1)!)$ bound.

In [102]:
/* Symbolic Taylor polynomials for exp(x) at orders 2, 4, 6, 8 */
err_orders : [2, 4, 6, 8]$
exp_polys : makelist(
  ratdisrep(taylor(exp(x), x, 0, err_orders[i])),
  i, 1, length(err_orders)
)$

/* Error expressions: log10(|exp(x) - T_n(x)| + 1e-16) to avoid log(0) */
err_exprs : makelist(
  log(abs(exp(x) - exp_polys[i]) + 1e-16) / log(10),
  i, 1, length(err_orders)
)$
In [103]:
/* Plot log10(error) for each order */
ax_draw2d(
  color="#e41a1c", name="Order 2",
  explicit(err_exprs[1], x, -3, 3),
  color="#ff7f00", name="Order 4",
  explicit(err_exprs[2], x, -3, 3),
  color="#4daf4a", name="Order 6",
  explicit(err_exprs[3], x, -3, 3),
  color="#377eb8", name="Order 8",
  explicit(err_exprs[4], x, -3, 3),
  title="Approximation Error: log10(|exp(x) - T_n(x)|)",
  ylabel="log10(error)", xlabel="x",
  grid=true
)$
No description has been provided for this image

Radius of Convergence [S+N]¶

Not all Taylor series converge everywhere. The function $f(x) = \frac{1}{1+x^2}$ is analytic on all of $\mathbb{R}$, yet its Taylor series around $x=0$ has radius of convergence $R = 1$.

This happens because $f$ has poles at $x = \pm i$ in the complex plane, and the radius of convergence equals the distance to the nearest singularity. Maxima derives the series symbolically; the plot reveals the divergence outside $|x| < 1$.

In [104]:
/* Taylor expansion of 1/(1+x^2) — converges only for |x| < 1 */
g : 1/(1 + x^2)$
T10 : ratdisrep(taylor(g, x, 0, 10))$
T20 : ratdisrep(taylor(g, x, 0, 20))$

print("T_10 =", T10)$
print("T_20 =", T20)$
T_10 = -x^10+x^8-x^6+x^4-x^2+1
T_20 = x^20-x^18+x^16-x^14+x^12-x^10+x^8-x^6+x^4-x^2+1
In [105]:
/* Plot: the Taylor series diverges wildly outside |x| < 1 */
ax_draw2d(
  color=black, line_width=3, name="1/(1+x^2)",
  explicit(g, x, -2, 2),
  color=red, name="Taylor order 10",
  explicit(T10, x, -2, 2),
  color=blue, name="Taylor order 20",
  explicit(T20, x, -2, 2),
  title="Radius of Convergence: 1/(1+x^2)",
  yrange=[-2, 3], grid=true
)$
No description has been provided for this image

The Taylor polynomials match perfectly inside $|x| < 1$ but oscillate violently beyond the radius of convergence. Higher order makes the divergence worse, not better — a striking contrast to the entire-function case above.

Multivariate Taylor [S+N]¶

Maxima's taylor also handles multivariate expansions. Here we expand $\sin(x)\cos(y)$ around the origin to 4th order in both variables, then compare the 3D surfaces.

In [106]:
/* Multivariate Taylor expansion */
h : sin(x)*cos(y)$
T_xy : taylor(h, [x, y], [0, 0], 4);
T_xy_poly : ratdisrep(T_xy);
x-(x^3+3*y^2*x)/6
Out[106]:
In [107]:
/* 3D surface: actual function */
ax_draw3d(
  explicit(sin(x)*cos(y), x, -%pi, %pi, y, -%pi, %pi),
  title="sin(x) cos(y)", colorscale="Viridis"
)$
No description has been provided for this image
In [108]:
/* 3D surface: Taylor approximation */
ax_draw3d(
  explicit(T_xy_poly, x, -%pi, %pi, y, -%pi, %pi),
  title="Taylor Approximation (order 4)", colorscale="Viridis"
)$
No description has been provided for this image