From: Shadi on
I have a specific question about using the "trapz" feature.

I'm trying to find the area under a curve which can be done using "trapz". However, I want to find the area of a curve under a specific range and not the entire curve. How can I do that?

thanks in advanced
From: Sadik on
Hi Shadi,

trapz can be told to integrate over a specific interval. In fact, you had better specify the interval since your deltaX will depend on that.

An example:

x = -1:0.01:1;
y = x.^2;

area1 = trapz(y);
area2 = trapz(x,y);

area1 here is in fact integrated over 1:length(y) where deltaX = 1, while area2 is integrated over -1:0.001:1, or x, and deltaX is 0.01. You can see, the second one is the correct one.

If you want to integrate from 0 to 1 instead of -1 to 1, then you have two choices:

1. Redefine x and y:

x = 0:0.01:1;
y = x.^2;

area = trapz(x,y);

2. If you do not have the analytical formula for y = f(x), you can find the index of the starting point of integration and ending point of it. Let's say

y = [-3 4 2 -1 8 5 -6];
x = [0 2 4 6 8 10 12];

and you want to integrate from 6 to 10. Then,

startingIndex = find(x==6);
endingIndex = find(x==10);

desiredX = x(startingIndex:endingIndex);
desiredY = y(startingIndex:endingIndex);
area = trapz(desiredX,desiredY);

Best.
From: Sadik on
I am sorry the following caught a typo:

....while area2 is integrated over -1:0.001:1,...

should have been

....while area2 is integrated over -1:0.01:1,...

Best.
 | 
Pages: 1
Prev: working with map
Next: Remapping a Matrix.