Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Computing area under curve

Subject: Computing area under curve

From: naseeb

Date: 3 Mar, 2012 19:33:22

Message: 1 of 3

I am trying to compute area under the curve using trapz.

The relationship of variables are as:

x = f(z)
y = f(z)

plot(x,y)

It plots x, y over the interval of z.

Now I am interested to find area under the plot (x,y). When I went through the documentation of trapz it seems like my problem is not exactly the same for how they defined trapz(x,y). Please suggest me on how to use it.

Subject: Computing area under curve

From: Roger Stafford

Date: 3 Mar, 2012 20:33:34

Message: 2 of 3

"naseeb " <adnan.iut@gmail.com> wrote in message <jitrm2$g49$1@newscl01ah.mathworks.com>...
> I am trying to compute area under the curve using trapz.
> > The relationship of variables are as:
> x = f(z)
> y = f(z)
- - - - - - - - -
  (Did you mean x = f(z), y = g(z) with f and g different functions so that x and y need not be equal?)

  Provided that x = f(z) increases monotonically over the range of z, you can get the (approximate) area under the curve from z = a to z = b using 'trapz' as:

 z = linspace(a,b,n);
 x = f(z);
 y = g(z);
 area = trapz(x,y);

The accuracy of this depends on how finely the z interval [a,b] is divided up, that is, how large n is.

  If f(z) is not monotonically increasing, the areas where x is decreasing will be subtracted from the area calculation. For example if you have:

 z = linspace(0,2*pi,8192);
 x = -cos(z);
 y = sin(z) + 10;
 A = trapz(x,y);

As z goes from 0 to pi, the x variable increases from -1 to +1 and the area computed there by trapz will be positive, but from pi to 2*pi x will be decreasing and the value computed by trapz there will be negative. That will cause the area under the bottom half of a unit circle to be subtracted from the area under the top half, giving you just the area of the circle itself, namely, pi. My computer gives 3.141592345 which is good to 7 decimal places.

Roger Stafford

Subject: Computing area under curve

From: Roger Stafford

Date: 3 Mar, 2012 21:14:36

Message: 3 of 3

"Roger Stafford" wrote in message <jitv6u$q1r$1@newscl01ah.mathworks.com>...
> Provided that x = f(z) increases monotonically over the range of z, you can get the (approximate) area under the curve from z = a to z = b using 'trapz' as:
>
> z = linspace(a,b,n);
> x = f(z);
> y = g(z);
> area = trapz(x,y);
> ........
- - - - - - - - -
  I should have mentioned that in places where x is increasing but where y becomes negative, that is, places where your curve dips below the x-axis, the area computed there by trapz(x,y) will be negative. On the other hand if y is negative and x is decreasing then you would again get positive results for that portion.

  In the example I gave with a circle, if the 10 were replaced by, say, -10 yielding negative values for y, the final result would still have been just the same; the separate area values for the two half ranges of z would each have had 40 subtracted from them, but their algebraic difference would remain unchanged.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us