In the previous lecture, we have created this data.
These individual points here, and the reason why I want to start with these data points are not the
function itself is that I want to avoid the possibility of decreasing the integration into law.
So of course, what we could do when we take this method here is we could make these intervals smaller
and smaller.
So these these red rectangles, we could make them narrower.
We take more points into account.
And of course, then at some point, all the integral, all of the integration methods will give the
same result and will have very good accuracy.
However, quite often when you have very realistic data, then you don't know the function behind it.
So you don't have the option to just decrease the width of these columns and to increase the data points.
So we have to work with what we have just can work with these 13 data points, and now we will explore
the methods that I have presented here.
So we take the some of these data points and then we also take these trapezoidal methods into account.
So let's get started with the sum and you I've written down that basically, this factor describes the
size of each of these columns.
So if each of these bars.
And what I tried to do here is we have 13 data points and you see, if we really take 13 times the width
of these bars, then we have a two large integration interval.
So what I tried instead is I I decrease the, you know, the number of points and therefore decrease
the weight of all of these points.
And therefore, we have the integration interval correct from minus three to three.
But still, these points at the edges, they will be over pronounced, which will give us a wrong answer.
So maybe just to compare the analytical results was six point six.
And now, since the edges are over pronounced and since they are positive, I would expect that now
we will get two large values using this method.
So let's get started.
I define a function called integral some, and the only argument is to data and what we return is.
And P dot sum.
So it's a sum over the right and the array.
Or yeah, well, basically just be the values of this function.
So we have an odd sum of data and then the weichel components.
So this will be the f of X I.
And then we just have to consider this factor here so we can write data zero comma minus one minus data
zero zero.
So this means take the X value of the last point minus the value of the first point.
So this will be three minus minus three.
So this gives us six, which is the total interval for the integration.
And now we can test two things.
First, we can just divide by end, which would probably be the first idea.
So we just right divided by the length of data one.
So this will be the number of points that we have and then we can test what is the numerical result?
Integral sum of data is 7.2, which is way too large.
Yeah.
So then we could also try to correct this with the minus one here to basically reduce the number of
points.
But then the results will get even worse.
So you see, there is really a big problem that we have here.
Of course, as I said, this method would give you a better result if you just increase the number of
data points because then the effect of over pronouncing these two points becomes decreased.
So then it's not that big of a deal anymore.
However, what is much better is to use the trapezoidal methods and in fact, the trapezoidal method
is equivalent to this methods.
So we will first program this method and then we will.
I'm sorry.
First, we will program this method of trapezoidal methods and then we will program this methods and
we will see that both of them give the same results.
So the trapezoidal method basically means that we take our data and we do not take these rectangular
bars anymore over which we add up, but instead we take.
Is lights, so we have two points to neighboring points and we have this line, and then we can calculate
just the area of this trapezoid and the area will be calculated by taking basically the average value
of the Y coordinates.
So somewhere between here this will be done the height and then we multiply by the width of this integral.
So we have this interval.
So what we do is we just right, define trap P. Zoidberg and then we can call it maybe, maybe call
it better integral trapezoidal data.
And now we first have this some term here.
So of course, we could just take it and piss some of an array that we construct.
But I think it's easier to just use loop.
So we loop.
For I in range length of data zero, which will be the number of our points, and then we subtract one
because we have 13 data points, which means we have 13 individual intervals.
This is why I write here minus one.
And then here we add up.
So we write is a plus some term, and in the end we return to value eight.
And the thing over which we had this is this term here.
So as I said, we take the average value of two neighboring points.
So this would be data one comma pi plus one and then plus data one comma I.
And then we divide by two and then we multiply by the size or by the width of these individual intervals.
So in our case, we have equidistant points.
But the trapezoidal methods works also for non equidistant data, which is really nice.
So to make it more general, I will just write here data on zero.
And then I plus one minus data zero comma.
I thought we could have also taken just basically this one here would have worked in the same way.
So I had to find this, and then I will calculate the results, so I will call integral trapezoidal
data.
And the result is six point sixty five.
So the analytical result was six point six zero.
And you see there is a small deviation.
For example, here you can see the blue curve does look a bit different compared to the red curve.
But overall, the result is pretty good and we only have an error of 0.05, which is below one percent.
So the trapezoidal method works way better than this method, and it's quite a good approximation.
So maybe one more comment.
This trapezoidal method corresponds to integrating a linear spline function through two data points.
So you see, we have linear functions that are defined piece wise, and then you can also show that
when you rearrange the terms of the sum that we have just programmed that this method is equivalent
to this method and this we will explore now by example.
So we will program this method here, which relies on the fact that the starting and end point only
contribute with half of their weight.
Besides that, the method is the same as this one.
So let's go ahead and program this one here for equidistant data.
So what we can do to make our lives easier is to just copy the code from the integral sample.
And I call this method.
I don't know, maybe I call it therapy, he saw all the queue because then these two are equivalent
and what we program here is this equation basically.
So we have the prefecture that we also had before, which is basically the size of the interval, then
we have here to some also the same as before.
But the sum goes from the second point to the second last point and then the first and the last points.
They are also added up, but with a weight factor of one half.
So basically, you're right here that we have in sum and the sum of one, which will be the Y component.
And now we don't start at zero, but we start at one, which will be a second value in the list and
we go to minus one, which means we.
So this is maybe a bit tricky in Python, I think, because this means excludes the last point.
So this will be the second to the second last point.
All right.
And now we just have to add the two other terms.
So we will have to add brackets here.
And then we just write one half times data one from a zero, which is the first data point.
Plus then our end piece sum and then plus one half times data, one comma and then the last point minus
one.
And now we can run this and we can call it.
And you see the result is exactly the same.
So this is because you can just take these individual components here and grouped them together.
And you see here we have, for example, we take, for example, ie equal to three and equal to four.
Then we have two times this term, which would be f of X for half and then we have the twice.
So this gives us just f of X, and this is true for every I, except for the first and for the last
data points, which we will only have once.
So there we will have to factor one half.
So you see, if we have equidistant data, then these two methods are really the same, even though
from the graphics they look quite different.
But adding up these areas in red here give you exactly the same result.