1
00:00:00,060 --> 00:00:05,400
So in the previous lecture, we have explored the SAM method and did trapezoidal methods.

2
00:00:06,090 --> 00:00:12,690
And also, I mentioned that using the trapezoidal method gives us quite a good result, and it corresponds

3
00:00:12,690 --> 00:00:16,710
to integrating a Linnaeus line through two data points.

4
00:00:17,340 --> 00:00:22,620
So when we look here again, we have linear functions connecting to neighboring points and then we just

5
00:00:22,620 --> 00:00:25,620
integrate over these linear functions, which is quite easy.

6
00:00:26,850 --> 00:00:33,890
However, we could also interpolate these data points not with linear functions, but with polynomial

7
00:00:33,900 --> 00:00:34,500
functions.

8
00:00:35,250 --> 00:00:40,560
And this works especially well in our case because our function is in fact a polynomial.

9
00:00:41,720 --> 00:00:49,290
And this is this is done by using the so-called Simpson rule or also the Newton Courtes equations,

10
00:00:49,290 --> 00:00:51,960
which are general generalized versions of the Simpson rule.

11
00:00:51,960 --> 00:00:59,010
So they are even better, and this method corresponds to integrating a polynomial interpolation function

12
00:00:59,010 --> 00:01:00,180
through two data points.

13
00:01:01,170 --> 00:01:07,650
And you may wonder where all of these coefficients from this equation here come from, and you can really

14
00:01:07,650 --> 00:01:09,000
derive this analytically.

15
00:01:09,000 --> 00:01:15,630
And the coefficients have, of course, been optimized so that this formula, where we add up individual

16
00:01:15,630 --> 00:01:24,330
terms corresponds to having first interpolated the data with a third order polynomial and then integrating

17
00:01:24,330 --> 00:01:25,500
over this polynomial.

18
00:01:26,220 --> 00:01:29,040
So this is where all of these coefficients come from.

19
00:01:29,910 --> 00:01:38,310
So here we just take our individual data points X1 to X2 13 in this case, and we add them up with these

20
00:01:38,310 --> 00:01:39,150
coefficients.

21
00:01:40,140 --> 00:01:47,730
So it's a bit similar to this one, but even better, and you see basically here we would write one

22
00:01:47,730 --> 00:01:53,760
of a two f of x one and then one times f of x two, one times f of x three and so on.

23
00:01:53,760 --> 00:01:55,380
And then the last one will be one half.

24
00:01:55,950 --> 00:01:58,620
But here we have the same thing just different coefficients.

25
00:01:59,400 --> 00:02:03,630
One of a three four three two over three four three two over three and so on.

26
00:02:03,990 --> 00:02:06,120
And then the last one will again be one over three.

27
00:02:06,930 --> 00:02:10,470
So the idea behind it is quite similar.

28
00:02:11,460 --> 00:02:16,140
So here just one thing I have to mention you have to be careful because this method only works if you

29
00:02:16,140 --> 00:02:17,730
have an odd number of data points.

30
00:02:18,330 --> 00:02:23,520
And if you know the function itself, then this is absolutely no problem because you could always just

31
00:02:23,520 --> 00:02:29,400
say, OK, I just create my data points with an odd number.

32
00:02:29,970 --> 00:02:33,210
But if you are given the data, then it can be a bit tricky.

33
00:02:34,530 --> 00:02:38,760
However, luckily in our case, we have 13 data points, which is a lot numbers, so we can just go

34
00:02:38,760 --> 00:02:43,410
ahead and implement the function and check if it works or not.

35
00:02:44,520 --> 00:02:52,870
So I write define integral Simpson of data.

36
00:02:55,900 --> 00:02:58,420
And then I return.

37
00:03:01,200 --> 00:03:06,270
The individual, yeah, individual terms, I would say.

38
00:03:07,350 --> 00:03:13,020
So what I want to do is I want to start with the brackets here, so I just type along.

39
00:03:13,680 --> 00:03:22,740
One over three times data, one from a zero, which means y coordinate of the first data point.

40
00:03:23,360 --> 00:03:27,700
Then I will go ahead and take the second one.

41
00:03:27,720 --> 00:03:31,020
This would be for over three times data.

42
00:03:32,430 --> 00:03:33,570
One comma one.

43
00:03:34,440 --> 00:03:36,270
And then the next one.

44
00:03:37,830 --> 00:03:43,380
So don't worry, I will not Typekit of the data points because now we are a bit of a trouble because

