So in a previous lecture, we have expanded the sound function into position zero is equal to zero.
And the reason why we were only able to do it at this particular position is that we wanted to calculate
the derivatives analytically.
And here it is particularly easy because it's just zero minus one and plus one.
However, if we want to do it in a more general way, then we also must be able to define or to expand
the sine function or any other function at any given position.
And for this to to be able to do it in general, we must define the derivative numerically and this
is what we are going to do next.
So first of all, we have to define the derivative, that first derivative, but also the higher derivatives.
And I have dedicated in this course a whole section on derivatives so that we will explore this in much
more detail and we will we will explore how you can calculate derivatives with a much higher accuracy.
But in very simple terms, you can define a derivative by this argument here or by the way, this term,
mathematically speaking.
So you introduce a small number each, which will be a distance in x direction and then you take the
difference for the Y values of this function at these two positions.
So you take it at X plus h and an X, and then you just divide by the distance and X direction.
And this gives you the slope of the triangle and the slope of the tangent of the function, and this
is the first derivative.
And we can the find this quite easily, so we can define here a function called the derivative and for
the arguments.
We need, of course, to function and the position, but also we must define the step size.
So basically the value of H.
So as I said after B, the function X is the argument of the function and H is the step size.
And the value that we return is pretty easy.
It's, of course, just this expression that is given here.
So if the Position X plus each line is at the position X and then we divide the whole thing by H.
So here in terms of mathematics, we have to do a limit of age goes to zero.
Only then the derivative is really exact.
But of course, in America, we cannot really do this.
So we must say, OK, we just take a small value for h, and that's all we can do.
Of course, as I said, we will also explore in the separate lecture how we can improve this version
and how we can make the accuracy much, much better than what is written here.
Now, for the higher derivative terms, it is not so easy.
I mean, in terms of mathematics, it is pretty easy to first calculate the first derivative of the
function F and to calculate the second derivative.
You just calculate the same thing again, but here with the first derivative.
So basically, you do this and iterations to calculate, for example, the third derivative, you first
must calculate the first derivative.
Then from here, you take this equation again with the first derivative if you calculate the second
derivative.
And from there, you calculate the third derivative.
But in our case, we want to have a function that is able to instantly calculate the and derivative.
And so you can imagine that what you have to do then if you think about iterations when you do this
over and over again, is that for the second derivative, for example, you must calculate here the
first derivative not only at Position X, but also at the Position X plus h.
And this is the problem.
To calculate the derivative at the position X plus h, you must know the.
The the value of the function itself at the position X plus h and X plus two h.
And so if you go to the fourth derivative for higher, then you must know the function at the Position
X plus order of derivative times h.
And so this is really, yeah, what's what, what we will do next year.
And you can also imagine that when you do this, when you have several terms here and some of these,
you can count how often they occur.
So some of them will occur more than once.
And this is where this term here comes from.
And yeah, if you think about it very carefully, I think you could come up with the solution.
But for the moment, let's just take this equation and that's try to program it.
So this is just something that you can find, for example, on Wikipedia.
Well, as I said, it totally makes sense.
And if you look at it carefully, for example, if you try to first calculate the second derivative,
then you will see how you end up with this expression.
So it looks more difficult than it actually is.
This is what I want to say.
So let us program this and then we will see that it's not so bad, actually.
So we can just go ahead and calculate to find a function called and derivative.
And this time we need the function to position the step, size and of course, also the order.
And so has previously the first three arguments are the same as for the first derivative, and then
the end will be the basically the end derivative.
And then we have to add up terms because we have the same here.
So once again, we do it similarly to our Taylor expansion and we create such a loop where we can,
for example, take a for loop and we have a running index case starting at zero and going to the value
end.
And we include and so therefore we write and plus one here.
And then in the end, we return for value of T.
So T will be updated here.
T is equal to T plus some terms.
And we will return the value of T.
And you can see here that this term is outside of the sum.
So of course, I could put it inside of the sum and write it here.
But it saves a bit of calculation time too, just after summing up the terms to divide by what is written
here.
So this will be h to the power of end.
So no, we have to just program this term, so it is actually not so difficult.
So we just have to write minus oops minus one to the power of and or no, it's not, and it's close.
And then we have this fraction of these factorial.
So we have then p dots, math dot factorial off and divided by and then let me copy this because it's
quite a lot to write.
We divide by Factorial K times factorial of and minus K.
And then we are only left to the function.
So we write F 50 Argument X Plus K Times page.
And I think this is already it.
So to test the whole thing, I will first test the derivative and then the higher derivative.
So I will define here a function which will be a test function.
So the function will, for example, be called two times sine of Tax Square.
So let's just use something a bit more difficult that we cannot so easily calculate analytically.
I mean, it would definitely be possible to calculate here easily the first, second, third derivative.
But if you want to calculate the tenth derivative, it's pretty annoying, of course.
So we can do it like this.
So we have our function and now we can calculate, for example, at the position x zero is equal to
ten point five the value that we had before.
We can now calculate the value of the function, which was twelve point something, and we can calculate
the first derivative of F, which we have called function func.
And then X is zero and then we have to define some h value.
So here I will just take, you know, I don't know, zero point one.
So like this.
And I get an error.
Let me see what is the problem here, name derivative is not defined.
Maybe I didn't run it.
Let's see.
OK.
Yeah, I just forgot to run it.
So, yeah, no problem.
Now it works.
So we have now our function, our derivative.
No, we can test a higher order derivative.
And we can basically copy this year and write and derivative of function x zero h.
And then we need.
And we can say and will be the and the river type.
So if we take a zero, then we should get the actual value of the function.
So I will write zero.
This gives us twelve point zero four.
Then we can write one.
This gives us the first derivative, which is this one.
So it worked.
And now, of course, we could just continue and check out some values.
But yeah, here we don't know if it's correct.
But since the first two terms are correct, I have high hopes that this works well.
All right.
But to really know if it works.
Let us define the general function for the Taylor expansion and then plot the function two times sine
squared plus x.
And then that is also plot the Taylor expansion and this we will do in the next lecture.