So in the previous lecture, we have explored the SAM method and did trapezoidal methods.
And also, I mentioned that using the trapezoidal method gives us quite a good result, and it corresponds
to integrating a Linnaeus line through two data points.
So when we look here again, we have linear functions connecting to neighboring points and then we just
integrate over these linear functions, which is quite easy.
However, we could also interpolate these data points not with linear functions, but with polynomial
functions.
And this works especially well in our case because our function is in fact a polynomial.
And this is this is done by using the so-called Simpson rule or also the Newton Courtes equations,
which are general generalized versions of the Simpson rule.
So they are even better, and this method corresponds to integrating a polynomial interpolation function
through two data points.
And you may wonder where all of these coefficients from this equation here come from, and you can really
derive this analytically.
And the coefficients have, of course, been optimized so that this formula, where we add up individual
terms corresponds to having first interpolated the data with a third order polynomial and then integrating
over this polynomial.
So this is where all of these coefficients come from.
So here we just take our individual data points X1 to X2 13 in this case, and we add them up with these
coefficients.
So it's a bit similar to this one, but even better, and you see basically here we would write one
of a two f of x one and then one times f of x two, one times f of x three and so on.
And then the last one will be one half.
But here we have the same thing just different coefficients.
One of a three four three two over three four three two over three and so on.
And then the last one will again be one over three.
So the idea behind it is quite similar.
So here just one thing I have to mention you have to be careful because this method only works if you
have an odd number of data points.
And if you know the function itself, then this is absolutely no problem because you could always just
say, OK, I just create my data points with an odd number.
But if you are given the data, then it can be a bit tricky.
However, luckily in our case, we have 13 data points, which is a lot numbers, so we can just go
ahead and implement the function and check if it works or not.
So I write define integral Simpson of data.
And then I return.
The individual, yeah, individual terms, I would say.
So what I want to do is I want to start with the brackets here, so I just type along.
One over three times data, one from a zero, which means y coordinate of the first data point.
Then I will go ahead and take the second one.
This would be for over three times data.
One comma one.
And then the next one.
So don't worry, I will not Typekit of the data points because now we are a bit of a trouble because
we in generally we don't know how many data points we have, so I cannot write down explicitly.
So I have to say, OK, the second, the fourth, the sixth and so on data point, they will all be
multiplied here.
So what I can do is writing and p dot some over several data points and you can write here one two minus
one in steps of two.
And this means take the indices one, three, five, seven and so on.
And this corresponds to taking the second, fourth, sixth eight data points.
And here we can do the same thing.
We just write two to minus one in steps of two.
So this just excludes the very last data point because for this, um, we will once again have to take
one of three.
So, yeah, I forgot to write and peddled some.
So let's just edit.
Like this and then the rides plus the last one will be one over three times data.
One comma minus one.
All right.
So I close the bracket, and now we only have to consider this one here, and we have this before already.
It was just this expression.
So you just multiply it here and we are good to go.
So here now, the command is a bit long, so the trick you could use is to use such a backslash and
then enter and then some tabs.
So this means you continue the line in the next line and I think how it should work.
Try to find this and I'll write integral.
I can just copy this so I don't make a typo here.
And the result is six point six, exactly.
So maybe you read this already?
I wrote it down before we get to the perfect result here.
And the reason is because our data has been generated using a third order polynomial.
And the Simpson rule considers also third all the polynomials for the interpolation of the data.
So it's clear that it must get the perfect result.
So, of course, it will not always get the perfect result.
For example, if we would have taken an exponential function, then the result would not be perfect.
But it would still be much better than the linear interpolation method, which corresponds to the trapezoidal
method.
So you see, when you cannot tune the number of data points that you take into account that you must
really make the best out of your data.
And a good idea is to interpolate the data first and then integrate over this function.
And this is exactly done by the Simpson rule and the Newton Carter's equations.
They basically consider a higher order polynomials, which work even better for more difficult functions.
But yeah, I think since we already get the perfect result, it's not really necessary to implement
these.
But just as an example, for example, you can take the boules rule, which has even more complicated
coefficients, which arise from the fact that you take a higher order polynomial to interpolate the
data first and then you integrate over it if you want to know more of these methods.
Here they are all listed on this Wikipedia page.