So we have just seen that the central difference, this method is much more accurate than the forward
and backward diferencias methods for calculating the first order derivative of the function F.
We have also seen that we can prove this analytically.
And the reason was basically that certain error terms cancel out when we calculate the difference of
these two terms.
Then these two terms, he will vanish.
And this would lead to a first order error term and h.
And since they cancel out, the order of the error will be square, which arises from this term, which
will be the next higher term.
But from this idea, you can already see that it's possible to calculate the derivative even more accurately.
And for this, we of course, have two methods.
The first one is pretty similar to the mathematical way we just decreased the value of H.
So if we have a continuous function like in our case and we know already what the function is, then
this is not such a big problem.
We can just decrease the value of H.
But for example, if we would just have some data points and we would not know the precise function,
then this is of course, not an option because then H is basically fixed.
So what it must tend do is we must find ways to calculate the derivative with a higher accuracy.
And for this, we must take into account even more terms, of course.
So even more points of our list.
So you see, for example, here we take forward the four differences method the current point and the
next point.
And for the back, what differences the current point and the previous point.
And here for the central differences, we take the next and the previous point.
So an idea would be to take an account even the second next and second previous points.
And this is exactly what you can do to get a higher accuracy.
And this is based on this Richardson equation for the first derivative term.
You see, we take into account the yeah, the term with a minus two h minus one h positive one h and
positive to h.
So this is like a bit of a central difference this method, but even more refined because we take into
account even the further terms.
But you also see that in this weighted sum here, the the terms with it x minus h index plus h have
a much higher weight than the terms with the two h.
But anyway, let's defined as richesse and function and see how it performs compared to our central
differences methods.
So we write and define and then I just give it a name and I would call the D1 Richardson.
You could, of course, give it a different name as well.
And now we must find some arguments.
Of course, we need the function itself.
We need the argument and we need the step size.
So let me write this down just in case we forget that later on after will be the function X will be
the argument.
So these here are just comments, of course.
So you could leave them out.
But it's always helpful to have something like this in your notebook so that later on you remember what
these arguments mean.
And then our function should just return a number and the number is given by this equation.
So it's pretty easy to implement which is the right one over 12 h, and then we must write down these
four terms.
So the first term will be f of X minus two h.
Then we have another term, which is minus eight times half of x minus h.
Then we have plus eight times of x plus h, and then the last term will be minus one times f of x minus
two times h.
And so this is our Richardson formula.
And now we can compare how this method performs compared to the other methods.
So I will just copy this one here and.
And another plot, so I will just add the plot.
First of all, we must create a list, of course, so I write Richardson.
The list is given by the one Richardson.
Our new function and the function will be after the one that we have to find previously.
X will be our list of X coordinates and H will be h, which was zero point one that we have to find
previously.
And now we can add another plot here where we subtract this new list from the analytical solution and
for the color, it's just black.
And so here we see that there seems to be a problem and an arrow because the newly added function the
Richardson method gives the largest arrow, which is of course not what we expect.
So let's check where it is the error.
So this one is correct.
And see here I wrote accidentally a minus two h, but it's a plus to age, of course.
So let's redefine and let's rerun it.
And we see this time the black function is almost equal to zero.
So we know that these first two functions to forward and backward method, they are not so accurate.
So let's at them out so that we only have green and black.
And you see, black is a much, much better.
So for green, we have an aura of the power of 10 to the minus two, and for green we have an error
of 10 to the minus five.
So it's approximately 1000 times better.
And this is, of course, because here we did not only consider the neighboring points, but also the
second neighbors.
And so this allowed us to construct a superposition that has more of these error terms cancel out and
therefore gives us a much better accuracy.
And an even better accuracy can, of course, be achieved if we take more and more neighbors into account.
So that in adding several of these Taylor expansions, more and more of these error terms will cancel
out and then the error will have a higher and higher order and the result will be better and better.
And this is what is called the richest in interpolation or rich iteration formula.
And so the iteration formula is as every iteration formula based on the previous results.
So you repeat this process over and over again.
So we start from D1, which is the result of our D1 Richardson given by this equation.
And then we can improve the results by calculating D2, according to this equation.
So in this case, it would be and would be one.
So we would have here two to the power of two, which is four than we have here.
Our previous results for the step with H.
Then we have here our previous results for the step with two h.
And here we have four minus one, which is three.
And then we can iterated another time that we can go to the third order term and we can repeat this
as many times as we want.
And the result will get better and better because more of these terms in its halo expansion will cancel
out.
So I want to show you how we can, how we can implement such an iteration formula in our Python notebook.
And of course, here it's a bit tricky because you see we have here the previous term for Step with
H and here, the previous term for step was to H.
So if we go to the second order, so which would be here an equal one so that we have here two, then
we would need D1 at h and we want it to h if we go one step further.
If we go to an equal to which we have here the three, then we would need D two of H and the two of
two to, uh, two h.
And for the two of two h, we would need D one of two h and the one for H.
So you see, we need more and more First Order terms, the higher order we have, the higher order we
want to consider.
So let me just maybe show you the solution or my idea how I would implement it.
So of course, we'll be a bit similar to our D one Richardson.
So I ride to D1 and Richardson, and we need to define.
Here's some order.
So this would be the order not of the derivative, but order of iteration for first degree.
Of it.If.
So this time, we will not return this command here, but we will store this in some intermediate value.
So maybe something like this.
However, I also told you that we do not only need the first auditorium at age, but also at 2H.
And if we go to a higher order, we even need it for age and then at eight h.
So this depends on the order.
So for our zero, I will define an array and this array will of course, be given by this term.
But here we multiply.
Or basically we replace all of the ages by age times two to the power of J.
So let me do this right here at times two to the power of J.
And then I do this for every age.
And the value of G goes for J in range from zero to end.
So this, of course, depends on the order of iteration that we want to consider.
So this is now an array.
So of course, what?
Maybe then it becomes more clear what I want to say here.
Of course, we could have also written down here is that the zero is just such an empty dot array for
j and range.
But the entry itself is just given by the Yvonne Richardson.
F comma, x comma and then h will be modified by H Times two to the power of G.
But the thing is, I do not really like to call functions inside other functions because maybe at some
point we want to copy this to a different notebook and then we must remember that we also copy this
function.
So I think it's better to just use this more general approach here, even though it looks a bit more
difficult.
But this is just the same thing that we have written down in the Richardson function.
All right.
So this will be our starting point.
So to say our D one and then to calculate D two, so which means an equal one, we need to do the following
we need to update this we need to see I right.
It's already in such a loop here for I in range one to NW, we do the following just for testing purposes,
I would say we print, first of all, d zero so that we see the value at the current stage of iteration.
And then we update the value of D um, which we will, by the way, further out outputs later.
So.
So return D will be this thing that we do at the very end.
But now we update to B and P dot array.
And here we were at the command, which would basically be this equation here for Jay can range.
And now basically we must reduce the length with every iteration.
So I just try to length D0 minus one.
And here we write them this equation.
So there will be two to the power of two times.
I yes, because um yeah, maybe I should have called it a bit differently.
So maybe you let me do it like this.
I hope this is sort of it's less confusing to you.
I called the order M or I could also call it, and max and Max is good.
And here and we have to replace this one with and Max.
And here it goes to and Max and then here we can use and so that we have the same one as here.
So then we can ride like this.
Yeah, I like this much better so that we can vote two to the power of two end times.
The solution of the for argument h.
So this would be a D0 and Jayco component.
So of course, for the first element of the list of the NRA, this will be zero zero, which will be
the first term here, which will be the end of h.
And then we rides minus D zero j plus one and then we divide by the denominator.
So this will be two to the power of two times J.
Two times and sorry.
And then the line is one.
Exactly.
So now we have Jay here.
Iterator for the index of the zero and An, which is the looping index for this and from this equation,
I think this looks good.
And of course, then we have to update our D0 with the current D.
So when I run the function, I think you will see what happens here.
So I hope I didn't make a typo.
This would be bad, but let's see what happens.
So I just call the function one.
And Richardson and Max let's, I don't know, use five f will be our function from previously and I
want to do it at the position, let's say three
three and I run this, OK?
And so this these will be the intermediate results from the print command.
So you see in the first loop, we get this result in the second loop, we get this one and the third
loop, this one, fourth, this one and then the fifth loop.
Finally, we get our result, which will be this value.
And now we can compare this to the D1 Richardson.
And for this, I will write D1 Richardson, for example.
This value and this value corresponds to this value.
So let me show you something.
I think that becomes more clear what I did above.
I will copy this five times and then I will write you two times h four times h, eight times h, 16
times h.
And these five values are exactly the values that we get here.
So these are the values that are in this and P Todd's array.
So, yeah, basically we have here this iterated J going from zero to and max.
And so we have five terms in this array.
And then comes the first iteration where we calculate where we update our D, according to this equation.
And so here we calculate basically D to.
Based on this one, and we need the data for four different values we needed for age four to age four
four eight four eight each.
Then comes these three and then we need the three for age to age and for age, the four we need for
age and to age.
And then from this, we can calculate DeFife.
And now the only thing left to do is to see if this really is an approved is it is an improvement.
So I have to write three times dot and P Dot Cosine, because that's the analytical solution.
So you see, I tested this before with an argument of two, but this time I choose three, so we have
to update this.
Um, I'm not really sure why I did this before, but that's updated to three.
And then let's right and p dot sign of three minus three over 100 times three squared.
So this will be the value that we get analytically and now we can compare this value and we can just
subtract the value from the Richardson formalism.
So when I called his one and then the output from the return disappears and will instead be stored here.
And so then we can subtract a two year deal on end, and we see the error with respect to the analytical
solution would be a very, very small.
It will be to tend to the power of minus nine.
And previously from the first or two terms, we had an error of 10 to the power of minus five.
So this was just to show you that there are methods to calculate the derivative much more accurately.
And the trick is to combine more and more terms and further and further nearest neighbors for these
individual data points.
So it had several hours and a Taylor function cancel out.
Of course, in our course, we will not always do this difficult stuff here.
We will not always want to have such a precise derivative.
Most often, we will just use the central difference as methods.
Well, we'll just use the left and the right neighbors and calculate the derivative from here.
And maybe sometimes we need a bit of a better accuracy.
So we take the Richardson formula.
But if you want to have a really precise derivative, then this is possible and you can take this iteration
formula.
And of course, here we have taken and max equal to five.
But of course, you could take year 100 and then the derivative will be more and more precise.
But most of the time, it's really a question is it worth it?
Because you see to calculate this one derivative?
We had to calculate here five plus four plus three plus two plus one values to finally have it.
So you really have to think, is it worth it to spend the time to calculate the derivative more precisely?
Or is it OK to just stick with the central differences?
And then most of the cases, it is OK to just use central differences.