1
00:00:00,180 --> 00:00:06,270
So in a previous lecture, we have expanded the sound function into position zero is equal to zero.

2
00:00:06,990 --> 00:00:13,080
And the reason why we were only able to do it at this particular position is that we wanted to calculate

3
00:00:13,080 --> 00:00:15,120
the derivatives analytically.

4
00:00:15,270 --> 00:00:20,430
And here it is particularly easy because it's just zero minus one and plus one.

5
00:00:21,450 --> 00:00:28,410
However, if we want to do it in a more general way, then we also must be able to define or to expand

6
00:00:28,410 --> 00:00:32,400
the sine function or any other function at any given position.

7
00:00:33,000 --> 00:00:39,630
And for this to to be able to do it in general, we must define the derivative numerically and this

8
00:00:39,630 --> 00:00:41,220
is what we are going to do next.

9
00:00:42,120 --> 00:00:50,100
So first of all, we have to define the derivative, that first derivative, but also the higher derivatives.

10
00:00:50,700 --> 00:00:57,840
And I have dedicated in this course a whole section on derivatives so that we will explore this in much

11
00:00:57,840 --> 00:01:04,530
more detail and we will we will explore how you can calculate derivatives with a much higher accuracy.

12
00:01:04,920 --> 00:01:11,630
But in very simple terms, you can define a derivative by this argument here or by the way, this term,

13
00:01:11,640 --> 00:01:12,780
mathematically speaking.

14
00:01:13,200 --> 00:01:20,370
So you introduce a small number each, which will be a distance in x direction and then you take the

15
00:01:20,370 --> 00:01:25,430
difference for the Y values of this function at these two positions.

16
00:01:25,440 --> 00:01:32,070
So you take it at X plus h and an X, and then you just divide by the distance and X direction.

17
00:01:32,490 --> 00:01:38,910
And this gives you the slope of the triangle and the slope of the tangent of the function, and this

18
00:01:38,910 --> 00:01:40,260
is the first derivative.

19
00:01:41,040 --> 00:01:49,140
And we can the find this quite easily, so we can define here a function called the derivative and for

20
00:01:49,140 --> 00:01:49,770
the arguments.

21
00:01:49,770 --> 00:01:55,090
We need, of course, to function and the position, but also we must define the step size.

22
00:01:55,140 --> 00:01:57,000
So basically the value of H.

23
00:01:58,170 --> 00:02:11,520
So as I said after B, the function X is the argument of the function and H is the step size.

24
00:02:15,150 --> 00:02:18,330
And the value that we return is pretty easy.

25
00:02:18,360 --> 00:02:22,890
It's, of course, just this expression that is given here.

26
00:02:23,250 --> 00:02:30,710
So if the Position X plus each line is at the position X and then we divide the whole thing by H.

27
00:02:31,560 --> 00:02:35,790
So here in terms of mathematics, we have to do a limit of age goes to zero.

28
00:02:36,090 --> 00:02:38,340
Only then the derivative is really exact.

29
00:02:38,730 --> 00:02:40,980
But of course, in America, we cannot really do this.

30
00:02:40,980 --> 00:02:46,230
So we must say, OK, we just take a small value for h, and that's all we can do.

31
00:02:46,950 --> 00:02:52,950
Of course, as I said, we will also explore in the separate lecture how we can improve this version

32
00:02:52,950 --> 00:02:56,580
and how we can make the accuracy much, much better than what is written here.

33
00:02:56,850 --> 00:03:00,720
Now, for the higher derivative terms, it is not so easy.

34
00:03:00,990 --> 00:03:06,390
I mean, in terms of mathematics, it is pretty easy to first calculate the first derivative of the

35
00:03:06,390 --> 00:03:09,480
function F and to calculate the second derivative.

36
00:03:09,990 --> 00:03:13,770
You just calculate the same thing again, but here with the first derivative.

37
00:03:14,580 --> 00:03:20,670
So basically, you do this and iterations to calculate, for example, the third derivative, you first

38
00:03:20,670 --> 00:03:22,440
must calculate the first derivative.

39
00:03:22,920 --> 00:03:28,980
Then from here, you take this equation again with the first derivative if you calculate the second

40
00:03:28,980 --> 00:03:29,640
derivative.

41
00:03:30,090 --> 00:03:32,400
And from there, you calculate the third derivative.

