Before we can really get started with interpolating our data in terms of functions, I want to talk
about a concept called Taylor expansion and Taylor expansion means that you can basically take any function
and you can expand.
It is a series or as a sum that goes to infinity over these terms that you can see here.
So you can expand it as a polynomial.
And the equation is a sum of these terms, which are basically here.
The end the derivative of the function f at the position x zero, which is the position or the argument
around which we expand the function and that we have here are X dependence.
So here the distance from the zero will then be squared and then taken to the power three and four and
so on.
So of course, in numerics, you cannot go to infinity here.
So at some point this series has to terminate and you have to define some and max value at which you
stop expanding the function.
And then hopefully in the range where you are interested in the data, then the function is approximated
to a good amount and we want to start and we want to test this concept, the Taylor expansion at the
example of the exponential function.
But first of all, I will run the cell here where I will import our two most important modules non pi
and multiplied lib.
So here I've written down already that we are investigating the exponential function, which is f of
X is equal to exponential function of X.
And as you can see from this equation here, we need the first derivative, the second to third and
so on.
We need all the derivatives and the special feature about the exponential function.
This is how it is defined, is that the derivatives are all the same.
So every derivative of the function is the function itself.
It's still exponential function of X.
So what we can do then very easily is we can define a function called EEGs P Taylor, which will be
our Taylor expanded series of the function of X is equal to exponential function of X.
So of course, we need an argument x.
Then you can see we need to define some position at which we expand our series.
This would be x zero and then we must define the upper boundary here because, as I said, we cannot
go to infinity.
So we have to define some and maximal u.
And just to make sure that we remember later on, I will explain here all of these arguments.
So X is the argument of the function that zero is the argument at which the derivatives will be calculated
and then the and maximal you will be the end at which the series will terminate or at which it will
break.
You could also say and now what we will do is we will sum up several parts here and you can do this
in several methods.
You could, for example, define the individual terms as an array and then you can use the N pitot sum
equation.
We will do this also later in this notebook.
But here I will just use a simple loop.
So I will define here some value T, which will in the end, be the value of our Taylor expansion.
And initially it is zero.
And then we make a loop here for n in range.
You can, of course, also take another type of loop.
So there are many possibilities how you can do this, and we are summing up starting from zero and going
to an max.
And we include the value of N Max, which is why I wrote, which is why I wrote and max plus one here.
So now what we do is we add up all of these values.
So we write T is equal to the old T plus some value.
And now, of course, it is a bit stupid to use the exponential function to define the Taylor expansion
here.
But yeah, let's let's do it for the moment is just an exercise here.
So we will calculate them the and P X value at the position x zero.
So for example, if we would select zero equal to zero, then we could just write here one, because
we know the exponential function of zero is always one.
So then it would maybe be a bit more reason.
But then we would be restricted to just to one argument, zero.
And I want to show you that this series can also look different when we choose different values of zero.
So this is why I take your export of cereal.
And then we multiply.
So we take the the argument x minus x zero to the power of end.
And so we have these two terms and now we are just missing this one here, which is two factorial.
So for example, four factorial four exclamation mark means four times, three times, two times one.
And we could, of course, program this here.
But there is already a function that's implemented in nonpaying math module, which is called factorial.
So we can just use this one here.
And then don't forget, we have to return some value and of course, we want to return to.
So as I've said, you can also write all of this in just one line when you use the and p dot some command
and just sum up over an array where the elements of the array on these terms here.
But yeah, let's not make it too complicated.
And I think this one is even easier to understand.
And now we can write the P Taylor.
So now we run this function and we can choose, for example, the argument one for the evolution point,
we take zero and then for the terms, let's use ten for the beginning.
And so we get this number here.
Two point seven one.
So maybe this reminds you of the Euler constant and that check.
So and p dot s p of one is almost this value.
So you see, by taking into account only 10 terms of this infinitely large series here allows us to
reproduce the Euler a number by a very large accuracy.
So it works pretty well for this particular function.
And also the reason why this works well is because our position one is quite close to our ex zero.
And I want to show you what happens when you select a different position for the evolution.
So I want to make a plot here, so I write X list is equal to NP total in space.
And I want to plot in the range of four minus five to five.
And then first of all, I want to do a sketch of plot and I want to plot this list of coordinates that
I've just created.
And then we just plot the exponential function.
So this will be our reference point.
This will be the correct exponential function as it is implemented in non pi.
So this will be the one that we are trying to recreate here, and I will do this now.
Maybe first let me add labels here on appeal to you told X Label X, of course.
Then we need a y label.
Oops, sorry y label, which is why and we need some limits.
So I write pl t y limits x limit as OK, we have already defined this by this command and the Y limit
will be minus five to 100.
All right.
And now I can go ahead and now I can plot the Taylor expansion that we have just find.
So here I will do a line plot, all right, to appeal to the plot and then axe lists come out XP Taylor.
And for the argument, we do not only take a single value, but we take our whole list.
And of course, this works.
And then 40 zero.
Here I want to take zero and then we to find some value and max, which we have previously chosen to
be 10.
So that's right.
And Max.
And let's create these points or this line in a color blue.
So you see, if we take 10 points into account, then it works quite well.
I mean, you see maybe a bit here, some change, some deviation, but overall it works quite well.
For example, if we take five only, then you see in the vicinity of X is equal to zero, which is zero.
In all case, it works well.
But then when we go way beyond plus minus two and we see some large deviation of the plot, and this
changes if we take the different reference point for the evolution, for example, we could take here
minus three and we can plot it in red or we could take two and we plotted in green.
And then I see for the to function, which corresponds to minus three.
It works pretty well here for small values, but not for high values and for the green function, which
is expanded near zero equal to two.
It is exactly the opposite way.
So here and works well in this area, but it doesn't work at all for small values.
So you see Taylor expansions work and we can tune the accuracy by this parameter and max.
So of course, the higher end max will be the more terms we take into account and the longer our calculation
will run.
But you see, if we make it very large, then we can basically fit any function that's continuous in
any range for the X value.
So I will go back to the initial value because here you can see nicely that the position of zero matters
and in the next lecture, we will expand our next function and I want to show you something else.