1
00:00:00,330 --> 00:00:07,140
So we have just seen that the central difference, this method is much more accurate than the forward

2
00:00:07,140 --> 00:00:13,420
and backward diferencias methods for calculating the first order derivative of the function F.

3
00:00:14,040 --> 00:00:17,550
We have also seen that we can prove this analytically.

4
00:00:18,150 --> 00:00:28,020
And the reason was basically that certain error terms cancel out when we calculate the difference of

5
00:00:28,020 --> 00:00:28,860
these two terms.

6
00:00:29,070 --> 00:00:30,900
Then these two terms, he will vanish.

7
00:00:31,290 --> 00:00:36,800
And this would lead to a first order error term and h.

8
00:00:36,810 --> 00:00:42,780
And since they cancel out, the order of the error will be square, which arises from this term, which

9
00:00:42,780 --> 00:00:44,430
will be the next higher term.

10
00:00:46,050 --> 00:00:54,060
But from this idea, you can already see that it's possible to calculate the derivative even more accurately.

11
00:00:54,900 --> 00:00:56,730
And for this, we of course, have two methods.

12
00:00:57,300 --> 00:01:03,580
The first one is pretty similar to the mathematical way we just decreased the value of H.

13
00:01:04,080 --> 00:01:10,740
So if we have a continuous function like in our case and we know already what the function is, then

14
00:01:10,740 --> 00:01:12,570
this is not such a big problem.

15
00:01:12,570 --> 00:01:14,490
We can just decrease the value of H.

16
00:01:15,240 --> 00:01:21,390
But for example, if we would just have some data points and we would not know the precise function,

17
00:01:21,750 --> 00:01:25,770
then this is of course, not an option because then H is basically fixed.

18
00:01:26,610 --> 00:01:31,900
So what it must tend do is we must find ways to calculate the derivative with a higher accuracy.

19
00:01:32,400 --> 00:01:36,330
And for this, we must take into account even more terms, of course.

20
00:01:36,660 --> 00:01:38,700
So even more points of our list.

21
00:01:39,390 --> 00:01:45,060
So you see, for example, here we take forward the four differences method the current point and the

22
00:01:45,060 --> 00:01:45,780
next point.

23
00:01:45,780 --> 00:01:49,410
And for the back, what differences the current point and the previous point.

24
00:01:50,310 --> 00:01:54,450
And here for the central differences, we take the next and the previous point.

25
00:01:55,410 --> 00:02:01,560
So an idea would be to take an account even the second next and second previous points.

26
00:02:02,490 --> 00:02:06,420
And this is exactly what you can do to get a higher accuracy.

27
00:02:06,690 --> 00:02:11,670
And this is based on this Richardson equation for the first derivative term.

28
00:02:12,090 --> 00:02:19,980
You see, we take into account the yeah, the term with a minus two h minus one h positive one h and

29
00:02:19,980 --> 00:02:21,090
positive to h.

30
00:02:21,600 --> 00:02:26,820
So this is like a bit of a central difference this method, but even more refined because we take into

31
00:02:26,820 --> 00:02:28,740
account even the further terms.

32
00:02:29,430 --> 00:02:38,790
But you also see that in this weighted sum here, the the terms with it x minus h index plus h have

33
00:02:38,790 --> 00:02:42,300
a much higher weight than the terms with the two h.

34
00:02:43,260 --> 00:02:48,870
But anyway, let's defined as richesse and function and see how it performs compared to our central

35
00:02:48,870 --> 00:02:50,370
differences methods.

36
00:02:51,300 --> 00:02:56,820
So we write and define and then I just give it a name and I would call the D1 Richardson.

37
00:02:57,270 --> 00:02:59,670
You could, of course, give it a different name as well.

38
00:03:00,660 --> 00:03:03,090
And now we must find some arguments.

39
00:03:03,480 --> 00:03:05,310
Of course, we need the function itself.

