1
00:00:00,300 --> 00:00:06,990
Before we can really get started with interpolating our data in terms of functions, I want to talk

2
00:00:06,990 --> 00:00:15,000
about a concept called Taylor expansion and Taylor expansion means that you can basically take any function

3
00:00:15,330 --> 00:00:16,890
and you can expand.

4
00:00:16,890 --> 00:00:24,390
It is a series or as a sum that goes to infinity over these terms that you can see here.

5
00:00:24,840 --> 00:00:27,780
So you can expand it as a polynomial.

6
00:00:28,920 --> 00:00:35,790
And the equation is a sum of these terms, which are basically here.

7
00:00:35,970 --> 00:00:42,600
The end the derivative of the function f at the position x zero, which is the position or the argument

8
00:00:42,600 --> 00:00:48,130
around which we expand the function and that we have here are X dependence.

9
00:00:48,150 --> 00:00:55,200
So here the distance from the zero will then be squared and then taken to the power three and four and

10
00:00:55,200 --> 00:00:55,650
so on.

11
00:00:56,490 --> 00:01:00,810
So of course, in numerics, you cannot go to infinity here.

12
00:01:01,230 --> 00:01:07,470
So at some point this series has to terminate and you have to define some and max value at which you

13
00:01:07,470 --> 00:01:09,630
stop expanding the function.

14
00:01:10,110 --> 00:01:17,250
And then hopefully in the range where you are interested in the data, then the function is approximated

15
00:01:17,310 --> 00:01:25,800
to a good amount and we want to start and we want to test this concept, the Taylor expansion at the

16
00:01:25,800 --> 00:01:27,750
example of the exponential function.

17
00:01:28,350 --> 00:01:34,860
But first of all, I will run the cell here where I will import our two most important modules non pi

18
00:01:35,130 --> 00:01:36,210
and multiplied lib.

19
00:01:37,500 --> 00:01:44,610
So here I've written down already that we are investigating the exponential function, which is f of

20
00:01:44,610 --> 00:01:47,240
X is equal to exponential function of X.

21
00:01:47,970 --> 00:01:55,860
And as you can see from this equation here, we need the first derivative, the second to third and

22
00:01:55,860 --> 00:01:56,190
so on.

23
00:01:56,190 --> 00:02:01,290
We need all the derivatives and the special feature about the exponential function.

24
00:02:01,590 --> 00:02:07,140
This is how it is defined, is that the derivatives are all the same.

25
00:02:07,350 --> 00:02:10,740
So every derivative of the function is the function itself.

26
00:02:10,830 --> 00:02:14,160
It's still exponential function of X.

27
00:02:15,300 --> 00:02:23,580
So what we can do then very easily is we can define a function called EEGs P Taylor, which will be

28
00:02:23,580 --> 00:02:30,090
our Taylor expanded series of the function of X is equal to exponential function of X.

29
00:02:31,200 --> 00:02:33,240
So of course, we need an argument x.

30
00:02:33,540 --> 00:02:38,880
Then you can see we need to define some position at which we expand our series.

31
00:02:38,940 --> 00:02:46,500
This would be x zero and then we must define the upper boundary here because, as I said, we cannot

32
00:02:46,500 --> 00:02:47,430
go to infinity.

33
00:02:47,760 --> 00:02:49,860
So we have to define some and maximal u.

34
00:02:50,850 --> 00:02:56,640
And just to make sure that we remember later on, I will explain here all of these arguments.

35
00:02:56,640 --> 00:03:09,180
So X is the argument of the function that zero is the argument at which the derivatives will be calculated

36
00:03:11,610 --> 00:03:25,050
and then the and maximal you will be the end at which the series will terminate or at which it will

37
00:03:25,050 --> 00:03:25,570
break.

38
00:03:25,590 --> 00:03:32,580
You could also say and now what we will do is we will sum up several parts here and you can do this

39
00:03:32,580 --> 00:03:33,810
in several methods.

40
00:03:33,810 --> 00:03:39,810
You could, for example, define the individual terms as an array and then you can use the N pitot sum

41
00:03:40,530 --> 00:03:41,150
equation.

42
00:03:41,160 --> 00:03:43,290
We will do this also later in this notebook.

43
00:03:43,560 --> 00:03:46,470
But here I will just use a simple loop.

