segunda-feira, abril 18, 2011

Some important Taylor/Maclaurin series

Have you ever asked yourself about how your scientific calculator or your computer "know" the sine or the cosine of a number?

Maybe you thought that those devices had predefined tables inside their memories to calculate those functions. But in fact those tables don't exist! So, how those devices are able to give you the correct answer with great precision?

The truth is that they use algorithms that calculate the results of Maclaurin functions. According to Wikipedia: "A Taylor series is a representation of a function as an infinite sum of terms calculated from the values of its derivatives at a single point. Taylor series were formally introduced by the English mathematician Brook Taylor in 1715. If the series is centered at zero, the series is also called a Maclaurin series, named after the Scottish mathematician Colin Maclaurin who made extensive use of this special case of Taylor series in the 18th century."

This is an important subject of Calculus, but the objective of this post is to show some of those functions, so we won't make demonstrations here. If you really want to study it further, I recommend you to check the book: "Calculus - Volume 2, by James Stewart". Bellow you can appreciate that interesting way to calculate some well known functions:

The exponential function:


Sine and Cosine (and hyperbolic ones):








We can tell you those are ways to approximate the functions values, and precision depends only of the value of n. Note that those sommations go from zero to the infinity, then the value of those sommations converge to the real values of those functions, but computers can't calculate them to infinity. Thus, we calculate each sommation to a finite (and relatively "small") value of n, in a manner that satisfies the desired precision that we need. One example is given bellow:


Here we calculate the exponential function for x = 2 (that is: e^2)


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 3.0
For n = 3 , the sommation is: 5.0
For n = 4 , the sommation is: 6.33333333333
For n = 5 , the sommation is: 7.0
For n = 6 , the sommation is: 7.26666666667
For n = 7 , the sommation is: 7.35555555556
For n = 8 , the sommation is: 7.38095238095
For n = 9 , the sommation is: 7.3873015873
For n = 10 , the sommation is: 7.38871252205
For n = 11 , the sommation is: 7.38899470899
For n = 12 , the sommation is: 7.38904601571
For n = 13 , the sommation is: 7.38905456683
For n = 14 , the sommation is: 7.38905588239
For n = 15 , the sommation is: 7.38905607033
For n = 16 , the sommation is: 7.38905609538
For n = 17 , the sommation is: 7.38905609852
For n = 18 , the sommation is: 7.38905609888
For n = 19 , the sommation is: 7.38905609893
For n = 20 , the sommation is: 7.38905609893

According to the built-in function of Python's math library, e^2 is: 7.38905609893

In this example, we conclude that n equal or greater than 19 gives a good precision (if compared with Python's built-in function) for x = 2. But for greater values of x, we should increase the number of iterations. Let's check another example, for x = 5:


For n = 1 , the sommation is: 1.0
For n = 2 , the sommation is: 6.0
For n = 3 , the sommation is: 18.5
For n = 4 , the sommation is: 39.3333333333
For n = 5 , the sommation is: 65.375
For n = 6 , the sommation is: 91.4166666667
For n = 7 , the sommation is: 113.118055556
For n = 8 , the sommation is: 128.619047619
For n = 9 , the sommation is: 138.307167659
For n = 10 , the sommation is: 143.68945657
For n = 11 , the sommation is: 146.380601025
For n = 12 , the sommation is: 147.603848505
For n = 13 , the sommation is: 148.113534955
For n = 14 , the sommation is: 148.309568205
For n = 15 , the sommation is: 148.37958008
For n = 16 , the sommation is: 148.402917371
For n = 17 , the sommation is: 148.410210275
For n = 18 , the sommation is: 148.412355247
For n = 19 , the sommation is: 148.412951072
For n = 20 , the sommation is: 148.413107868
For n = 21 , the sommation is: 148.413147067
For n = 22 , the sommation is: 148.4131564
For n = 23 , the sommation is: 148.413158522
For n = 24 , the sommation is: 148.413158983
For n = 25 , the sommation is: 148.413159079
For n = 26 , the sommation is: 148.413159098
For n = 27 , the sommation is: 148.413159102
For n = 28 , the sommation is: 148.413159102
For n = 29 , the sommation is: 148.413159103
For n = 30 , the sommation is: 148.413159103
According to the built-in function of Python's math library, e^5 is:148.413159103


Now note that n = 19 isn't enough to give the same precision as before. And the precision for n = 19 will get worse for greater values of x. Thus, the greater is x, the greater is the number of necessary iterations to get a good approximation of the real value of the function.

Nenhum comentário: