1
00:00:00,150 --> 00:00:02,310
Now, do we know what a derivative is?

2
00:00:02,580 --> 00:00:05,730
Let us discuss how we can define this in Python.

3
00:00:06,870 --> 00:00:11,610
So we basically have to implement one of these three equations.

4
00:00:11,910 --> 00:00:15,960
But the problem is that we have this limit h goes to zero.

5
00:00:16,440 --> 00:00:21,150
And of course, in numerics, the H can never really go to zero.

6
00:00:21,570 --> 00:00:27,810
And also, it could happen that we have some great some data points, some X coordinate, some Y coordinates,

7
00:00:28,170 --> 00:00:31,620
and we do not really know what is in between these data points.

8
00:00:32,430 --> 00:00:38,040
So of course, in this case, we could first to add interpolation and we could interpolate the function

9
00:00:38,040 --> 00:00:40,260
to have access to the points in between.

10
00:00:40,800 --> 00:00:46,950
But also, it's quite convenient to just be able to calculate the derivative based on the points that

11
00:00:46,950 --> 00:00:47,430
we have.

12
00:00:48,120 --> 00:00:52,410
So what this means, basically, is that h will have some fixed value.

13
00:00:52,410 --> 00:00:58,530
It will be the difference between two points or the difference of the X coordinates of the two points.

14
00:00:58,950 --> 00:01:02,550
And then here we will have the difference of the Y coordinates of the two points.

15
00:01:03,420 --> 00:01:05,400
And this is exactly what I have written down here.

16
00:01:05,850 --> 00:01:13,860
In a good approximation, we can write down that the derivative of F at some point and is given by the

17
00:01:13,860 --> 00:01:17,940
difference of the Y coordinates divided by the difference of the X coordinates.

18
00:01:18,840 --> 00:01:24,360
And as discussed previously, we can do this in terms of forward differences, backward differences

19
00:01:24,360 --> 00:01:26,130
and also central differences.

20
00:01:27,210 --> 00:01:34,590
And in this case, since we don't have the limit h goes to zero, all three methods can give a different

21
00:01:34,590 --> 00:01:35,100
result.

22
00:01:35,790 --> 00:01:37,620
And this is what we will explore first.

23
00:01:37,650 --> 00:01:45,810
We will implement these three methods and we will investigate how the derivatives will look different

24
00:01:46,440 --> 00:01:49,170
for the three methods for this example, function.

25
00:01:49,890 --> 00:01:55,960
We have chosen a bit of a more difficult function so that we can really see some maxima and minima and

26
00:01:55,960 --> 00:01:56,490
the function.

27
00:01:56,490 --> 00:02:01,980
It's sine function multiplied by X and then some third order polynomial function.

28
00:02:03,180 --> 00:02:07,920
So as always, we will first import Nampai and much plot lib.

29
00:02:08,400 --> 00:02:13,560
So let me run the cell and then I will first define our function.

30
00:02:14,280 --> 00:02:21,600
So I write def half of X, and of course, the output will be a return function and it will just return

31
00:02:21,600 --> 00:02:22,740
to value of the function.

32
00:02:22,750 --> 00:02:32,400
So I write and pitot sine of X and then times X, and then the other turn will be minus one over 100

33
00:02:33,090 --> 00:02:35,580
times X to the power of three.

34
00:02:36,930 --> 00:02:45,060
So this is our function, which is now defined and now we can continue and we can, yeah, have a look

35
00:02:45,060 --> 00:02:45,780
at the function.

36
00:02:46,500 --> 00:02:52,710
So for this, I will write X list over the final list of X coordinates flyer and P total in space.

37
00:02:53,940 --> 00:03:03,750
And for the plot range, I've chosen minus 10 to 10 and I will use 201 points because by doing so,

38
00:03:04,650 --> 00:03:08,250
the difference between each of these points will be zero point one.

39
00:03:09,630 --> 00:03:11,690
So we have now our X list.

40
00:03:12,030 --> 00:03:20,400
I can define Y list, so this will be, you know, just f our function.

41
00:03:20,400 --> 00:03:22,470
And then the argument will be D X list.

42
00:03:23,190 --> 00:03:28,740
And since this is here and every since I wrote and put model in space, we can apply the function to

43
00:03:28,740 --> 00:03:30,900
every element by writing it down like this.

44
00:03:32,040 --> 00:03:33,690
And so now we are almost finished.

45
00:03:33,690 --> 00:03:43,290
We can just write pal T dot plot and X List Comma Y list, and then we can add some labels for the XS

46
00:03:43,290 --> 00:03:50,730
Plus Tot X label, which will be X and Y label will be F of X.

47
00:03:54,760 --> 00:03:57,940
And we run the cell and have a look at our plot.

48
00:03:59,440 --> 00:04:05,410
So now you can see here, it's really a pretty complicated plot with several maxima and minima.

49
00:04:05,830 --> 00:04:12,970
And in the next step, we are going to calculate the derivative according to these three equations here.

50
00:04:14,650 --> 00:04:22,600
So what we do next is we, of course, take our X list, so it's not really necessary to define it once

51
00:04:22,600 --> 00:04:22,930
again.

52
00:04:22,930 --> 00:04:26,290
But here we'll do it anyway, just in case we change something later on.

53
00:04:26,800 --> 00:04:32,200
And then I will, first of all, calculate the derivative analytically.

54
00:04:32,890 --> 00:04:35,420
So analytical the list.

55
00:04:35,950 --> 00:04:37,920
So for derivative.

56
00:04:40,000 --> 00:04:44,390
And so our function is has two parts.

57
00:04:44,410 --> 00:04:50,020
So in X times x and then this polynomial, the derivative of the polynomial is, of course, very simple.

58
00:04:50,020 --> 00:04:55,330
It's just three times one over 100 times x squared.

59
00:04:55,780 --> 00:05:00,550
And then here for this function, since it's a product, we must use the product rule and we get two

60
00:05:00,550 --> 00:05:01,780
terms for the derivative.

61
00:05:02,230 --> 00:05:08,200
The first one will be cosine times X because the derivative of sine as cosine.

62
00:05:08,680 --> 00:05:16,330
And then the other term will be just sine x times one because the derivative of X is one.

63
00:05:17,470 --> 00:05:19,780
So we will have three terms in total.

64
00:05:20,200 --> 00:05:27,040
This will be an impedance cosine of X lists times x list.

65
00:05:29,170 --> 00:05:34,750
Then the other term will be A. Dots sine of X list times of one.

66
00:05:35,710 --> 00:05:44,740
And then the last term will be minus three times one of 100 times X lists square.

67
00:05:46,090 --> 00:05:48,250
And of course, now we can plot this.

68
00:05:48,520 --> 00:05:53,200
So let's just copy the plot command and change this list here.

69
00:05:54,010 --> 00:05:56,140
And let's right here derivative.

70
00:05:58,300 --> 00:05:59,680
And I did the mistake.

71
00:05:59,860 --> 00:06:01,840
This is oh no, not really a mistake.

72
00:06:01,840 --> 00:06:09,010
But this is, of course, because for the dash here, for the derivative, we cannot use the same symbol.

73
00:06:09,010 --> 00:06:13,670
So let me replace two symbols that let me write it down like this.

74
00:06:13,930 --> 00:06:14,710
Then it should work.

75
00:06:16,000 --> 00:06:16,420
OK.

76
00:06:17,410 --> 00:06:21,760
So the resolution is not really that good in that plot, but I think I think it's sufficient to just

77
00:06:21,760 --> 00:06:22,540
do it like that.

78
00:06:23,290 --> 00:06:28,360
So you see, we have a derivative function, which also has several maxima and minima.

79
00:06:28,720 --> 00:06:34,090
And for example, when the function has a maximum, it goes through zero here.

80
00:06:35,140 --> 00:06:35,560
All right.

81
00:06:35,570 --> 00:06:36,880
So it seems to be correct.

82
00:06:37,300 --> 00:06:41,140
And now we can also add the numerical derivatives.

83
00:06:41,920 --> 00:06:50,980
And for this, I will first of all, define in h value, which will be zero point one so that it corresponds

84
00:06:50,980 --> 00:06:53,880
basically to the data points in our list.

85
00:06:53,950 --> 00:07:00,760
So this h two step size of zero point one is the same difference between two points in this and p total

86
00:07:00,760 --> 00:07:02,380
in space of our X list.

87
00:07:03,340 --> 00:07:08,140
And now I can define a list, for example, for what D list?

88
00:07:09,400 --> 00:07:11,590
And now I must

89
00:07:14,260 --> 00:07:15,820
define it in this way.

90
00:07:15,830 --> 00:07:19,480
So we take the value of the function at the next point.

91
00:07:19,900 --> 00:07:23,260
Minus the values value of the function at the current point.

92
00:07:23,560 --> 00:07:26,200
Divide it by H, basically.

