Welcome back to the course, but we have so far discussed how to calculate first derivatives of functions.
And now we want to discuss how we can calculate higher derivatives.
So for example, here is a second derivative and mathematically speaking, this is totally easy.
You just take the function, calculate the first derivative and then from the first derivative, you
calculate the second derivative in exactly the same way.
So once again, you take the limit to zero of the first derivative and you introduce such a small value,
which then goes to zero.
So you see when we now use our definition for the first derivative and plug it in here.
This gives us many possibilities for the definition of the second derivative.
So, for example, we could use here the forward method here, the Beckworth method we could use here
the central method to forward methods so we can combine all of these methods and we have to figure out
which one is the best way to do it.
So as an example, we could, for example, use double forwhat.
So this means we start from this equation and we use for the f first derivative and this one also the
forward method.
So this means for this term, we will get the function and x plus two h minus the function of X plus
one h divided by h divided by h.
So this means we have this term here divided by h squared.
And then for the other term we get function and x plus h minus function and x divided by h divided by
h.
So we get this divided by square.
And so if we solve these brackets here, we get this expression with the three terms.
If we to the double backwards, it's pretty similar.
We get a similar results.
We get here just different arguments where we'll use the previous point and the second nabor point in
the previous direction or in the backward direction.
What we can also do is, of course, we can combine forward and backward methods.
So this means we start from here.
But do you use the backward method for this one and for this one?
So here we will get f off x plus h minus f of X, and here we will get f off x minus f of x minus h.
And so if we solve then the brackets, we get this one.
So we get f at the next point minus two times F at the current point, + f at x minus the previous point.
And then we divide all cases by age square and take the limit h to zero.
This is exactly the same result as we get for using the double central methods.
Here we would just have then x plus two h minus two times F and X plus f x minus two h divided by four
h square.
But since h, uh, goes to zero, we can also substitute it with a new variable to Asia's equal to G.
And then we have this expression and this is exactly the same as this one.
So don't worry if this is too mathematical for you, it's not a big deal.
The main message here is that in the end, we have once again a forward method, a backward method and
a central point methods.
And these ones we are going to implement now and we will like previously for the first derivative,
we will compare them.
And since it's so similar, let me copy the code from previously.
So this one?
And let me just paste it here at the bottom and updated for the second derivative.
So we have here the analytical de-list, which we don't need anymore.
And instead, we need the analytical D to list for the second derivative.
And so here we need to think a bit.
Maybe let's first do the last term because this is most easy.
It's just this one.
And then we calculate here the derivative, which is
cosine.
Then from here we get to terms.
The first term will be cosine times one.
So we get this term second time and the other term will be like this with a minus sign because a derivative
of cosine is minus sine.
So we update this two analytical D2 lists.
And here we update the label to the second derivative.
And now we must update these lists here to the to the to the two tier as well.
And of course, now we must.
Update the values themselves, so in all cases, we have here to square.
And here we now also have an h square and here we have X list plus two times h minus two times f x list
plus each.
And then we have plus f x list.
And the other terms are pretty similar.
So I will just copy them.
And here the difference is that we have had like this minus two times h.
And here we have it like this.
Yes, so I have not implemented the double forward, double backwards and the double central, which
is equivalent to the forward and backward methods.
And when I run this, we see first of all, it seems to be correct because all four curves are roughly
the same.
And we know that the analytical result is, of course, always correct.
And you see also that there are some differences you can really see here yellow, red and also green.
So once again, let us compare the differences and we do this, of course, in the same way as previously
we right here Arrow.
And we right here.
That we calculate the difference between these two lists, and we always take the analytical list as
a reference point.
And then we also change the color of the plot.
So here we use red.
Here we use blue.
And here we use green.
So as previously, the red and blue curves double forward and the double back watch are not as good
as the double central or combined forward and backward mechanism.
And the reason is, of course, it's pretty similar to previously when we would do now the Taylor expansion,
we would see that the double central methods would have pearls that cancel out.
And so the accuracy would be much, much better.
Now, this is not really surprising.
And also, it's not surprising that we can now also take into account further nearest neighboring points
to achieve a higher accuracy.
And this time, the Richardson formula is given by this expression.
So once again, we take the nearest and to second nearest neighboring points.
And I will just use the code from previously to update to update our function.
So I will use this one and also I will use the following cell.
Let me copy this as well.
Until this time, of course, we will now have to detour Richardson, and we must update now this equation,
which will be minus F and X minus two h plus 16, that we have an additional term that cancels out previously,
which is minus three times f of X than we have plus 16 and minus those one is OK is correct.
OK, now we just change what we have written down here.
D2, Richardson, Richardson, d to list and then we just update here everywhere.
D two and here as well.
And now I run both cells, and we see that apparently I made a mistake, which is OK, but let's see
if we can find the mistake.
So it seems as if the Richardson function gives a large arrow, but we know that this is not what's
supposed to happen.
And of course, here I forgot something here.
It's one of a 12h square.
So here we missed the square.
And this time it looks much better.
Yeah.
This time we have an error for the double of central methods of approximately six times 10 to the minus
three and four the Richardson method.
We have an error of 10 to the minus five.
So it is, I don't know, maybe, yeah, like 500 times better.
So the logic behind this is really, really similar to the first derivatives.
You can calculate the second derivatives pretty similarly.
You just have to use the first derivative equations and plug them into each other so that you end up
with some double forward, double backward or double the central methods.
And then you will also find once again that some of these equations are better than others, and you
could understand this analytically if you would write down the tether expansion.
But yeah, that's a bit too much of a hassle here.
We just believe these results and just analyze it numerically.
And what we found is that the double central method works much, much better than the double forwhat
and the double backward methods, and that there exist even better methods like the Richardson equation
that is given here, which is based on taking into account also second nearest neighboring points.
So this is all not really surprising because it is absolutely identical to the first derivative procedure
that we have discussed in the previous lectures.
Still is, of course, very important to keep in mind that this also works for the higher derivatives.