Infinite series

Roughly speaking, an infinite series is the sum of infinitely many numbers:

\[ \sum\limits_{n=0}^\infty a_n=a_0+a_1+a_2+a_3+\cdots \]

Strictly speaking, it is the limit of the sequence of partial sums:

\[ \sum\limits_{n=0}^\infty a_n=\lim_{k\to\infty}\sum\limits_{n=0}^ka_n. \]

This limit may or may not be finite, and there are many famous examples of this.

Geometric series

Don’t you love this:

\[ \frac{a}{1-r}=\sum\limits_{n=0}^\infty ar^n,\quad |r|<1. \]

The partial sums are also well-behaved:

\[ \sum\limits_{n=0}^k ar^n = \begin{cases} a(k+1) & r=1\\ a\left(\frac{1-r^{k+1}}{1-r}\right) & \text{else}. \end{cases} \]

\(p\)-series

If \(p>1\), this converges:

\[ \sum\limits_{n=1}^\infty\frac{1}{n^p}. \]

Here are concrete examples:

\[ \begin{aligned} \sum\limits_{n=1}^\infty\frac{1}{n}&=\infty &&\text{(harmonic series)}\\ \sum\limits_{n=1}^\infty\frac{1}{n^2}&=\frac{\pi^2}{6}\\ \sum\limits_{n=1}^\infty\frac{1}{n^3}&\approx 1.202057. \end{aligned} \]

Taylor series

If a function \(f\) is infinitely differentiable at 0, then we can rewrite it using the Taylor expansion:

\[ f(x) = \sum\limits_{n=0}^\infty \frac{f^{(n)}(0)}{n!}x^n,\quad \text{for some }x. \]

The set of \(x\)-values for which the Taylor expansion is valid is called the radius of convergence. Here are examples for specific functions:

\[ \begin{aligned} \frac{1}{1-x}&=\sum\limits_{n=0}^\infty x^n,&&-1<x<1\\ e^x&=\sum\limits_{n=0}^\infty\frac{x^n}{n!},&&-\infty < x < \infty\\ \ln(1-x)&=-\sum\limits_{n=1}^\infty\frac{x^n}{n},&&-1\leq x < 1. \end{aligned} \]

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 500

# app.R
library(shiny)

ui <- fluidPage(
  titlePanel("Taylor Polynomial Approximation"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "func",
        label = "Choose a function:",
        choices = c("exp(x)", "sin(x)")
      ),
      sliderInput(
        inputId = "degree",
        label = "Degree of Taylor polynomial (n):",
        min = 0, max = 10, value = 1, step = 1
      )
    ),
    
    mainPanel(
      plotOutput("taylorPlot")
    )
  )
)

server <- function(input, output) {
  output$taylorPlot <- renderPlot({
    # Define function and y-limits
    f <- switch(input$func,
                "exp(x)" = function(x) exp(x),
                "sin(x)" = function(x) sin(x))
    x <- seq(-5, 5, length.out = 400)
    y_true <- f(x)
    
    # Fix y-limits by function
    ylims <- switch(input$func,
                    "exp(x)" = c(-1, 150),
                    "sin(x)" = c(-1.5, 1.5))
    
    # Define Taylor polynomial
    taylor_poly <- function(x, n) {
      sapply(x, function(xi) {
        sum(sapply(0:n, function(k) {
          deriv_k <- switch(input$func,
                            "exp(x)" = 1,
                            "sin(x)" = if (k %% 4 == 0) 0 else if (k %% 4 == 1) 1 else if (k %% 4 == 2) 0 else -1)
          deriv_k * xi^k / factorial(k)
        }))
      })
    }
    
    y_approx <- taylor_poly(x, input$degree)
    
    # Draw the plot
    plot(x, y_true, type = "l", col = "black", lwd = 2,
         ylim = ylims, xlab = "x", ylab = "y",
         main = paste("Taylor approximation of", input$func, "around x = 0"),
         bty = "n", axes = FALSE)
    
    # Add axes through origin
    abline(h = 0, v = 0, col = "gray70")
    axis(1, pos = 0)
    axis(2, pos = 0, las = 1)
    
    # Add Taylor curve
    lines(x, y_approx, col = "red", lwd = 2)
    
    # Add legend
    legend("topleft",
           legend = c("f(x)", paste("Taylor polynomial (n = ", input$degree, ")", sep = "")),
           col = c("black", "red"), lwd = 2, bty = "n")
  })
}

shinyApp(ui = ui, server = server)