40
00:03:05,760 --> 00:03:09,360
We need the argument and we need the step size.

41
00:03:11,010 --> 00:03:17,070
So let me write this down just in case we forget that later on after will be the function X will be

42
00:03:17,070 --> 00:03:18,690
the argument.

43
00:03:18,830 --> 00:03:21,150
So these here are just comments, of course.

44
00:03:22,050 --> 00:03:23,550
So you could leave them out.

45
00:03:23,550 --> 00:03:29,190
But it's always helpful to have something like this in your notebook so that later on you remember what

46
00:03:29,190 --> 00:03:30,300
these arguments mean.

47
00:03:31,350 --> 00:03:36,330
And then our function should just return a number and the number is given by this equation.

48
00:03:36,990 --> 00:03:43,650
So it's pretty easy to implement which is the right one over 12 h, and then we must write down these

49
00:03:43,650 --> 00:03:44,490
four terms.

50
00:03:44,940 --> 00:03:48,150
So the first term will be f of X minus two h.

51
00:03:48,750 --> 00:03:56,550
Then we have another term, which is minus eight times half of x minus h.

52
00:03:57,030 --> 00:04:07,530
Then we have plus eight times of x plus h, and then the last term will be minus one times f of x minus

53
00:04:07,530 --> 00:04:09,120
two times h.

54
00:04:10,650 --> 00:04:13,230
And so this is our Richardson formula.

55
00:04:13,710 --> 00:04:19,230
And now we can compare how this method performs compared to the other methods.

56
00:04:19,740 --> 00:04:24,600
So I will just copy this one here and.

57
00:04:27,400 --> 00:04:30,120
And another plot, so I will just add the plot.

58
00:04:31,480 --> 00:04:35,110
First of all, we must create a list, of course, so I write Richardson.

59
00:04:35,590 --> 00:04:40,150
The list is given by the one Richardson.

60
00:04:40,450 --> 00:04:45,700
Our new function and the function will be after the one that we have to find previously.

61
00:04:46,090 --> 00:04:52,150
X will be our list of X coordinates and H will be h, which was zero point one that we have to find

62
00:04:52,150 --> 00:04:52,870
previously.

63
00:04:53,950 --> 00:05:02,470
And now we can add another plot here where we subtract this new list from the analytical solution and

64
00:05:02,470 --> 00:05:03,940
for the color, it's just black.

65
00:05:05,860 --> 00:05:13,090
And so here we see that there seems to be a problem and an arrow because the newly added function the

66
00:05:13,090 --> 00:05:17,950
Richardson method gives the largest arrow, which is of course not what we expect.

67
00:05:18,640 --> 00:05:20,430
So let's check where it is the error.

68
00:05:20,440 --> 00:05:22,930
So this one is correct.

69
00:05:23,560 --> 00:05:30,160
And see here I wrote accidentally a minus two h, but it's a plus to age, of course.

70
00:05:30,970 --> 00:05:32,860
So let's redefine and let's rerun it.

71
00:05:33,340 --> 00:05:38,080
And we see this time the black function is almost equal to zero.

72
00:05:38,980 --> 00:05:44,290
So we know that these first two functions to forward and backward method, they are not so accurate.

73
00:05:44,290 --> 00:05:48,100
So let's at them out so that we only have green and black.

74
00:05:48,550 --> 00:05:51,490
And you see, black is a much, much better.

75
00:05:51,700 --> 00:06:00,640
So for green, we have an aura of the power of 10 to the minus two, and for green we have an error

76
00:06:00,640 --> 00:06:02,200
of 10 to the minus five.

77
00:06:02,770 --> 00:06:05,860
So it's approximately 1000 times better.

78
00:06:07,000 --> 00:06:13,750
And this is, of course, because here we did not only consider the neighboring points, but also the

79
00:06:13,750 --> 00:06:14,770
second neighbors.

80
00:06:15,430 --> 00:06:23,170
And so this allowed us to construct a superposition that has more of these error terms cancel out and

