Lecture 65

IIT Kanpur July 201
20 followers
Oct 22, 2023
So far in this lecture, we have talked about different techniques of evaluating numerical
integration.
We have talked about trapezoidal rule, Simpson's 1 3rd and 3 8th rule.
We have discussed few examples on trapezoidal and Simpson rules.
And we talked about Gaussian quadrature.
We'll now come to double integral.
And specifically, we want to point out that double integral requires use of these techniques.
It's not a new tool.
It's just repetitive use of these techniques will help us to evaluate double, triple or
any higher integrals.
So let us see how we do it.
Let us go back to our problem of evaluating area of a circle.
Here we have an unit circle, that is, r equals 1.
The center of the circle is at the origin, that is, 0.00.
And we have discussed that area of this circle is 4 times 0 to 1 y dx.
And y in this case is 1 minus x square, since this circle is x square plus y square equals
1.
When we write y dx, this y dx is actually this small area here, strip of area over this
dx and y.
Since this is small, this is treated as a rectangle.
Therefore, the area is y dx.
And then we span this idea from x equals 0 to x equals 1.
Another way of looking at this problem is using double integral.
That means integral over the area over dy dx.
In this case, the area dy dx is a small rectangle here.
And then we span this small rectangle over the entire space.
So it can be written as x goes to 0 and 1, y goes to 0, root over 1 minus x square.
What does that mean is that we are first we are evaluating this part.
In this part, this small area I am spanning in this y direction.
I am calculating area over here.
And then I am going in x direction, then I am taking it in this direction.
So as I go in y direction, I actually get this kind of an area.
And you remember that this area we were taking in x direction.
So we are then taking in x direction.
So here we sweep in y direction first, I get the area in y direction and then I sweep in
x direction.
Another way is of course, I sweep it in x direction first.
That means I get an area like this first.
And then I go in y direction, then the order of integration will be different.
However, here even though I am sweeping in x direction first and then y direction, the
results from both of this expression must be same.
Otherwise, we will have a problem because both of them indicate the area of the circle.
It should not matter that in which way I do the integration.
Now how do we actually evaluate this integral analytically?
When we analytically evaluate this integral, what we do here, this part, we complete it
first within the third bracket.
This part we complete it first.
And when we do this integration, we completely forget about what is there outside it.
So as I do this integration, this integration is y within limit 1 minus x square and 0.
So this integration will be root over 1 minus x square.
So it comes x equals 0 to 1, 4 root over 1 minus x square dx.
We have to evaluate this integral.
So this is the first step.
Second step is evaluating this.
And analytically we know how to evaluate it.
So it's basically we replace x with sine theta and then it is 1 minus sine square theta.
Then we can write dx equals cos theta d theta.
x equals 0 gives theta equals 0 and x equals 1 gives theta equals pi by 2.
So I have limit, I have changed 0 to pi by 2, 1 minus sine square theta cos theta.
And it gives us 4, 0 to pi by 2 cos square theta.
1 minus sine square theta is cos square theta and 1 cos theta here.
Then this cos square theta, I replace it by 1 plus cos 2 theta, which is equal to 2
cos square theta.
So this becomes 2.
Now we can perform the integration and it will come down as 2 theta, sine 2 theta by
2 here.
But that really doesn't matter within 0 to pi by 2 limit, this becomes 0.
Only this theta remains, therefore it gives us the value pi, which is indeed the area
of this unit circle.
Now see that we are getting pi, we are not really getting the value of pi, which we were
getting when we numerically evaluated the integral.
So in certain cases, as you see that even though analytical solutions are available,
numerical solutions we still perform to have more information.
Anyway so overall procedure in analytical method for double integral is what we have
just seen.
We have two single integrals we have evaluated, that's it.
But numerically, this is slightly different.
So let us see the example taking trapezoidal method.
So in trapezoidal method, the rule is I equals h by 2, y naught plus 2y1, 2y2, etc, etc.
If I want to use this to evaluate this integral, what I will do, I will treat this as my y
and I will evaluate this y at different location, this y naught, y1, y2, etc are value of y
at different x location, which means each of this y naught, y1, y2, etc are actually
are this integral, value of this integral for different x.
What kind of value?
So for instance, y naught, we start from x equals 0, this integral y0 to root over
1 minus x square for x equals 0, this is one particular x.
Similarly y1, y equals 0, 1 minus x square for x equal to 0 plus h, so on and so forth.
This is how each of this when we evaluate, we have to evaluate this integral.
This is once again, numerically we have to evaluate integral.
So all of this y naught to yn, we are getting by numerically evaluating the integral.
So it requires a series of integration.
So numerical double integral is essentially a series of single integral as opposed to
the analytical double integral, which is basically two single integrals.
Here there are lot of single integrals.
So overall the process is way more complicated now.
So we would like to see that how this process can be implemented in a computer.
But since this process is pretty complicated, we will do in a slightly different way.
So let us say we wish to find out the area of the circle or to be specific the value
of pi, we wish to find out using the double integral.
I will directly use the scipy routine.
This is a Python package, scientific Python.
So this scientific Python package I will use and I will evaluate this four times this area.
This is the area of quarter circle and four times this quarter circle area will be the
value of pi.
So here in scipy integrate, there is this DBL quad.
This gives a double integral using Gaussian quadrature.
So first we have to define the function that we are going to integrate and that is the
integrand and integrand in this case that is simply one because I have only one here
I have nothing else.
So integrate in one, then x lower limit is 0, x upper limit is 1, 0 to 1, then y lower
limit could be a function of x that is y lambda x, but in this case it is 0, y upper limit
is also it could be a function of x.
So this is root over 1 minus x square, this mt is the math.
So this square root to evaluate square root, we are using this math package.
So this is the lower limit of y, upper limit of y is root over 1 minus x square.
So now we are ready.
We can actually calculate this integral simply from scipy, within scipy I have to give the
input of this integrand, integrand means this function here, then lower limit of x,
upper limit of x, lower limit of y and upper limit of y and it will calculate two quantities.
One is the result of that integral, another is the error.
So this routine uses Gaussian quadrature, in fact it uses 10 point Gaussian quadrature
and so it is a very, very high accuracy result and then it even compares the result with a
higher order quadrature and between these two quadrature what is the difference that it
calculates as error.
So this error is not the difference with analytical solution because you cannot have analytical
solution for all kinds of function.
Error is between two different evaluation of integral.
The basic is this 10 point Gaussian quadrature and then there is an even a more accurate
estimation and between these two they calculate the error which is usually very, very small.
If I run this case, the output comes as 3.14159 etcetera, etcetera.
So this is a very accurate value of pi, you can see it.
So this Gaussian quadrature it has given us highly accurate value.
We can take one more example that is the calculating the centroid of this quarter circle
using the same routine.
So in this case, we have to understand that how we can calculate the centroid.
So the theory suggests that if we have this centroid coordinate xc, yc,
recall that we are taking the centroid for this quarter circle here.
This is 1, x equals 1.
So xc will be x da integral of that divided by da.
This is the theory and I really do not want to go to the theory.
This is moment of area divided by area.
You can open up any physics or mechanics book to get the theory.
But this is xc.
Therefore, here I put this x.
So my integrand is now x and this da integration da is of course, a and that is pi by 4.
So here this is pi by 4, this integration divided by pi by 4.
Why xc and yc are equal?
Well, because of symmetry, if I have xc here, there is no reason why yc will be somewhere else.
So because of symmetry, we understand it will be here.
This problem is a standard problem in mechanics as well as in geometry.
You can evaluate this integral analytically and you will find that this xc will be 4r by 3 pi,
where this r is the radius of the circle.
So here if I run this case, so please see that there is no difference between the previous case
and this case.
Everything else is same.
Only thing I have changed here is that here the integrand has changed to x and the result means
this integral divided by pi by 4.
So 4 divided by pi, this mt dot pi is math dot pi because pi is coming from this math package
of Python.
And then dbl quad is the double integral package.
So I am calling this double integral package with all these inputs.
All right, if I run this program now, I will get 0.424413
and the analytical solution is 0.424413 of r.
And in this case, of course, we are taking r equals 1 case.
So this is r equals 1 case we have computed here.
And you see, this is the very, very high accuracy of this Gaussian quadrature.
So now we can summarize.
We have seen that numerically double integrals are equivalent to many single integrals.
Unlike analytically where double integrals means two integrals.
If I want to extend this idea to triple integral, then it's simply straightforward.
Also, I have shown you the formulation using trapezoidal rule.
But if you want to use Simpson's rule or some other rule, that is also straightforward.
Gaussian quadrature is a highly accurate scheme.
And that is why it is used in many software.
You have already seen it is in SciPy.
And SciPy actually takes this package from our old Fortran routine,
where also this Gaussian quadrature is implemented with very high accuracy.
Gaussian quadrature is also used in finite element packages because of its high accuracy.
For your exercise, you write a pseudocode or write a computer program
to numerically evaluate this integral.
You can also try an analytical solution of that.
If you do not have the scope of writing a program, just focus on the pseudocode part.
If you can run this code, which I have just shown, you can take that code and change it like this
and try to see that if you are getting the right result.
That's all for this lecture.
Thank you and I'll see you in next class.
