liveBook · Manning

Taylor series

As explained in Julia as a Second Language

A Taylor series is a power series representing a function as an expansion around a specified point using its derivatives.

A Taylor series approximates a function by adding terms built from its mathematical pattern. For sine, it turns a calculation that once required tables into a repeatable program. section 3.1

The summation behaves like a loop: iterate across a range, calculate each term, and add it to a running total. The exact sine definition uses infinitely many terms, so a program chooses a finite n. section 3.4.1

Implementing sine in Julia

A compact Julia function sets n = 5, loops from 0 to n, and accumulates (-1)^i*x^(2i+1)/factorial(2i + 1). Its result closely matches Julia’s built-in sin: sine(0.5) returns 0.4794255386041834, while sin(0.5) returns 0.479425538604203. section 3.4.1

A practical Taylor-series workflow:

  • Express the function as a summation. section 3.4.1
  • Translate the summation into a for loop with a running total. section 3.4.1
  • Increase n and compare the approximation with the built-in function. section 3.4.1
Listing 3.11 Sine implemented using the Taylor series
function sine(x)
   n = 5
   total = 0
   for i in 0:n
       total += (-1)^i*x^(2i+1)/factorial(2i + 1)
   end
   total
end

View the complete Julia implementation that converts the sine Taylor series into executable code. section 3.4.1

How Julia as a Second Language covers it

How deep it goes
Explains summation-to-loop translation and finite accuracy, but does not prove the Taylor-series definition.
Examples
Uses sine calculations, trigonometric tables, a Julia summation function, and comparisons with built-in sin.
What it assumes
Assumes basic high-school trigonometry and develops summation, Boolean conditions, loops, and multiline functions incrementally.
What you can do after
Build it from scratch by implementing sine with a finite Taylor-series loop and testing its accuracy against sin.
How it connects
Connects summation, for loops, functions, approximation, sine, cosine, and tangent.

Also explained in

Compare how 3 books explain Taylor series →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.