81
00:06:23,170 --> 00:06:26,110
therefore gives us a much better accuracy.

82
00:06:27,040 --> 00:06:33,130
And an even better accuracy can, of course, be achieved if we take more and more neighbors into account.

83
00:06:33,700 --> 00:06:40,870
So that in adding several of these Taylor expansions, more and more of these error terms will cancel

84
00:06:40,870 --> 00:06:46,450
out and then the error will have a higher and higher order and the result will be better and better.

85
00:06:47,080 --> 00:06:55,300
And this is what is called the richest in interpolation or rich iteration formula.

86
00:06:56,140 --> 00:07:02,350
And so the iteration formula is as every iteration formula based on the previous results.

87
00:07:02,350 --> 00:07:05,890
So you repeat this process over and over again.

88
00:07:06,760 --> 00:07:13,960
So we start from D1, which is the result of our D1 Richardson given by this equation.

89
00:07:14,470 --> 00:07:19,870
And then we can improve the results by calculating D2, according to this equation.

90
00:07:20,770 --> 00:07:24,760
So in this case, it would be and would be one.

91
00:07:24,850 --> 00:07:29,960
So we would have here two to the power of two, which is four than we have here.

92
00:07:29,960 --> 00:07:33,040
Our previous results for the step with H.

93
00:07:33,460 --> 00:07:36,610
Then we have here our previous results for the step with two h.

94
00:07:37,060 --> 00:07:40,030
And here we have four minus one, which is three.

95
00:07:41,170 --> 00:07:47,650
And then we can iterated another time that we can go to the third order term and we can repeat this

96
00:07:47,650 --> 00:07:49,060
as many times as we want.

97
00:07:49,060 --> 00:07:55,420
And the result will get better and better because more of these terms in its halo expansion will cancel

98
00:07:55,420 --> 00:07:55,750
out.

99
00:07:56,830 --> 00:08:05,650
So I want to show you how we can, how we can implement such an iteration formula in our Python notebook.

100
00:08:06,580 --> 00:08:12,730
And of course, here it's a bit tricky because you see we have here the previous term for Step with

101
00:08:12,730 --> 00:08:17,140
H and here, the previous term for step was to H.

102
00:08:18,280 --> 00:08:25,180
So if we go to the second order, so which would be here an equal one so that we have here two, then

103
00:08:25,180 --> 00:08:31,240
we would need D1 at h and we want it to h if we go one step further.

104
00:08:31,240 --> 00:08:38,950
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

105
00:08:38,950 --> 00:08:40,630
two to, uh, two h.

106
00:08:41,679 --> 00:08:48,640
And for the two of two h, we would need D one of two h and the one for H.

107
00:08:49,480 --> 00:08:56,440
So you see, we need more and more First Order terms, the higher order we have, the higher order we

108
00:08:56,440 --> 00:08:57,280
want to consider.

109
00:08:58,390 --> 00:09:02,890
So let me just maybe show you the solution or my idea how I would implement it.

110
00:09:03,340 --> 00:09:06,190
So of course, we'll be a bit similar to our D one Richardson.

111
00:09:06,190 --> 00:09:10,930
So I ride to D1 and Richardson, and we need to define.

112
00:09:10,930 --> 00:09:11,800
Here's some order.

113
00:09:13,360 --> 00:09:25,950
So this would be the order not of the derivative, but order of iteration for first degree.

114
00:09:26,040 --> 00:09:26,550
Of it.If.

115
00:09:28,050 --> 00:09:35,480
So this time, we will not return this command here, but we will store this in some intermediate value.

116
00:09:35,850 --> 00:09:37,350
So maybe something like this.

117
00:09:38,550 --> 00:09:46,080
However, I also told you that we do not only need the first auditorium at age, but also at 2H.

118
00:09:46,500 --> 00:09:50,670
And if we go to a higher order, we even need it for age and then at eight h.

