1
00:00:00,150 --> 00:00:03,000
In the previous lecture, we have created this data.

2
00:00:03,030 --> 00:00:08,250
These individual points here, and the reason why I want to start with these data points are not the

3
00:00:08,250 --> 00:00:15,570
function itself is that I want to avoid the possibility of decreasing the integration into law.

4
00:00:16,350 --> 00:00:22,410
So of course, what we could do when we take this method here is we could make these intervals smaller

5
00:00:22,410 --> 00:00:22,980
and smaller.

6
00:00:22,980 --> 00:00:26,130
So these these red rectangles, we could make them narrower.

7
00:00:26,430 --> 00:00:28,320
We take more points into account.

8
00:00:28,710 --> 00:00:34,320
And of course, then at some point, all the integral, all of the integration methods will give the

9
00:00:34,320 --> 00:00:37,410
same result and will have very good accuracy.

10
00:00:38,100 --> 00:00:43,920
However, quite often when you have very realistic data, then you don't know the function behind it.

11
00:00:43,920 --> 00:00:52,440
So you don't have the option to just decrease the width of these columns and to increase the data points.

12
00:00:53,070 --> 00:01:00,570
So we have to work with what we have just can work with these 13 data points, and now we will explore

13
00:01:00,570 --> 00:01:02,760
the methods that I have presented here.

14
00:01:03,120 --> 00:01:10,530
So we take the some of these data points and then we also take these trapezoidal methods into account.

15
00:01:11,490 --> 00:01:17,670
So let's get started with the sum and you I've written down that basically, this factor describes the

16
00:01:17,700 --> 00:01:19,600
size of each of these columns.

17
00:01:19,950 --> 00:01:21,330
So if each of these bars.

18
00:01:21,720 --> 00:01:29,880
And what I tried to do here is we have 13 data points and you see, if we really take 13 times the width

19
00:01:29,880 --> 00:01:34,110
of these bars, then we have a two large integration interval.

20
00:01:34,710 --> 00:01:41,730
So what I tried instead is I I decrease the, you know, the number of points and therefore decrease

21
00:01:41,730 --> 00:01:43,110
the weight of all of these points.

22
00:01:43,650 --> 00:01:48,270
And therefore, we have the integration interval correct from minus three to three.

23
00:01:49,140 --> 00:01:56,280
But still, these points at the edges, they will be over pronounced, which will give us a wrong answer.

24
00:01:57,270 --> 00:02:02,850
So maybe just to compare the analytical results was six point six.

25
00:02:03,360 --> 00:02:09,030
And now, since the edges are over pronounced and since they are positive, I would expect that now

26
00:02:09,030 --> 00:02:11,850
we will get two large values using this method.

27
00:02:12,900 --> 00:02:13,860
So let's get started.

28
00:02:16,170 --> 00:02:25,470
I define a function called integral some, and the only argument is to data and what we return is.

29
00:02:26,610 --> 00:02:28,610
And P dot sum.

30
00:02:29,250 --> 00:02:33,720
So it's a sum over the right and the array.

31
00:02:33,870 --> 00:02:37,800
Or yeah, well, basically just be the values of this function.

32
00:02:38,910 --> 00:02:43,680
So we have an odd sum of data and then the weichel components.

33
00:02:43,680 --> 00:02:45,480
So this will be the f of X I.

34
00:02:45,990 --> 00:03:01,320
And then we just have to consider this factor here so we can write data zero comma minus one minus data

35
00:03:02,250 --> 00:03:03,740
zero zero.

36
00:03:04,290 --> 00:03:09,720
So this means take the X value of the last point minus the value of the first point.

37
00:03:09,720 --> 00:03:12,670
So this will be three minus minus three.

38
00:03:12,690 --> 00:03:16,350
So this gives us six, which is the total interval for the integration.

39
00:03:17,220 --> 00:03:18,750
And now we can test two things.

40
00:03:19,080 --> 00:03:22,740
First, we can just divide by end, which would probably be the first idea.

41
00:03:23,580 --> 00:03:30,390
So we just right divided by the length of data one.

42
00:03:30,720 --> 00:03:36,360
So this will be the number of points that we have and then we can test what is the numerical result?

43
00:03:37,920 --> 00:03:46,410
Integral sum of data is 7.2, which is way too large.

44
00:03:48,390 --> 00:03:48,690
Yeah.

