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:
Calculating the area enclosed by a plot

Subject: Calculating the area enclosed by a plot

From: George

Date: 5 Jan, 2011 20:53:05

Message: 1 of 4

Recently I undertook the testing a a spring damper system. The tests involved cycling the unit with a sinusoidal displacement and recording the force.

From these tests plots are created which look like this: http://www.flickr.com/photos/57922959@N02/5328255652/

I need to find the area enclosed by the data, can Matlab do this?
I have tried to use the polyarea command to no success (areas that I know to be larger than others come out smaller)

Thanks
George.

Subject: Calculating the area enclosed by a plot

From: Sean de

Date: 5 Jan, 2011 21:42:05

Message: 2 of 4

"George " <mrhotpotato@hotmail.co.uk> wrote in message <ig2lnh$lgp$1@fred.mathworks.com>...
> Recently I undertook the testing a a spring damper system. The tests involved cycling the unit with a sinusoidal displacement and recording the force.
>
> From these tests plots are created which look like this: http://www.flickr.com/photos/57922959@N02/5328255652/
>
> I need to find the area enclosed by the data, can Matlab do this?
> I have tried to use the polyarea command to no success (areas that I know to be larger than others come out smaller)
>
> Thanks
> George.

help trapz
You'll have to call it twice: integrate the top part, subtract the integral of the bottom part. The index that corresponds to the point between the two parts is:
[trash, idx] = max(x_values);
so
the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)

Good Luck!

Subject: Calculating the area enclosed by a plot

From: George

Date: 6 Jan, 2011 23:12:06

Message: 3 of 4

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ig2ojd$r6$1@fred.mathworks.com>...
> "George " <mrhotpotato@hotmail.co.uk> wrote in message <ig2lnh$lgp$1@fred.mathworks.com>...
> > Recently I undertook the testing a a spring damper system. The tests involved cycling the unit with a sinusoidal displacement and recording the force.
> >
> > From these tests plots are created which look like this: http://www.flickr.com/photos/57922959@N02/5328255652/
> >
> > I need to find the area enclosed by the data, can Matlab do this?
> > I have tried to use the polyarea command to no success (areas that I know to be larger than others come out smaller)
> >
> > Thanks
> > George.
>
> help trapz
> You'll have to call it twice: integrate the top part, subtract the integral of the bottom part. The index that corresponds to the point between the two parts is:
> [trash, idx] = max(x_values);
> so
> the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)
>
> Good Luck!

Thank you for the help. Worked Perfect.
George.

Subject: Calculating the area enclosed by a plot

From: Brad

Date: 14 Nov, 2014 20:08:05

Message: 4 of 4

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ig2ojd$r6$1@fred.mathworks.com>...
> "George " <mrhotpotato@hotmail.co.uk> wrote in message <ig2lnh$lgp$1@fred.mathworks.com>...
> > Recently I undertook the testing a a spring damper system. The tests involved cycling the unit with a sinusoidal displacement and recording the force.
> >
> > From these tests plots are created which look like this: http://www.flickr.com/photos/57922959@N02/5328255652/
> >
> > I need to find the area enclosed by the data, can Matlab do this?
> > I have tried to use the polyarea command to no success (areas that I know to be larger than others come out smaller)
> >
> > Thanks
> > George.
>
> help trapz
> You'll have to call it twice: integrate the top part, subtract the integral of the bottom part. The index that corresponds to the point between the two parts is:
> [trash, idx] = max(x_values);
> so
> the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)
>
> Good Luck!

I realize this thread is old but I am performing a similar task as the OP and wanted to check my hand calcs from a printout to the trapz method in matlab. I was getting a drastically different answer than my hand calcs so I ran a trapz on each individual output

upper_area = trapz(x(1:idx),y(1:idx)) = positive value
lower_area = trapz(x((idx+1):end),y((idx+1):end)) = negative value

I discovered that the trapz output for the bottom part of the curve generated a negative value. I think it has to do with the fact that the displacement is decreasing as the array index goes up. I changed the equation to add the two together and I got within 1.1Nmm of my hand calc.

Just an FYI for anyone else that reads this in the future.

the_area = trapz(x(1:idx),y(1:idx))+trapz(x((idx+1):end),y((idx+1):end)

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