44
00:03:46,890 --> 00:03:53,250
So I will define here some value T, which will in the end, be the value of our Taylor expansion.

45
00:03:53,580 --> 00:03:55,350
And initially it is zero.

46
00:03:55,710 --> 00:03:59,880
And then we make a loop here for n in range.

47
00:03:59,890 --> 00:04:02,100
You can, of course, also take another type of loop.

48
00:04:02,520 --> 00:04:08,070
So there are many possibilities how you can do this, and we are summing up starting from zero and going

49
00:04:08,100 --> 00:04:09,660
to an max.

50
00:04:09,660 --> 00:04:16,440
And we include the value of N Max, which is why I wrote, which is why I wrote and max plus one here.

51
00:04:17,490 --> 00:04:20,190
So now what we do is we add up all of these values.

52
00:04:20,190 --> 00:04:24,690
So we write T is equal to the old T plus some value.

53
00:04:25,590 --> 00:04:33,870
And now, of course, it is a bit stupid to use the exponential function to define the Taylor expansion

54
00:04:33,870 --> 00:04:34,200
here.

55
00:04:34,200 --> 00:04:37,620
But yeah, let's let's do it for the moment is just an exercise here.

56
00:04:38,070 --> 00:04:45,780
So we will calculate them the and P X value at the position x zero.

57
00:04:47,010 --> 00:04:54,180
So for example, if we would select zero equal to zero, then we could just write here one, because

58
00:04:54,180 --> 00:04:57,060
we know the exponential function of zero is always one.

59
00:04:57,720 --> 00:04:59,940
So then it would maybe be a bit more reason.

60
00:05:00,260 --> 00:05:04,500
But then we would be restricted to just to one argument, zero.

61
00:05:04,720 --> 00:05:10,300
And I want to show you that this series can also look different when we choose different values of zero.

62
00:05:10,300 --> 00:05:14,280
So this is why I take your export of cereal.

63
00:05:14,560 --> 00:05:15,820
And then we multiply.

64
00:05:16,210 --> 00:05:25,900
So we take the the argument x minus x zero to the power of end.

65
00:05:27,220 --> 00:05:31,600
And so we have these two terms and now we are just missing this one here, which is two factorial.

66
00:05:32,140 --> 00:05:40,990
So for example, four factorial four exclamation mark means four times, three times, two times one.

67
00:05:41,890 --> 00:05:44,350
And we could, of course, program this here.

68
00:05:44,650 --> 00:05:51,520
But there is already a function that's implemented in nonpaying math module, which is called factorial.

69
00:05:51,850 --> 00:05:53,470
So we can just use this one here.

70
00:05:54,370 --> 00:05:59,180
And then don't forget, we have to return some value and of course, we want to return to.

71
00:06:00,160 --> 00:06:05,410
So as I've said, you can also write all of this in just one line when you use the and p dot some command

72
00:06:05,410 --> 00:06:09,520
and just sum up over an array where the elements of the array on these terms here.

73
00:06:09,850 --> 00:06:12,310
But yeah, let's not make it too complicated.

74
00:06:12,700 --> 00:06:15,700
And I think this one is even easier to understand.

75
00:06:16,450 --> 00:06:19,180
And now we can write the P Taylor.

76
00:06:20,230 --> 00:06:30,100
So now we run this function and we can choose, for example, the argument one for the evolution point,

77
00:06:30,100 --> 00:06:35,080
we take zero and then for the terms, let's use ten for the beginning.

78
00:06:36,070 --> 00:06:37,720
And so we get this number here.

79
00:06:37,720 --> 00:06:39,040
Two point seven one.

80
00:06:39,040 --> 00:06:42,490
So maybe this reminds you of the Euler constant and that check.

81
00:06:43,060 --> 00:06:49,300
So and p dot s p of one is almost this value.

82
00:06:49,990 --> 00:06:56,440
So you see, by taking into account only 10 terms of this infinitely large series here allows us to

83
00:06:56,440 --> 00:07:01,660
reproduce the Euler a number by a very large accuracy.

84
00:07:02,020 --> 00:07:04,720
So it works pretty well for this particular function.

85
00:07:05,590 --> 00:07:11,650
And also the reason why this works well is because our position one is quite close to our ex zero.

86
00:07:12,970 --> 00:07:17,350
And I want to show you what happens when you select a different position for the evolution.

