So we spent our last lecture in defining the general form of the derivative.
So now we are able to numerically calculate the derivative of any function, not only the first derivative
but the derivative.
So now we can go ahead and define the Taylor expansion so we can right now def Taylor.
And this time it will really be a general Taylor from the river.
Sorry, a Taylor expansion of any function and up to and or there.
And yeah, really the most general thing you can do for Taylor expansions.
And so now we need quite a lot of arguments.
So of course we need f, we need X, we need X0 Then we need and max the index word a series will terminate.
And then also we need the value h because we have the derivatives here.
So F X and zero are the same as we had before.
So like, for example, here.
So I will copy this.
These are just the comments for the arguments and f will be the function, of course, and age will
be what we have written here.
The step size.
And now we can go ahead and we can basically just program the very first equation in our notebook,
so let me briefly scroll up here.
So this equation we have to program and for this derivative here, we have just defined our general
function.
So we will of course, call this function now.
So I will scroll down back to the Taylor code and once again, we have a sum, so I will once again
do it in a loop, even though this is not really the most elegant way of doing it.
But I think it's very instructive and you understand much better what is going on.
And now we, of course, as previously add up the values and then we will return the value of teeth.
So now we have to program the terms.
And if you don't remember, you can scroll up in your notebook once again.
But I remember we have to calculate the derivative.
So now you just have to be careful to use the correct arguments here.
So we have function acts, age and so function will, of course, be the same as here then.
We have zero than we have each, and we have.
And so here you have to be careful that you don't use and max because and Max is the index in which
the series will terminate.
But here we have end, which is the index over which we add.
So I will just briefly scroll up one more time.
So here you see the indexes and really so yeah, like this should be correct.
And then we calculate the difference here x minus x zero, which is the position at which we calculate
the derivative and we calculate this to the power of RN.
And then we are only missing the factorial, which is given like this.
So factorial of and and now I have a problem because I forgot this one.
And now it could work.
Let's check.
At least we don't get an error message.
So I said already, I want to plot something here, so I want to make a plot pretty similar to this
one.
So I would copy this so that we don't have to type so much and don't lose too much time.
So for the next list, I will go to the limit minus five to five.
Then for the scatterplot, I will use X list and here I will use our function func than this one.
Looks good.
But now, of course, we have to update this one.
And here we have to just write in general, Taylor and.
For this one, I will come back later, and for Taylor, we have to, of course, use now the correct
arguments.
So we have to right first argument is to function funk, then x list for the X values, and this will
really be a whole list, not just individual numbers.
Then for zero, we can really try different values.
So for example, zero now we can use and Macs and Macs.
I would say we should take some number like let's start with five and four h.
We should also test a few values, but for the beginning, let's use zero point one.
And this looks good.
But now, just to add a few more of these plots at different x zero positions, I will use two and minus
three things is similar to what we had previously and change the color red and green.
And so now we have here our plots.
Maybe the right y range doesn't look so good.
That's changing a bit.
OK, now it looks better.
So you see these light blue dots here to scatter plots.
This is the actual function that we have defined.
This was something like sine square.
Or is it here two times sine squared plus x and the other functions, the plots here it has lines.
These are our Taylor expansions at different positions, at the position zero, two and three or minus
three.
So you see that at their respective points, for example, here near zero for blue, it works, OK?
But the range in which it works is not too large.
So what we would have to do here is probably to increase the number of terms to be taken to account
for this series.
So if I increased this number?
It looks much better.
So we have a much larger range of X values where the function would series agrees with the function.
However, the agreement is not really that good.
So what we can do is we can change the value of h for the derivative.
And then you see, I would I would say the fit now looks better in this in this very narrow area.
But this time, of course, again, the range where the fit is OK has decreased.
So this means decreasing h gives you a better quality for the derivatives, which gives you a better
accuracy of the series near the expansion point, but it loses you some accuracy far away from the expansion
point.
So that's a bit of a trade off here.
So, of course, you could also increase the number of end max, but then you get sometimes some artifacts,
so you really have to play them with the values of H and and Max to get really an ideal result here.
So it can be sometimes a bit difficult.
All right.
So but I think overall we have seen that it works quite well to approximate these functions in some
narrow range around the position X0.
And we have learned that we can describe it basically any function as a polynomial by just using our
our expansion formula from the very beginning, F of X is equal to the series of these derivatives times,
these polynomial terms.
What I think is also really nice is that we have written all of this in a very general way, so we cannot
only calculate this Taylor expansion for this particular function, but we can just copy the whole block
of code here and also copy this line of code where we have defined the function and we can just redefine
the function here in our new self.
We can just take another function.
For example, we could take two times sine squared times and p dot exponential function.
And then we just use some gauss curve.
So this time, maybe we should change the range of it.
This time we have such as sine function or sine squared, but to to say and modify modulated by the
gauss function.
And you see in the vicinity of the respective expansion point, the Taylor expansion works quite well.
And I think this is really a nice tool that we have established here with this definition of the Taylor
expansion where we have used also this definition of the derivative.