93
00:07:27,070 --> 00:07:31,150
So I write down the value of the function.

94
00:07:33,650 --> 00:07:48,590
At the Find X list plus age minus, well, you have to function at X list and then we divide the whole

95
00:07:48,590 --> 00:07:50,120
thing by H.

96
00:07:52,640 --> 00:07:59,180
So the next thing that we can do is backwards differences here we take the value of the function minus

97
00:07:59,180 --> 00:08:04,340
the value of the function at the previous point, and then we will also program the central differences,

98
00:08:04,340 --> 00:08:11,570
which will be the difference of the following point and the previous point divided by 2H this time.

99
00:08:12,980 --> 00:08:14,210
So let's program it.

100
00:08:15,770 --> 00:08:16,310
Here we are.

101
00:08:16,760 --> 00:08:24,890
So I will just copy this line twice, so I don't have to type so much and I will write backward and

102
00:08:25,010 --> 00:08:25,730
central.

103
00:08:28,290 --> 00:08:33,900
And then here we said we'd take the current point minus the previous point, divided by age.

104
00:08:34,440 --> 00:08:41,100
And here we said we take the following point minus the previous point divided by two h.

105
00:08:42,990 --> 00:08:43,409
All right.

106
00:08:43,409 --> 00:08:49,200
So now we have created these three lists characterizing derivatives calculated by three different methods.

107
00:08:49,410 --> 00:08:57,450
And now we can add these plots, and I just have to exchange the names for the lists.

108
00:08:59,080 --> 00:09:06,640
So let me do this takes a few seconds, and I hope you also program along with me, and when I run this,

109
00:09:07,900 --> 00:09:10,390
we basically see not a big difference.

110
00:09:10,570 --> 00:09:15,070
I mean, we can still see here red blow to green blood, maybe a blue plot in the background.

111
00:09:15,340 --> 00:09:21,490
So there is some small difference comparing the three plots, but the difference is quite small.

112
00:09:23,170 --> 00:09:29,140
But of course, now on the following we want to analyze this in more detail.

113
00:09:29,920 --> 00:09:34,270
And for this, I want to analyze now the arrow.

114
00:09:35,290 --> 00:09:43,240
So I will copy the plot function and I will write for the y axis out of the derivative.

115
00:09:44,230 --> 00:09:55,360
And so what we do is we take this list analytical list as a reference point and we will subtract the

116
00:09:55,360 --> 00:09:57,520
other lists from these reference points.

117
00:09:59,170 --> 00:10:07,120
So like this, and I will delete this list because this is no our reference until we will plot only

118
00:10:07,120 --> 00:10:07,720
the difference.

119
00:10:08,650 --> 00:10:16,000
This works because we choose the same X list for all of the plots, so we cannot just calculate the

120
00:10:16,000 --> 00:10:17,230
difference of the why list.

121
00:10:18,310 --> 00:10:26,140
And now I will add some color so that we know which plot corresponds to which function blue and green.

122
00:10:27,400 --> 00:10:34,260
So when I run it, we see this is all a result.

123
00:10:34,270 --> 00:10:40,420
This is the error of the derivative of X compared to the analytical result, which we know is the correct

124
00:10:40,420 --> 00:10:40,660
one.

125
00:10:41,170 --> 00:10:47,020
And we see that basically the red and the blue function have quite a large difference.

126
00:10:47,020 --> 00:10:56,320
At some points it goes even up to 0.4, which is considerable if we consider our function as a change

127
00:10:56,320 --> 00:10:58,900
from plus seven to minus 12.

128
00:10:59,590 --> 00:11:02,470
So yeah, it is some error of a few percent.

129
00:11:02,920 --> 00:11:09,070
And what's also interesting is that we can also see that the absolute error of the forward and the backwards

130
00:11:09,070 --> 00:11:10,630
method is the same.

131
00:11:10,630 --> 00:11:11,950
It's just a different sign.

132
00:11:12,760 --> 00:11:18,970
But most importantly, the green function, which is the central method, has a much, much smaller

133
00:11:18,970 --> 00:11:19,390
arrow.

134
00:11:20,050 --> 00:11:24,640
It's much better to use this method for calculating the derivative.

135
00:11:26,080 --> 00:11:31,060
And in the following lecture, we want to explore why this is the case.

136
00:11:31,060 --> 00:11:39,160
So the name is why is the central differences methods typically better than the forwhat and the backroads

137
00:11:39,220 --> 00:11:40,390
differences methods?

