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:
Calculate area of enclosed plot?

Subject: Calculate area of enclosed plot?

From: J K

Date: 10 Apr, 2013 06:37:10

Message: 1 of 4

http://www.mathworks.com/matlabcentral/newsreader/view_thread/300254
I refer to link above, but I don't quite get the part
the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)

What does idx mean?

I'm a novice Matlab user, please be patient with me.

Thank you.

Subject: Calculate area of enclosed plot?

From: Barry Williams

Date: 10 Apr, 2013 10:40:07

Message: 2 of 4

"J K" wrote in message <kk31am$hop$1@newscl01ah.mathworks.com>...
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/300254
> I refer to link above, but I don't quite get the part
> the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)
>
> What does idx mean?
>
> I'm a novice Matlab user, please be patient with me.
>
> Thank you.

The OP asked how to pick out the area enclosed by curves with the same x and y values at the minimum and maximum values of x. The function max can have two outputs. The first is the maximum value returned by the function, and the second is the index of the maximum value. The line below looks for the maximum value of of x, returns it as trash, and the index of the maximum value as idx:
[trash, idx] = max(x_values);
We have to assume that all of the x and y data for both curves are contained in a single array of x values and a single array of y values. So [trash idx] = m(x_values) returns the maximum value of x as trash, which we don't need, and its index value as idx.
The index value of the first x data point is, of course, 1. So x(1:idx) then represents all the values of x in the first curve. The next point in the x array, x(1+idx), is the first x point in the other curve, and x(1+idx:end) includes all of the x values in the second curve. The rest just captures the corresponding y values and uses trapz to integrate and then subtract the lower curve from the upper one, giving the enclosed area.
Barry

Subject: Calculate area of enclosed plot?

From: J K

Date: 10 Apr, 2013 12:33:08

Message: 3 of 4

"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <kk3fi7$or9$1@newscl01ah.mathworks.com>...
> "J K" wrote in message <kk31am$hop$1@newscl01ah.mathworks.com>...
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/300254
> > I refer to link above, but I don't quite get the part
> > the_area = trapz(x(1:idx),y(1:idx))-trapz(x((idx+1):end),y((idx+1):end)
> >
> > What does idx mean?
> >
> > I'm a novice Matlab user, please be patient with me.
> >
> > Thank you.
>
> The OP asked how to pick out the area enclosed by curves with the same x and y values at the minimum and maximum values of x. The function max can have two outputs. The first is the maximum value returned by the function, and the second is the index of the maximum value. The line below looks for the maximum value of of x, returns it as trash, and the index of the maximum value as idx:
> [trash, idx] = max(x_values);
> We have to assume that all of the x and y data for both curves are contained in a single array of x values and a single array of y values. So [trash idx] = m(x_values) returns the maximum value of x as trash, which we don't need, and its index value as idx.
> The index value of the first x data point is, of course, 1. So x(1:idx) then represents all the values of x in the first curve. The next point in the x array, x(1+idx), is the first x point in the other curve, and x(1+idx:end) includes all of the x values in the second curve. The rest just captures the corresponding y values and uses trapz to integrate and then subtract the lower curve from the upper one, giving the enclosed area.
> Barry


There's something wrong with this expression. My x values is V, and my y values is Pressure. I need to find the work done, that's area enclosed by the plot. But I get error.
From Matlab:
??? Work = trapz(V(1:idx),Pressure(1:idx))-trapz(V((idx+1):end),Pressure((idx+1):end)
                                                                                     |
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

Subject: Calculate area of enclosed plot?

From: dpb

Date: 10 Apr, 2013 12:41:05

Message: 4 of 4

On 4/10/2013 7:33 AM, J K wrote:
...

> ??? Work =
> trapz(V(1:idx),Pressure(1:idx))-trapz(V((idx+1):end),Pressure((idx+1):end)
> |
> Error: Expression or statement is incorrect--possibly unbalanced (, {,
> or [.

Count/match paren's....

--

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