119
00:09:51,420 --> 00:09:52,980
So this depends on the order.

120
00:09:53,910 --> 00:10:01,620
So for our zero, I will define an array and this array will of course, be given by this term.

121
00:10:02,010 --> 00:10:03,990
But here we multiply.

122
00:10:03,990 --> 00:10:12,240
Or basically we replace all of the ages by age times two to the power of J.

123
00:10:13,290 --> 00:10:17,750
So let me do this right here at times two to the power of J.

124
00:10:18,930 --> 00:10:20,610
And then I do this for every age.

125
00:10:24,930 --> 00:10:36,280
And the value of G goes for J in range from zero to end.

126
00:10:36,300 --> 00:10:41,940
So this, of course, depends on the order of iteration that we want to consider.

127
00:10:44,220 --> 00:10:45,720
So this is now an array.

128
00:10:46,140 --> 00:10:47,510
So of course, what?

129
00:10:47,520 --> 00:10:50,160
Maybe then it becomes more clear what I want to say here.

130
00:10:50,640 --> 00:10:57,480
Of course, we could have also written down here is that the zero is just such an empty dot array for

131
00:10:57,480 --> 00:10:58,590
j and range.

132
00:10:59,610 --> 00:11:05,250
But the entry itself is just given by the Yvonne Richardson.

133
00:11:07,810 --> 00:11:15,520
F comma, x comma and then h will be modified by H Times two to the power of G.

134
00:11:16,630 --> 00:11:23,740
But the thing is, I do not really like to call functions inside other functions because maybe at some

135
00:11:23,740 --> 00:11:28,480
point we want to copy this to a different notebook and then we must remember that we also copy this

136
00:11:28,480 --> 00:11:29,000
function.

137
00:11:29,620 --> 00:11:36,520
So I think it's better to just use this more general approach here, even though it looks a bit more

138
00:11:36,520 --> 00:11:37,060
difficult.

139
00:11:37,300 --> 00:11:41,020
But this is just the same thing that we have written down in the Richardson function.

140
00:11:42,280 --> 00:11:42,730
All right.

141
00:11:42,730 --> 00:11:44,440
So this will be our starting point.

142
00:11:44,890 --> 00:11:54,210
So to say our D one and then to calculate D two, so which means an equal one, we need to do the following

143
00:11:54,220 --> 00:12:00,340
we need to update this we need to see I right.

144
00:12:00,340 --> 00:12:12,790
It's already in such a loop here for I in range one to NW, we do the following just for testing purposes,

145
00:12:12,790 --> 00:12:19,900
I would say we print, first of all, d zero so that we see the value at the current stage of iteration.

146
00:12:20,350 --> 00:12:26,920
And then we update the value of D um, which we will, by the way, further out outputs later.

147
00:12:27,220 --> 00:12:27,790
So.

148
00:12:28,520 --> 00:12:33,280
So return D will be this thing that we do at the very end.

149
00:12:33,730 --> 00:12:37,600
But now we update to B and P dot array.

150
00:12:40,880 --> 00:12:49,730
And here we were at the command, which would basically be this equation here for Jay can range.

151
00:12:50,570 --> 00:12:55,460
And now basically we must reduce the length with every iteration.

152
00:12:55,460 --> 00:12:59,180
So I just try to length D0 minus one.

153
00:13:00,170 --> 00:13:01,970
And here we write them this equation.

154
00:13:02,300 --> 00:13:10,580
So there will be two to the power of two times.

155
00:13:11,960 --> 00:13:21,200
I yes, because um yeah, maybe I should have called it a bit differently.

156
00:13:21,530 --> 00:13:25,400
So maybe you let me do it like this.

157
00:13:26,420 --> 00:13:28,790
I hope this is sort of it's less confusing to you.

158
00:13:29,090 --> 00:13:35,450
I called the order M or I could also call it, and max and Max is good.

159
00:13:37,840 --> 00:13:42,130
And here and we have to replace this one with and Max.