45
00:03:49,140 --> 00:03:56,010
So then we could also try to correct this with the minus one here to basically reduce the number of

46
00:03:56,010 --> 00:03:56,520
points.

47
00:03:57,060 --> 00:03:59,190
But then the results will get even worse.

48
00:03:59,700 --> 00:04:02,940
So you see, there is really a big problem that we have here.

49
00:04:03,600 --> 00:04:09,270
Of course, as I said, this method would give you a better result if you just increase the number of

50
00:04:09,270 --> 00:04:15,430
data points because then the effect of over pronouncing these two points becomes decreased.

51
00:04:15,450 --> 00:04:17,670
So then it's not that big of a deal anymore.

52
00:04:18,839 --> 00:04:27,630
However, what is much better is to use the trapezoidal methods and in fact, the trapezoidal method

53
00:04:27,960 --> 00:04:29,850
is equivalent to this methods.

54
00:04:30,240 --> 00:04:33,930
So we will first program this method and then we will.

55
00:04:34,270 --> 00:04:34,860
I'm sorry.

56
00:04:34,950 --> 00:04:40,380
First, we will program this method of trapezoidal methods and then we will program this methods and

57
00:04:40,380 --> 00:04:42,840
we will see that both of them give the same results.

58
00:04:44,460 --> 00:04:54,330
So the trapezoidal method basically means that we take our data and we do not take these rectangular

59
00:04:54,330 --> 00:04:58,380
bars anymore over which we add up, but instead we take.

60
00:04:59,670 --> 00:05:06,240
Is lights, so we have two points to neighboring points and we have this line, and then we can calculate

61
00:05:06,240 --> 00:05:13,980
just the area of this trapezoid and the area will be calculated by taking basically the average value

62
00:05:14,310 --> 00:05:15,710
of the Y coordinates.

63
00:05:15,720 --> 00:05:22,440
So somewhere between here this will be done the height and then we multiply by the width of this integral.

64
00:05:23,070 --> 00:05:24,150
So we have this interval.

65
00:05:25,530 --> 00:05:40,170
So what we do is we just right, define trap P. Zoidberg and then we can call it maybe, maybe call

66
00:05:40,170 --> 00:05:43,910
it better integral trapezoidal data.

67
00:05:45,330 --> 00:05:49,470
And now we first have this some term here.

68
00:05:49,950 --> 00:05:54,780
So of course, we could just take it and piss some of an array that we construct.

69
00:05:55,200 --> 00:05:57,750
But I think it's easier to just use loop.

70
00:05:58,290 --> 00:05:59,070
So we loop.

71
00:05:59,190 --> 00:06:09,810
For I in range length of data zero, which will be the number of our points, and then we subtract one

72
00:06:10,110 --> 00:06:16,800
because we have 13 data points, which means we have 13 individual intervals.

73
00:06:17,670 --> 00:06:19,200
This is why I write here minus one.

74
00:06:20,130 --> 00:06:22,590
And then here we add up.

75
00:06:22,770 --> 00:06:28,220
So we write is a plus some term, and in the end we return to value eight.

76
00:06:29,760 --> 00:06:32,610
And the thing over which we had this is this term here.

77
00:06:33,450 --> 00:06:37,440
So as I said, we take the average value of two neighboring points.

78
00:06:38,010 --> 00:06:47,070
So this would be data one comma pi plus one and then plus data one comma I.

79
00:06:48,360 --> 00:06:57,780
And then we divide by two and then we multiply by the size or by the width of these individual intervals.

80
00:06:58,530 --> 00:07:01,170
So in our case, we have equidistant points.

81
00:07:01,170 --> 00:07:06,540
But the trapezoidal methods works also for non equidistant data, which is really nice.

82
00:07:07,170 --> 00:07:11,760
So to make it more general, I will just write here data on zero.

83
00:07:13,710 --> 00:07:18,960
And then I plus one minus data zero comma.

84
00:07:18,960 --> 00:07:27,030
I thought we could have also taken just basically this one here would have worked in the same way.

85
00:07:28,200 --> 00:07:34,800
So I had to find this, and then I will calculate the results, so I will call integral trapezoidal

86
00:07:35,190 --> 00:07:35,790
data.

87
00:07:36,330 --> 00:07:39,060
And the result is six point sixty five.

88
00:07:40,080 --> 00:07:44,130
So the analytical result was six point six zero.

