From: Justin Sharber on
Hi, I'm fairly new to Matlab. Does it have a function where I can find the area between two curves that cross, without my having to find the point of intersection myself? I know about trapz, which uses the trapezoidal rule to evaluate integrals. The problem is that it counts the area as positive if the curve is above the x axis, and negative if the curve is below the x axis. Thus, trapz(sin(x)) from 0 to 2*pi is zero. I would like to find a method such that Matlab always takes the absolute value of a trapezoidal segment (or whatever) so that the answer to an evaluation of sin(x) from 0 to 2*pi yields the number 4.
From: Jomar Bueyes on
On Mar 3, 2:21 pm, Justin Sharber <justin.shar...(a)gmail.com> wrote:
> Hi, I'm fairly new to Matlab.  Does it have a function where I can find the area between two curves that cross, without my having to find the point of intersection myself?  I know about trapz, which uses the trapezoidal rule to evaluate integrals.  The problem is that it counts the area as positive if the curve is above the x axis, and negative if the curve is below the x axis.  Thus, trapz(sin(x)) from 0 to 2*pi is zero.  I would like to find a method such that Matlab always takes the absolute value of a trapezoidal segment (or whatever) so that the answer to an evaluation of sin(x) from 0 to 2*pi yields the number 4.


Justin,

You just mentioned the solution to your problem. You want absolute
value, then integrate abs(sin(x)).

HTH

Jomar
From: John D'Errico on
Jomar Bueyes <jomarbueyes(a)hotmail.com> wrote in message <84e3aea4-5cd4-4464-8787-fd191469e92b(a)g11g2000yqe.googlegroups.com>...
> On Mar 3, 2:21 pm, Justin Sharber <justin.shar...(a)gmail.com> wrote:
> > Hi, I'm fairly new to Matlab.  Does it have a function where I can find the area between two curves that cross, without my having to find the point of intersection myself?  I know about trapz, which uses the trapezoidal rule to evaluate integrals.  The problem is that it counts the area as positive if the curve is above the x axis, and negative if the curve is below the x axis.  Thus, trapz(sin(x)) from 0 to 2*pi is zero.  I would like to find a method such that Matlab always takes the absolute value of a trapezoidal segment (or whatever) so that the answer to an evaluation of sin(x) from 0 to 2*pi yields the number 4.
>
>
> Justin,
>
> You just mentioned the solution to your problem. You want absolute
> value, then integrate abs(sin(x)).

This will indeed work. But remember that once
you put an absolute value inside this function,
it will reduce the accuracy of your numerical
integration. Essentially you have just created a
function with a derivative singularity in it.

You will indeed get better results from trapz if
you know where that singularity resides, and
are able to integrate around it by breaking the
domain down into sub-domains with no
singularities at all.

John