160
00:13:42,910 --> 00:13:50,200
And here it goes to and Max and then here we can use and so that we have the same one as here.

161
00:13:51,190 --> 00:13:54,280
So then we can ride like this.

162
00:13:54,610 --> 00:14:01,060
Yeah, I like this much better so that we can vote two to the power of two end times.

163
00:14:02,260 --> 00:14:05,950
The solution of the for argument h.

164
00:14:06,250 --> 00:14:15,010
So this would be a D0 and Jayco component.

165
00:14:17,200 --> 00:14:26,110
So of course, for the first element of the list of the NRA, this will be zero zero, which will be

166
00:14:26,110 --> 00:14:29,320
the first term here, which will be the end of h.

167
00:14:30,340 --> 00:14:43,420
And then we rides minus D zero j plus one and then we divide by the denominator.

168
00:14:44,110 --> 00:14:49,950
So this will be two to the power of two times J.

169
00:14:53,230 --> 00:14:54,640
Two times and sorry.

170
00:14:55,300 --> 00:14:56,500
And then the line is one.

171
00:14:59,120 --> 00:14:59,780
Exactly.

172
00:14:59,780 --> 00:15:01,300
So now we have Jay here.

173
00:15:01,570 --> 00:15:09,260
Iterator for the index of the zero and An, which is the looping index for this and from this equation,

174
00:15:10,100 --> 00:15:11,390
I think this looks good.

175
00:15:11,390 --> 00:15:16,940
And of course, then we have to update our D0 with the current D.

176
00:15:18,150 --> 00:15:21,440
So when I run the function, I think you will see what happens here.

177
00:15:22,100 --> 00:15:23,800
So I hope I didn't make a typo.

178
00:15:23,810 --> 00:15:26,240
This would be bad, but let's see what happens.

179
00:15:26,480 --> 00:15:29,000
So I just call the function one.

180
00:15:29,000 --> 00:15:35,660
And Richardson and Max let's, I don't know, use five f will be our function from previously and I

181
00:15:35,660 --> 00:15:38,240
want to do it at the position, let's say three

182
00:15:41,270 --> 00:15:46,130
three and I run this, OK?

183
00:15:46,340 --> 00:15:50,930
And so this these will be the intermediate results from the print command.

184
00:15:51,350 --> 00:15:58,760
So you see in the first loop, we get this result in the second loop, we get this one and the third

185
00:15:58,760 --> 00:16:01,310
loop, this one, fourth, this one and then the fifth loop.

186
00:16:01,310 --> 00:16:04,490
Finally, we get our result, which will be this value.

187
00:16:05,540 --> 00:16:09,380
And now we can compare this to the D1 Richardson.

188
00:16:10,100 --> 00:16:18,440
And for this, I will write D1 Richardson, for example.

189
00:16:18,440 --> 00:16:22,010
This value and this value corresponds to this value.

190
00:16:23,000 --> 00:16:24,170
So let me show you something.

191
00:16:25,070 --> 00:16:27,800
I think that becomes more clear what I did above.

192
00:16:28,550 --> 00:16:36,080
I will copy this five times and then I will write you two times h four times h, eight times h, 16

193
00:16:36,080 --> 00:16:36,740
times h.

194
00:16:37,910 --> 00:16:41,840
And these five values are exactly the values that we get here.

195
00:16:42,890 --> 00:16:47,500
So these are the values that are in this and P Todd's array.

196
00:16:48,830 --> 00:16:54,710
So, yeah, basically we have here this iterated J going from zero to and max.

197
00:16:55,190 --> 00:16:57,680
And so we have five terms in this array.

198
00:16:58,280 --> 00:17:05,150
And then comes the first iteration where we calculate where we update our D, according to this equation.

199
00:17:06,079 --> 00:17:11,300
And so here we calculate basically D to.

200
00:17:12,450 --> 00:17:19,710
Based on this one, and we need the data for four different values we needed for age four to age four