45
00:03:43,770 --> 00:03:48,810
we in generally we don't know how many data points we have, so I cannot write down explicitly.

46
00:03:49,290 --> 00:03:55,110
So I have to say, OK, the second, the fourth, the sixth and so on data point, they will all be

47
00:03:55,110 --> 00:03:56,070
multiplied here.

48
00:03:56,550 --> 00:04:08,760
So what I can do is writing and p dot some over several data points and you can write here one two minus

49
00:04:08,760 --> 00:04:11,280
one in steps of two.

50
00:04:11,790 --> 00:04:15,930
And this means take the indices one, three, five, seven and so on.

51
00:04:16,589 --> 00:04:21,480
And this corresponds to taking the second, fourth, sixth eight data points.

52
00:04:22,650 --> 00:04:24,120
And here we can do the same thing.

53
00:04:24,120 --> 00:04:30,480
We just write two to minus one in steps of two.

54
00:04:31,350 --> 00:04:39,030
So this just excludes the very last data point because for this, um, we will once again have to take

55
00:04:39,030 --> 00:04:39,810
one of three.

56
00:04:40,530 --> 00:04:42,600
So, yeah, I forgot to write and peddled some.

57
00:04:43,260 --> 00:04:44,460
So let's just edit.

58
00:04:46,930 --> 00:04:53,680
Like this and then the rides plus the last one will be one over three times data.

59
00:04:56,790 --> 00:04:58,560
One comma minus one.

60
00:04:59,850 --> 00:05:00,330
All right.

61
00:05:00,930 --> 00:05:06,540
So I close the bracket, and now we only have to consider this one here, and we have this before already.

62
00:05:06,750 --> 00:05:08,220
It was just this expression.

63
00:05:08,760 --> 00:05:12,480
So you just multiply it here and we are good to go.

64
00:05:13,200 --> 00:05:20,340
So here now, the command is a bit long, so the trick you could use is to use such a backslash and

65
00:05:20,340 --> 00:05:22,590
then enter and then some tabs.

66
00:05:22,860 --> 00:05:28,410
So this means you continue the line in the next line and I think how it should work.

67
00:05:29,350 --> 00:05:33,840
Try to find this and I'll write integral.

68
00:05:37,640 --> 00:05:40,610
I can just copy this so I don't make a typo here.

69
00:05:41,270 --> 00:05:45,020
And the result is six point six, exactly.

70
00:05:45,920 --> 00:05:47,810
So maybe you read this already?

71
00:05:47,810 --> 00:05:51,410
I wrote it down before we get to the perfect result here.

72
00:05:51,640 --> 00:05:56,390
And the reason is because our data has been generated using a third order polynomial.

73
00:05:56,870 --> 00:06:02,240
And the Simpson rule considers also third all the polynomials for the interpolation of the data.

74
00:06:02,660 --> 00:06:05,630
So it's clear that it must get the perfect result.

75
00:06:06,320 --> 00:06:08,780
So, of course, it will not always get the perfect result.

76
00:06:08,800 --> 00:06:13,310
For example, if we would have taken an exponential function, then the result would not be perfect.

77
00:06:13,700 --> 00:06:20,090
But it would still be much better than the linear interpolation method, which corresponds to the trapezoidal

78
00:06:20,090 --> 00:06:20,510
method.

79
00:06:21,200 --> 00:06:28,700
So you see, when you cannot tune the number of data points that you take into account that you must

80
00:06:28,700 --> 00:06:31,070
really make the best out of your data.

81
00:06:31,610 --> 00:06:38,030
And a good idea is to interpolate the data first and then integrate over this function.

82
00:06:38,490 --> 00:06:43,640
And this is exactly done by the Simpson rule and the Newton Carter's equations.

83
00:06:43,640 --> 00:06:49,580
They basically consider a higher order polynomials, which work even better for more difficult functions.

84
00:06:50,300 --> 00:06:55,070
But yeah, I think since we already get the perfect result, it's not really necessary to implement

85
00:06:55,070 --> 00:06:55,460
these.

86
00:06:55,880 --> 00:07:05,450
But just as an example, for example, you can take the boules rule, which has even more complicated

87
00:07:05,450 --> 00:07:13,070
coefficients, which arise from the fact that you take a higher order polynomial to interpolate the

88
00:07:13,070 --> 00:07:20,000
data first and then you integrate over it if you want to know more of these methods.

89
00:07:20,360 --> 00:07:23,330
Here they are all listed on this Wikipedia page.