42
00:03:33,420 --> 00:03:40,740
But in our case, we want to have a function that is able to instantly calculate the and derivative.

43
00:03:41,520 --> 00:03:48,540
And so you can imagine that what you have to do then if you think about iterations when you do this

44
00:03:48,570 --> 00:03:55,320
over and over again, is that for the second derivative, for example, you must calculate here the

45
00:03:55,320 --> 00:04:01,290
first derivative not only at Position X, but also at the Position X plus h.

46
00:04:02,010 --> 00:04:03,090
And this is the problem.

47
00:04:03,480 --> 00:04:09,300
To calculate the derivative at the position X plus h, you must know the.

48
00:04:11,720 --> 00:04:17,269
The the value of the function itself at the position X plus h and X plus two h.

49
00:04:18,290 --> 00:04:24,020
And so if you go to the fourth derivative for higher, then you must know the function at the Position

50
00:04:24,020 --> 00:04:27,890
X plus order of derivative times h.

51
00:04:29,030 --> 00:04:33,560
And so this is really, yeah, what's what, what we will do next year.

52
00:04:34,010 --> 00:04:41,150
And you can also imagine that when you do this, when you have several terms here and some of these,

53
00:04:41,930 --> 00:04:45,170
you can count how often they occur.

54
00:04:45,380 --> 00:04:47,570
So some of them will occur more than once.

55
00:04:47,870 --> 00:04:50,240
And this is where this term here comes from.

56
00:04:50,630 --> 00:04:56,300
And yeah, if you think about it very carefully, I think you could come up with the solution.

57
00:04:56,690 --> 00:05:01,250
But for the moment, let's just take this equation and that's try to program it.

58
00:05:01,280 --> 00:05:04,670
So this is just something that you can find, for example, on Wikipedia.

59
00:05:05,180 --> 00:05:07,220
Well, as I said, it totally makes sense.

60
00:05:07,220 --> 00:05:12,710
And if you look at it carefully, for example, if you try to first calculate the second derivative,

61
00:05:12,710 --> 00:05:16,400
then you will see how you end up with this expression.

62
00:05:17,480 --> 00:05:19,920
So it looks more difficult than it actually is.

63
00:05:19,940 --> 00:05:21,080
This is what I want to say.

64
00:05:22,100 --> 00:05:26,690
So let us program this and then we will see that it's not so bad, actually.

65
00:05:27,320 --> 00:05:33,980
So we can just go ahead and calculate to find a function called and derivative.

66
00:05:35,240 --> 00:05:42,380
And this time we need the function to position the step, size and of course, also the order.

67
00:05:42,530 --> 00:05:50,120
And so has previously the first three arguments are the same as for the first derivative, and then

68
00:05:50,120 --> 00:05:55,100
the end will be the basically the end derivative.

69
00:05:56,990 --> 00:06:01,340
And then we have to add up terms because we have the same here.

70
00:06:01,970 --> 00:06:08,120
So once again, we do it similarly to our Taylor expansion and we create such a loop where we can,

71
00:06:08,120 --> 00:06:17,060
for example, take a for loop and we have a running index case starting at zero and going to the value

72
00:06:17,060 --> 00:06:17,480
end.

73
00:06:18,110 --> 00:06:21,440
And we include and so therefore we write and plus one here.

74
00:06:22,790 --> 00:06:28,400
And then in the end, we return for value of T.

75
00:06:28,670 --> 00:06:30,530
So T will be updated here.

76
00:06:30,530 --> 00:06:33,380
T is equal to T plus some terms.

77
00:06:33,980 --> 00:06:35,990
And we will return the value of T.

78
00:06:37,100 --> 00:06:40,700
And you can see here that this term is outside of the sum.

79
00:06:41,030 --> 00:06:44,180
So of course, I could put it inside of the sum and write it here.

80
00:06:44,450 --> 00:06:50,810
But it saves a bit of calculation time too, just after summing up the terms to divide by what is written

81
00:06:50,810 --> 00:06:51,130
here.

82
00:06:51,140 --> 00:06:53,570
So this will be h to the power of end.

83
00:06:54,590 --> 00:07:01,060
So no, we have to just program this term, so it is actually not so difficult.

84
00:07:01,070 --> 00:07:09,620
So we just have to write minus oops minus one to the power of and or no, it's not, and it's close.

85
00:07:09,620 --> 00:07:15,350
And then we have this fraction of these factorial.

