Polynomial Approximations to Elementary Functions

Elementary functions such sin(x), log(x) and exp(x) can be expressed as Maclaurin series:

images

In fact, we can approximate any smooth continuous single-valued function by a polynomial of sufficiently high degree. To see this in action, consider the graph of sin(x) against x in the range 0 < x < π (where x is an angle measured in radians):

images

x<-seq(0,pi,0.01)
y<-sin(x)
plot(x,y,type="l",ylab="sin(x)")

Up to about x = 0.3 the very crude approximation sin(x) = x works reasonably well. The first approximation, including a single extra term for −x3/3!, extends the reasonable fit up to about x = 0.8:

a1<-x-x^3/factorial(3)
lines(x,a1,col="green")

Adding the term in x5/5! captures the first peak in sin(x) quite well. And so on.

a2<-x-x^3/factorial(3)+x^5/factorial(5)
lines(x,a2,col="red")

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.