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:
use of trapz

Subject: use of trapz

From: Shadi

Date: 31 Mar, 2010 09:08:02

Message: 1 of 3

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

Subject: use of trapz

From: Sadik

Date: 31 Mar, 2010 09:51:04

Message: 2 of 3

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);


Subject: use of trapz

From: Sadik

Date: 31 Mar, 2010 10:04:04

Message: 3 of 3

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,...


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