Now, let us discuss a bit about basic integration methods.
And here in this figure, I try to explain to you the problems that occur when we take two simple methods
for integration.
So as you probably know, when we integrate a function from boundary to boundary B, we basically try
to calculate the area under the curve.
So basically this red area here between the blue curve and the x axis.
And if we try to calculate this integral starting from minus three to plus three, then when we do it
numerically, we have to, first of all, find out some points of the graph, of course.
So we must determine d the f of X values of these red dots here.
And then what we can do is we can transform the integral, which is like a continuous treatment of this
problem.
We can transform it into a sum.
And now the question is, how do we calculate the sum exactly?
There exists several methods, and the most simple one would be to just consider basically a weighted
sum.
So basically, you take all of these values here and you add them up and then you just take into account
the size or the distance between two of these dots.
So basically the width of these bars and then you just add up all these rectangles here, and this will
approximately give you the integral.
However, you can see already a problem that occurs, especially when you have just a few of these points
and also when the function doesn't go to zero at the boundaries.
This is that the and the end point.
So here, minus three and plus three are over pronounced because here we are not really calculating
the integral from minus three to three.
But we calculate the integral from, let's say, minus three point two five two plus three point two
five.
So this is a bit of a problem here.
Our range is not perfectly determined.
So better methods have to be found.
And a very simple idea would be to just say that we can take the edge values of the interval over which
we integrate.
And this point here will only be taken into account with half the width or half the weight.
You could also see.
So you see this rectangle here has only half the width of the neighboring rectangle and then at least
the interval the range over which we integrate is correct.
And then there exist many other methods.
For example, this truck has trapezoid methods, or we can also derive more advanced methods that give
you a much better value for the integration.
So let's get started and let's define the function and I have plotted here, and let's create the data
points that we will use for our integration.
And also, of course, let's first discuss the analytical results, which will be our reference point
for evaluating the quality of these integration methods.
So first of all, as always, we will load our modules are Nampai and Plotnick.
And then we will define a function.
So this will be the function that we want to integrate, and the function is 0.5 plus zero point one
times x plus 0.2 times x squared.
And then we have the last term 0.03 times X to the power of three.
So it's a polynomial of third order.
And if we want to plot it, we just write X list is in a line space and put in space.
Do you see the plot range approximately, let's say, minus three point five to three point five and
seventy one points?
And then you plot the function by writing plot of plot x list and then the function acting on the X
list.
And here we go.
This is our functions the same as is shown here.
We can, of course, also change the Y range and we can add labels for the axes.
I think it's OK to just leave it as it is because we have the plot already here.
So now for the analytical solution, here we have again the function.
And when we calculate the area from minus three to three under the curve, then we have to integrate
over this function.
And so basically we have to increase the power of the X and then take into account the factor here.
So basically, when we arrived this function, we should end up with our initial function.
So you can test this, the derivative is one five plus one over 10 x plus one over five x squared plus
three over 100 x to the power of three.
And then we have here the boundaries from minus three to plus three.
So now we just have to calculate these two terms here.
So I copy this line of code from my other notebook and then run it.
So these are just these two terms.
And the solution is six point six, and this will be the reference point for the integral of this red
area.
And then we want to consider here a situation where let's say we have our data could be some experimental
measurement or could be just some.
Some data points that we have from have taken from the internet of some function.
Or maybe we did a survey or something like that.
And so we don't know the function behind it yet.
So we only know the data points that we are going to create next.
So the data points aren't the ones that I have plotted here and read.
This is the only information that we have about the graph.
So we want to make the situation as difficult as possible.
So the X points will be a in space and it goes from minus three to three and we have 13 points because
then we have a distance of the neighboring points of zero point five.
So you see here we have 13 points
and then the data itself will be an every and detailed array and the X coordinates will, of course,
be the X points and then the Y coordinates will be the function acting on the X points.
So this will be our data and then we can just plot the data and this will just be PLG dot scatter data
zero and data one.
So this will be the situation that we will start with.
We have just these data points and we try to calculate the integral over the corresponding function,
which we don't know from the area minus three to three.
This we will do by different methods and we will start in the next lecture.