1
00:00:00,270 --> 00:00:06,330
So we spent our last lecture in defining the general form of the derivative.

2
00:00:06,810 --> 00:00:12,840
So now we are able to numerically calculate the derivative of any function, not only the first derivative

3
00:00:13,200 --> 00:00:14,640
but the derivative.

4
00:00:15,420 --> 00:00:22,770
So now we can go ahead and define the Taylor expansion so we can right now def Taylor.

5
00:00:23,070 --> 00:00:26,670
And this time it will really be a general Taylor from the river.

6
00:00:26,940 --> 00:00:33,270
Sorry, a Taylor expansion of any function and up to and or there.

7
00:00:33,270 --> 00:00:37,410
And yeah, really the most general thing you can do for Taylor expansions.

8
00:00:37,920 --> 00:00:40,710
And so now we need quite a lot of arguments.

9
00:00:41,400 --> 00:00:50,640
So of course we need f, we need X, we need X0 Then we need and max the index word a series will terminate.

10
00:00:51,150 --> 00:00:55,680
And then also we need the value h because we have the derivatives here.

11
00:00:56,820 --> 00:01:01,980
So F X and zero are the same as we had before.

12
00:01:02,490 --> 00:01:04,200
So like, for example, here.

13
00:01:08,850 --> 00:01:10,380
So I will copy this.

14
00:01:10,410 --> 00:01:18,630
These are just the comments for the arguments and f will be the function, of course, and age will

15
00:01:18,630 --> 00:01:20,340
be what we have written here.

16
00:01:20,370 --> 00:01:21,240
The step size.

17
00:01:24,860 --> 00:01:32,480
And now we can go ahead and we can basically just program the very first equation in our notebook,

18
00:01:32,480 --> 00:01:34,280
so let me briefly scroll up here.

19
00:01:34,760 --> 00:01:41,420
So this equation we have to program and for this derivative here, we have just defined our general

20
00:01:41,420 --> 00:01:41,890
function.

21
00:01:41,900 --> 00:01:43,940
So we will of course, call this function now.

22
00:01:45,080 --> 00:01:52,160
So I will scroll down back to the Taylor code and once again, we have a sum, so I will once again

23
00:01:52,160 --> 00:01:56,870
do it in a loop, even though this is not really the most elegant way of doing it.

24
00:01:56,870 --> 00:02:01,640
But I think it's very instructive and you understand much better what is going on.

25
00:02:02,810 --> 00:02:11,870
And now we, of course, as previously add up the values and then we will return the value of teeth.

26
00:02:12,830 --> 00:02:14,660
So now we have to program the terms.

27
00:02:14,660 --> 00:02:18,620
And if you don't remember, you can scroll up in your notebook once again.

28
00:02:18,620 --> 00:02:21,860
But I remember we have to calculate the derivative.

29
00:02:23,030 --> 00:02:27,110
So now you just have to be careful to use the correct arguments here.

30
00:02:27,350 --> 00:02:36,200
So we have function acts, age and so function will, of course, be the same as here then.

31
00:02:39,090 --> 00:02:46,650
We have zero than we have each, and we have.

32
00:02:46,840 --> 00:02:55,830
And so here you have to be careful that you don't use and max because and Max is the index in which

33
00:02:55,830 --> 00:02:57,180
the series will terminate.

34
00:02:57,720 --> 00:03:01,810
But here we have end, which is the index over which we add.

35
00:03:01,830 --> 00:03:04,020
So I will just briefly scroll up one more time.

36
00:03:04,230 --> 00:03:10,350
So here you see the indexes and really so yeah, like this should be correct.

37
00:03:10,740 --> 00:03:17,190
And then we calculate the difference here x minus x zero, which is the position at which we calculate

38
00:03:17,190 --> 00:03:21,150
the derivative and we calculate this to the power of RN.

39
00:03:21,510 --> 00:03:27,030
And then we are only missing the factorial, which is given like this.

40
00:03:28,230 --> 00:03:33,660
So factorial of and and now I have a problem because I forgot this one.

41
00:03:34,230 --> 00:03:37,380
And now it could work.

42
00:03:37,390 --> 00:03:37,890
Let's check.

43
00:03:37,910 --> 00:03:39,540
At least we don't get an error message.

44
00:03:40,530 --> 00:03:47,100
So I said already, I want to plot something here, so I want to make a plot pretty similar to this

45
00:03:47,100 --> 00:03:47,300
one.

46
00:03:47,300 --> 00:03:51,780
So I would copy this so that we don't have to type so much and don't lose too much time.

47
00:03:52,380 --> 00:03:57,420
So for the next list, I will go to the limit minus five to five.

48
00:03:58,290 --> 00:04:08,160
Then for the scatterplot, I will use X list and here I will use our function func than this one.

49
00:04:08,160 --> 00:04:08,820
Looks good.

50
00:04:09,370 --> 00:04:11,760
But now, of course, we have to update this one.

51
00:04:12,540 --> 00:04:17,040
And here we have to just write in general, Taylor and.

52
00:04:18,380 --> 00:04:23,180
For this one, I will come back later, and for Taylor, we have to, of course, use now the correct

53
00:04:23,180 --> 00:04:23,870
arguments.

54
00:04:24,530 --> 00:04:31,010
So we have to right first argument is to function funk, then x list for the X values, and this will

55
00:04:31,010 --> 00:04:34,130
really be a whole list, not just individual numbers.