87
00:07:17,740 --> 00:07:24,370
So I want to make a plot here, so I write X list is equal to NP total in space.

88
00:07:25,600 --> 00:07:28,930
And I want to plot in the range of four minus five to five.

89
00:07:29,740 --> 00:07:36,190
And then first of all, I want to do a sketch of plot and I want to plot this list of coordinates that

90
00:07:36,190 --> 00:07:39,950
I've just created.

91
00:07:40,270 --> 00:07:43,000
And then we just plot the exponential function.

92
00:07:43,780 --> 00:07:45,460
So this will be our reference point.

93
00:07:45,460 --> 00:07:50,710
This will be the correct exponential function as it is implemented in non pi.

94
00:07:51,430 --> 00:07:56,740
So this will be the one that we are trying to recreate here, and I will do this now.

95
00:07:57,340 --> 00:08:04,750
Maybe first let me add labels here on appeal to you told X Label X, of course.

96
00:08:05,200 --> 00:08:06,520
Then we need a y label.

97
00:08:07,120 --> 00:08:13,150
Oops, sorry y label, which is why and we need some limits.

98
00:08:13,690 --> 00:08:21,310
So I write pl t y limits x limit as OK, we have already defined this by this command and the Y limit

99
00:08:21,310 --> 00:08:24,040
will be minus five to 100.

100
00:08:25,850 --> 00:08:26,420
All right.

101
00:08:27,050 --> 00:08:33,320
And now I can go ahead and now I can plot the Taylor expansion that we have just find.

102
00:08:33,770 --> 00:08:40,640
So here I will do a line plot, all right, to appeal to the plot and then axe lists come out XP Taylor.

103
00:08:43,010 --> 00:08:47,780
And for the argument, we do not only take a single value, but we take our whole list.

104
00:08:47,780 --> 00:08:49,220
And of course, this works.

105
00:08:50,000 --> 00:08:51,440
And then 40 zero.

106
00:08:51,660 --> 00:09:00,170
Here I want to take zero and then we to find some value and max, which we have previously chosen to

107
00:09:00,170 --> 00:09:00,700
be 10.

108
00:09:00,710 --> 00:09:01,460
So that's right.

109
00:09:01,460 --> 00:09:02,060
And Max.

110
00:09:02,750 --> 00:09:08,240
And let's create these points or this line in a color blue.

111
00:09:09,860 --> 00:09:14,990
So you see, if we take 10 points into account, then it works quite well.

112
00:09:15,080 --> 00:09:21,560
I mean, you see maybe a bit here, some change, some deviation, but overall it works quite well.

113
00:09:22,460 --> 00:09:33,050
For example, if we take five only, then you see in the vicinity of X is equal to zero, which is zero.

114
00:09:33,050 --> 00:09:34,570
In all case, it works well.

115
00:09:34,580 --> 00:09:41,690
But then when we go way beyond plus minus two and we see some large deviation of the plot, and this

116
00:09:41,690 --> 00:09:47,600
changes if we take the different reference point for the evolution, for example, we could take here

117
00:09:47,600 --> 00:09:56,930
minus three and we can plot it in red or we could take two and we plotted in green.

118
00:09:58,640 --> 00:10:02,650
And then I see for the to function, which corresponds to minus three.

119
00:10:02,660 --> 00:10:09,080
It works pretty well here for small values, but not for high values and for the green function, which

120
00:10:09,080 --> 00:10:13,220
is expanded near zero equal to two.

121
00:10:13,580 --> 00:10:15,970
It is exactly the opposite way.

122
00:10:15,980 --> 00:10:20,810
So here and works well in this area, but it doesn't work at all for small values.

123
00:10:21,620 --> 00:10:28,760
So you see Taylor expansions work and we can tune the accuracy by this parameter and max.

124
00:10:29,150 --> 00:10:35,060
So of course, the higher end max will be the more terms we take into account and the longer our calculation

125
00:10:35,060 --> 00:10:35,570
will run.

126
00:10:35,990 --> 00:10:41,930
But you see, if we make it very large, then we can basically fit any function that's continuous in

127
00:10:41,930 --> 00:10:44,570
any range for the X value.

128
00:10:46,070 --> 00:10:51,830
So I will go back to the initial value because here you can see nicely that the position of zero matters

129
00:10:52,220 --> 00:10:57,800
and in the next lecture, we will expand our next function and I want to show you something else.

