Using Trapz for Integral Evaluation

16 views (last 30 days)
S
S on 8 Feb 2014
Commented: Roger Stafford on 9 Feb 2014
Hi,
I have an expression I want to integrate with respect to THETA between 0 and 2pi.
The expression to be integrated is: sin^2THETA / r^3 Where: r = sqrt(2*cosTHETA)
So this is my attempt to evaluate this integral.
theta = 0:(2*pi/70):2*pi;
r = sqrt(2*cos(theta));
f = ((sin(theta)).^2) ./ ((r).^3);
I = trapz(theta,f);
Is this correct??
Thanks for any advice/suggestion.

Answers (2)

Roger Stafford
Roger Stafford on 9 Feb 2014
Edited: Roger Stafford on 9 Feb 2014
This integral is divergent, since theta crosses over 1/2*pi and 3/2*pi where the cos(theta) becomes zero. Having the cube of r in the denominator ensures that the integral will diverge to infinity at these places. This is therefore a decidedly inappropriate integration, not only for 'trapz', but for the other numerical matlab quadrature routines such as 'quad' and 'integral'. Moreover, over the range of theta from 1/2*pi to 3/2*pi the integrand becomes imaginary where cos(theta) is negative. Are you sure this is the integral you wish to solve? Perhaps if you tell us how you derived it, perhaps we could find a flaw in the reasoning.
  3 Comments
John D'Errico
John D'Errico on 9 Feb 2014
Sorry Walter, but this is a case where you are simply wrong. As long as the integral is formulated to integrate what was desired, trapz will be correct, to the limits of trapezoidal rule accuracy. You are still confused by your geometric interpretation of trapz, viewing it in Cartesian coordinates. But the fact is, had S called x the variable of integration, then you would have had no issue.

Sign in to comment.


Walter Roberson
Walter Roberson on 8 Feb 2014
It does not look correct to me. You are attempting to do a polar integration with a technique that assumes rectangular coordinates.
  6 Comments
John D'Errico
John D'Errico on 9 Feb 2014
Edited: John D'Errico on 9 Feb 2014
Walter - I think you are confusing the issue of converting an integral in cartesian form to an integral in polar form, with an integral already in polar form. Thus, you cannot simply append dr*dtheta to a function over a cartesian domain and perform an integral in polar coordinates.
However, IF the integral is properly formulated, with an appropriate differential element, then an integral over theta effectively DOES have parallel sides. Each trapezoidal side is indeed parallel in theta. Again, what matters is if the differential element is chosen properly, thus the transformation from cartesian to polar coordinates was done properly.
Once an integral is properly formulated as an integral, you could as easily call that variable x instead of theta, with limits of [0,2pi].
Roger Stafford
Roger Stafford on 9 Feb 2014
Edited: Roger Stafford on 9 Feb 2014
No, Walter, you are just flat out wrong on this issue! There is no other way to put it. The trapezoidal rule is so named because, if the two variables involved, namely the integrand and the variable of integration, are depicted in a graph, then the approximation that is made is that of a trapezoid approximating a section of an area beneath a curve. However, it does not follow that the actual interpretation of these variables must necessarily adhere to that graphical interpretation. The variable of integration might be, for example, time, and the other a varying current through a wire, with the integral therefore representing the accumulated charge on some condenser. These have nothing whatever to do with geometrical trapezoids. However, it is convenient for us mere mortals to think of that charge as the area under a curve whose height is the varying current level.
Just because "S" chose to name his variables r and theta does not mean that his integral is to be interpreted as having any relationship to polar coordinates, or even if they are polar coordinates, it does not mean that his integral referred to an area in the polar coordinate sense. He might have asked if 'trapz' was correct to use with the integral of sin(theta)^2*r^3, with r = 2*cos(theta), taken with respect to theta, or he might have asked for the integral of sin(x)^2*y^3 where y = 2*cos(x) taken with respect to x, and either way would have been a perfectly valid use of 'trapz'. 'Trapz' does not care about the names or the significances of the variables it uses in its computations. It just follows certain arithmetic rules to obtain a numeric answer which is designed to be an approximation to a desired definite integral value.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!