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:
How to calculate area of enclosed loop?

Subject: How to calculate area of enclosed loop?

From: J K

Date: 11 Apr, 2013 05:43:11

Message: 1 of 7

I get data(x,y), and I plot it with Matlab. The graph is an enclosed loop. How to calculate the area?

The idea is take the upward integral subtract the lower integral. But I don't kow how to translate it into Matlab code.

Can you guys help me with the code?

Subject: How to calculate area of enclosed loop?

From: Nasser M. Abbasi

Date: 11 Apr, 2013 06:07:34

Message: 2 of 7

On 4/11/2013 12:43 AM, J K wrote:
> I get data(x,y), and I plot it with Matlab. The graph is an enclosed loop.
>How to calculate the area?
>
> The idea is take the upward integral subtract the lower integral. But I don't kow
>how to translate it into Matlab code.
>
> Can you guys help me with the code?
>

It sounds that you are looking for Monte Carlo method?

i.e. generate many random points (using uniform distribution, make x and
y) and see the proportion of times the random point (x,y) is inside
the loop vs. outside the loop (easy to do since you know the max and min
values of your data.

This ratio is the same as the ratio of the area of the square that encloses
the loop. (you start by making a large square to put the loop inside).

Since you know the area of the outside square, then now you
know the area of the enclosed loop.

or something like that.

--Nasser

Subject: How to calculate area of enclosed loop?

From: J K

Date: 11 Apr, 2013 06:35:11

Message: 3 of 7

 It sounds that you are looking for Monte Carlo method?
>
> i.e. generate many random points (using uniform distribution, make x and
> y) and see the proportion of times the random point (x,y) is inside
> the loop vs. outside the loop (easy to do since you know the max and min
> values of your data.
>
> This ratio is the same as the ratio of the area of the square that encloses
> the loop. (you start by making a large square to put the loop inside).
>
> Since you know the area of the outside square, then now you
> know the area of the enclosed loop.
>
> or something like that.
>
> --Nasser
>

Monte Carlo Method is something new for me. Do you have any alternative method (trapz or polyarea)?

Subject: How to calculate area of enclosed loop?

From: Bruno Luong

Date: 11 Apr, 2013 06:40:08

Message: 4 of 7

"J K" wrote in message <kk5ihf$es8$1@newscl01ah.mathworks.com>...
> I get data(x,y), and I plot it with Matlab. The graph is an enclosed loop. How to calculate the area?
>
> The idea is take the upward integral subtract the lower integral. But I don't kow how to translate it into Matlab code.
>
> Can you guys help me with the code?

polyarea(x,y)

Bruno

Subject: How to calculate area of enclosed loop?

From: J K

Date: 11 Apr, 2013 06:56:07

Message: 5 of 7


> polyarea(x,y)
>
> Bruno


I though polyarea is for polygon(with straight line). http://www.mathworks.com/help/matlab/ref/polyarea.html

Mine is irregular line.

Can it be done?

Subject: How to calculate area of enclosed loop?

From: Roger Stafford

Date: 11 Apr, 2013 07:20:08

Message: 6 of 7

"J K" wrote in message <kk5ihf$es8$1@newscl01ah.mathworks.com>...
> I get data(x,y), and I plot it with Matlab. The graph is an enclosed loop. How to calculate the area?
- - - - - - - - - -
  I assume when you say "an enclosed loop" you mean a closed curve in the x-y plane - one that ends back at its start point. I believe there is a routine called "polyarea" which will do this but you can easily compute it yourself. Let x and y be vectors with cartesian coordinates that trace the points of the curve from beginning to end.

 area = 1/2*sum(x.*y([2:end,1])-y.*x([2:end,1]));

  If your curve follows around the curve in a counterclockwise direction as you proceed through x and y, this will give the positive area inside. With a clockwise direction it gives the negative of the area. Take its absolute value if you don't want that. (The curve should not cross over itself, as in a figure eight; otherwise part of its area would be positive and part negative, partially canceling one another.)

Roger Stafford

Subject: How to calculate area of enclosed loop?

From: J K

Date: 11 Apr, 2013 12:28:09

Message: 7 of 7

> I assume when you say "an enclosed loop" you mean a closed curve in the x-y plane - one that ends back at its start point. I believe there is a routine called "polyarea" which will do this but you can easily compute it yourself. Let x and y be vectors with cartesian coordinates that trace the points of the curve from beginning to end.
>
> area = 1/2*sum(x.*y([2:end,1])-y.*x([2:end,1]));
>
> If your curve follows around the curve in a counterclockwise direction as you proceed through x and y, this will give the positive area inside. With a clockwise direction it gives the negative of the area. Take its absolute value if you don't want that. (The curve should not cross over itself, as in a figure eight; otherwise part of its area would be positive and part negative, partially canceling one another.)
>
> Roger Stafford

Yes, that's exactly what I mean.
And thank you for replying.

In fact, thank you ALL for replying me.

Just one more thing, is the function "polyarea" and "trapz" the same? Because they gave me almost the same answer.

Tags for this Thread

No tags are associated with 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