86
00:07:15,650 --> 00:07:26,690
So we have then p dots, math dot factorial off and divided by and then let me copy this because it's

87
00:07:26,690 --> 00:07:28,100
quite a lot to write.

88
00:07:28,580 --> 00:07:35,570
We divide by Factorial K times factorial of and minus K.

89
00:07:37,310 --> 00:07:40,040
And then we are only left to the function.

90
00:07:40,040 --> 00:07:46,670
So we write F 50 Argument X Plus K Times page.

91
00:07:47,990 --> 00:07:50,270
And I think this is already it.

92
00:07:51,470 --> 00:07:56,990
So to test the whole thing, I will first test the derivative and then the higher derivative.

93
00:07:56,990 --> 00:08:01,730
So I will define here a function which will be a test function.

94
00:08:02,330 --> 00:08:13,160
So the function will, for example, be called two times sine of Tax Square.

95
00:08:13,400 --> 00:08:18,980
So let's just use something a bit more difficult that we cannot so easily calculate analytically.

96
00:08:19,400 --> 00:08:24,470
I mean, it would definitely be possible to calculate here easily the first, second, third derivative.

97
00:08:24,920 --> 00:08:28,610
But if you want to calculate the tenth derivative, it's pretty annoying, of course.

98
00:08:29,540 --> 00:08:30,770
So we can do it like this.

99
00:08:32,090 --> 00:08:38,679
So we have our function and now we can calculate, for example, at the position x zero is equal to

100
00:08:38,690 --> 00:08:40,820
ten point five the value that we had before.

101
00:08:41,360 --> 00:08:49,940
We can now calculate the value of the function, which was twelve point something, and we can calculate

102
00:08:49,940 --> 00:08:56,930
the first derivative of F, which we have called function func.

103
00:08:57,560 --> 00:09:00,920
And then X is zero and then we have to define some h value.

104
00:09:01,400 --> 00:09:07,220
So here I will just take, you know, I don't know, zero point one.

105
00:09:08,750 --> 00:09:09,830
So like this.

106
00:09:10,860 --> 00:09:12,670
And I get an error.

107
00:09:18,260 --> 00:09:25,310
Let me see what is the problem here, name derivative is not defined.

108
00:09:25,490 --> 00:09:26,540
Maybe I didn't run it.

109
00:09:27,440 --> 00:09:27,830
Let's see.

110
00:09:28,460 --> 00:09:28,790
OK.

111
00:09:28,860 --> 00:09:30,090
Yeah, I just forgot to run it.

112
00:09:30,110 --> 00:09:31,160
So, yeah, no problem.

113
00:09:31,580 --> 00:09:32,180
Now it works.

114
00:09:32,630 --> 00:09:35,270
So we have now our function, our derivative.

115
00:09:35,340 --> 00:09:37,610
No, we can test a higher order derivative.

116
00:09:38,150 --> 00:09:46,550
And we can basically copy this year and write and derivative of function x zero h.

117
00:09:46,550 --> 00:09:47,240
And then we need.

118
00:09:48,440 --> 00:09:53,420
And we can say and will be the and the river type.

119
00:09:53,720 --> 00:09:57,500
So if we take a zero, then we should get the actual value of the function.

120
00:09:58,010 --> 00:09:59,030
So I will write zero.

121
00:10:00,410 --> 00:10:02,960
This gives us twelve point zero four.

122
00:10:03,590 --> 00:10:05,240
Then we can write one.

123
00:10:07,130 --> 00:10:09,740
This gives us the first derivative, which is this one.

124
00:10:09,740 --> 00:10:10,490
So it worked.

125
00:10:10,970 --> 00:10:14,660
And now, of course, we could just continue and check out some values.

126
00:10:14,660 --> 00:10:16,810
But yeah, here we don't know if it's correct.

127
00:10:16,820 --> 00:10:21,170
But since the first two terms are correct, I have high hopes that this works well.

128
00:10:22,490 --> 00:10:22,960
All right.

129
00:10:22,970 --> 00:10:24,590
But to really know if it works.

130
00:10:24,770 --> 00:10:35,000
Let us define the general function for the Taylor expansion and then plot the function two times sine

131
00:10:35,000 --> 00:10:36,020
squared plus x.

132
00:10:36,350 --> 00:10:40,730
And then that is also plot the Taylor expansion and this we will do in the next lecture.