89
00:07:44,460 --> 00:07:47,660
And you see there is a small deviation.

90
00:07:47,670 --> 00:07:52,410
For example, here you can see the blue curve does look a bit different compared to the red curve.

91
00:07:52,770 --> 00:07:59,970
But overall, the result is pretty good and we only have an error of 0.05, which is below one percent.

92
00:08:00,600 --> 00:08:06,390
So the trapezoidal method works way better than this method, and it's quite a good approximation.

93
00:08:07,470 --> 00:08:08,820
So maybe one more comment.

94
00:08:09,210 --> 00:08:16,810
This trapezoidal method corresponds to integrating a linear spline function through two data points.

95
00:08:17,040 --> 00:08:23,850
So you see, we have linear functions that are defined piece wise, and then you can also show that

96
00:08:23,850 --> 00:08:30,090
when you rearrange the terms of the sum that we have just programmed that this method is equivalent

97
00:08:30,090 --> 00:08:33,890
to this method and this we will explore now by example.

98
00:08:33,929 --> 00:08:40,080
So we will program this method here, which relies on the fact that the starting and end point only

99
00:08:40,080 --> 00:08:42,480
contribute with half of their weight.

100
00:08:42,960 --> 00:08:45,990
Besides that, the method is the same as this one.

101
00:08:47,040 --> 00:08:53,100
So let's go ahead and program this one here for equidistant data.

102
00:08:54,060 --> 00:09:04,530
So what we can do to make our lives easier is to just copy the code from the integral sample.

103
00:09:07,120 --> 00:09:08,950
And I call this method.

104
00:09:12,560 --> 00:09:20,090
I don't know, maybe I call it therapy, he saw all the queue because then these two are equivalent

105
00:09:20,690 --> 00:09:24,530
and what we program here is this equation basically.

106
00:09:24,530 --> 00:09:30,950
So we have the prefecture that we also had before, which is basically the size of the interval, then

107
00:09:30,950 --> 00:09:33,860
we have here to some also the same as before.

108
00:09:35,330 --> 00:09:42,620
But the sum goes from the second point to the second last point and then the first and the last points.

109
00:09:43,040 --> 00:09:46,370
They are also added up, but with a weight factor of one half.

110
00:09:47,600 --> 00:09:56,690
So basically, you're right here that we have in sum and the sum of one, which will be the Y component.

111
00:09:56,690 --> 00:10:02,270
And now we don't start at zero, but we start at one, which will be a second value in the list and

112
00:10:02,270 --> 00:10:06,410
we go to minus one, which means we.

113
00:10:06,710 --> 00:10:14,480
So this is maybe a bit tricky in Python, I think, because this means excludes the last point.

114
00:10:14,480 --> 00:10:17,840
So this will be the second to the second last point.

115
00:10:19,100 --> 00:10:19,520
All right.

116
00:10:19,520 --> 00:10:22,980
And now we just have to add the two other terms.

117
00:10:23,000 --> 00:10:24,620
So we will have to add brackets here.

118
00:10:26,030 --> 00:10:37,790
And then we just write one half times data one from a zero, which is the first data point.

119
00:10:39,070 --> 00:10:52,730
Plus then our end piece sum and then plus one half times data, one comma and then the last point minus

120
00:10:52,730 --> 00:10:53,000
one.

121
00:10:54,410 --> 00:10:57,620
And now we can run this and we can call it.

122
00:11:00,710 --> 00:11:02,840
And you see the result is exactly the same.

123
00:11:03,980 --> 00:11:10,460
So this is because you can just take these individual components here and grouped them together.

124
00:11:10,670 --> 00:11:17,780
And you see here we have, for example, we take, for example, ie equal to three and equal to four.

125
00:11:18,110 --> 00:11:25,160
Then we have two times this term, which would be f of X for half and then we have the twice.

126
00:11:25,190 --> 00:11:31,220
So this gives us just f of X, and this is true for every I, except for the first and for the last

127
00:11:31,220 --> 00:11:33,670
data points, which we will only have once.

128
00:11:34,040 --> 00:11:36,110
So there we will have to factor one half.

129
00:11:36,830 --> 00:11:42,320
So you see, if we have equidistant data, then these two methods are really the same, even though

130
00:11:42,680 --> 00:11:44,630
from the graphics they look quite different.

131
00:11:44,870 --> 00:11:48,890
But adding up these areas in red here give you exactly the same result.

