|
"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
|