56
00:04:34,850 --> 00:04:38,540
Then for zero, we can really try different values.

57
00:04:38,930 --> 00:04:44,240
So for example, zero now we can use and Macs and Macs.

58
00:04:44,900 --> 00:04:50,180
I would say we should take some number like let's start with five and four h.

59
00:04:50,180 --> 00:04:55,850
We should also test a few values, but for the beginning, let's use zero point one.

60
00:04:56,930 --> 00:04:58,820
And this looks good.

61
00:04:59,610 --> 00:05:06,950
But now, just to add a few more of these plots at different x zero positions, I will use two and minus

62
00:05:06,950 --> 00:05:12,170
three things is similar to what we had previously and change the color red and green.

63
00:05:14,450 --> 00:05:16,850
And so now we have here our plots.

64
00:05:17,690 --> 00:05:20,120
Maybe the right y range doesn't look so good.

65
00:05:20,570 --> 00:05:21,530
That's changing a bit.

66
00:05:23,450 --> 00:05:24,860
OK, now it looks better.

67
00:05:25,790 --> 00:05:30,470
So you see these light blue dots here to scatter plots.

68
00:05:30,860 --> 00:05:34,140
This is the actual function that we have defined.

69
00:05:34,140 --> 00:05:37,070
This was something like sine square.

70
00:05:37,520 --> 00:05:45,590
Or is it here two times sine squared plus x and the other functions, the plots here it has lines.

71
00:05:45,920 --> 00:05:52,640
These are our Taylor expansions at different positions, at the position zero, two and three or minus

72
00:05:52,640 --> 00:05:52,970
three.

73
00:05:54,260 --> 00:06:03,380
So you see that at their respective points, for example, here near zero for blue, it works, OK?

74
00:06:03,560 --> 00:06:06,680
But the range in which it works is not too large.

75
00:06:07,610 --> 00:06:13,880
So what we would have to do here is probably to increase the number of terms to be taken to account

76
00:06:13,880 --> 00:06:14,720
for this series.

77
00:06:15,770 --> 00:06:17,180
So if I increased this number?

78
00:06:17,570 --> 00:06:18,830
It looks much better.

79
00:06:19,190 --> 00:06:26,780
So we have a much larger range of X values where the function would series agrees with the function.

80
00:06:28,070 --> 00:06:31,220
However, the agreement is not really that good.

81
00:06:31,700 --> 00:06:36,200
So what we can do is we can change the value of h for the derivative.

82
00:06:37,520 --> 00:06:43,820
And then you see, I would I would say the fit now looks better in this in this very narrow area.

83
00:06:44,090 --> 00:06:49,790
But this time, of course, again, the range where the fit is OK has decreased.

84
00:06:50,240 --> 00:06:57,020
So this means decreasing h gives you a better quality for the derivatives, which gives you a better

85
00:06:57,020 --> 00:07:05,330
accuracy of the series near the expansion point, but it loses you some accuracy far away from the expansion

86
00:07:05,330 --> 00:07:05,690
point.

87
00:07:06,260 --> 00:07:07,730
So that's a bit of a trade off here.

88
00:07:08,570 --> 00:07:15,110
So, of course, you could also increase the number of end max, but then you get sometimes some artifacts,

89
00:07:15,800 --> 00:07:22,790
so you really have to play them with the values of H and and Max to get really an ideal result here.

90
00:07:23,600 --> 00:07:26,210
So it can be sometimes a bit difficult.

91
00:07:27,290 --> 00:07:27,820
All right.

92
00:07:27,830 --> 00:07:34,400
So but I think overall we have seen that it works quite well to approximate these functions in some

93
00:07:34,400 --> 00:07:36,680
narrow range around the position X0.

94
00:07:37,040 --> 00:07:44,960
And we have learned that we can describe it basically any function as a polynomial by just using our

95
00:07:46,490 --> 00:07:54,980
our expansion formula from the very beginning, F of X is equal to the series of these derivatives times,

96
00:07:54,980 --> 00:07:56,360
these polynomial terms.

97
00:07:57,020 --> 00:08:05,000
What I think is also really nice is that we have written all of this in a very general way, so we cannot

98
00:08:05,000 --> 00:08:12,620
only calculate this Taylor expansion for this particular function, but we can just copy the whole block

99
00:08:12,620 --> 00:08:19,610
of code here and also copy this line of code where we have defined the function and we can just redefine

100
00:08:19,610 --> 00:08:23,360
the function here in our new self.

101
00:08:23,690 --> 00:08:25,340
We can just take another function.

102
00:08:26,090 --> 00:08:33,409
For example, we could take two times sine squared times and p dot exponential function.

103
00:08:34,190 --> 00:08:37,730
And then we just use some gauss curve.

104
00:08:39,350 --> 00:08:43,429
So this time, maybe we should change the range of it.

105
00:08:45,470 --> 00:08:51,650
This time we have such as sine function or sine squared, but to to say and modify modulated by the

106
00:08:51,650 --> 00:08:52,490
gauss function.

107
00:08:52,880 --> 00:08:59,810
And you see in the vicinity of the respective expansion point, the Taylor expansion works quite well.

108
00:09:00,170 --> 00:09:06,470
And I think this is really a nice tool that we have established here with this definition of the Taylor

109
00:09:06,470 --> 00:09:11,870
expansion where we have used also this definition of the derivative.