201
00:17:19,710 --> 00:17:21,210
four eight four eight each.

202
00:17:21,960 --> 00:17:28,710
Then comes these three and then we need the three for age to age and for age, the four we need for

203
00:17:28,710 --> 00:17:29,760
age and to age.

204
00:17:30,150 --> 00:17:32,520
And then from this, we can calculate DeFife.

205
00:17:33,870 --> 00:17:40,980
And now the only thing left to do is to see if this really is an approved is it is an improvement.

206
00:17:41,190 --> 00:17:49,380
So I have to write three times dot and P Dot Cosine, because that's the analytical solution.

207
00:17:49,650 --> 00:17:55,110
So you see, I tested this before with an argument of two, but this time I choose three, so we have

208
00:17:55,110 --> 00:17:55,890
to update this.

209
00:17:57,620 --> 00:18:02,580
Um, I'm not really sure why I did this before, but that's updated to three.

210
00:18:03,150 --> 00:18:14,820
And then let's right and p dot sign of three minus three over 100 times three squared.

211
00:18:16,720 --> 00:18:24,700
So this will be the value that we get analytically and now we can compare this value and we can just

212
00:18:24,700 --> 00:18:29,350
subtract the value from the Richardson formalism.

213
00:18:29,830 --> 00:18:40,000
So when I called his one and then the output from the return disappears and will instead be stored here.

214
00:18:40,600 --> 00:18:49,540
And so then we can subtract a two year deal on end, and we see the error with respect to the analytical

215
00:18:49,540 --> 00:18:51,610
solution would be a very, very small.

216
00:18:51,610 --> 00:18:55,810
It will be to tend to the power of minus nine.

217
00:18:55,810 --> 00:19:01,780
And previously from the first or two terms, we had an error of 10 to the power of minus five.

218
00:19:03,070 --> 00:19:10,900
So this was just to show you that there are methods to calculate the derivative much more accurately.

219
00:19:11,530 --> 00:19:17,500
And the trick is to combine more and more terms and further and further nearest neighbors for these

220
00:19:17,500 --> 00:19:18,670
individual data points.

221
00:19:19,030 --> 00:19:22,360
So it had several hours and a Taylor function cancel out.

222
00:19:23,320 --> 00:19:28,600
Of course, in our course, we will not always do this difficult stuff here.

223
00:19:28,600 --> 00:19:34,390
We will not always want to have such a precise derivative.

224
00:19:35,020 --> 00:19:39,070
Most often, we will just use the central difference as methods.

225
00:19:39,190 --> 00:19:44,140
Well, we'll just use the left and the right neighbors and calculate the derivative from here.

226
00:19:44,890 --> 00:19:48,010
And maybe sometimes we need a bit of a better accuracy.

227
00:19:48,010 --> 00:19:49,750
So we take the Richardson formula.

228
00:19:50,740 --> 00:19:57,790
But if you want to have a really precise derivative, then this is possible and you can take this iteration

229
00:19:57,790 --> 00:19:58,360
formula.

230
00:19:58,690 --> 00:20:01,870
And of course, here we have taken and max equal to five.

231
00:20:02,110 --> 00:20:07,930
But of course, you could take year 100 and then the derivative will be more and more precise.

232
00:20:09,010 --> 00:20:13,260
But most of the time, it's really a question is it worth it?

233
00:20:13,270 --> 00:20:16,330
Because you see to calculate this one derivative?

234
00:20:16,720 --> 00:20:22,330
We had to calculate here five plus four plus three plus two plus one values to finally have it.

235
00:20:23,110 --> 00:20:29,710
So you really have to think, is it worth it to spend the time to calculate the derivative more precisely?

236
00:20:29,710 --> 00:20:33,670
Or is it OK to just stick with the central differences?

237
00:20:34,090 --> 00:20:38,020
And then most of the cases, it is OK to just use central differences.

