MATLAB Answers

Area Under Curve and above X axis only, and between x axis limits.

75 views (last 30 days)
Bhanu Pratap Singh
Bhanu Pratap Singh on 18 Dec 2019
Commented: Looky on 20 Dec 2019
I need to calculate the area under the curve. The curve may go down the x axis, but I dont' want to calculate that area. And then it may come up the axis again. Also, I need to calculate area in the limits of 1Hz to 6Hz, as shown in the image attached.
1.png
Please help out. I went through the trapz() function, but I don't know, how I can measure the area in this case. Thanks in advance.

  2 Comments

Looky
Looky on 18 Dec 2019
The easiest solution would be to make a copy of your data and just set everything below zero to zero. The use trapz:
y=yourYData(yourXData>1&yourXData<6);
x=yourXData(yourXData>1&yourXData<6);
y(y<0)=0;
area=trapz(x, y )

Sign in to comment.

Accepted Answer

Allen
Allen on 18 Dec 2019
If you are trying to determine the area under the curve between the frequencies of 1 Hz to 6 Hz for all y-values greater than 0, then Looky's suggestion will do the trick. However, if you are trying to determine the area under the curve between 1 Hz to 6 Hz and for all y-values greater than some threshold value other than zero, you will need to some additional adjustments before running trapz(). From the image you provided it looks like 1 V/H is your target threshold.
% Assign the y-value threshold to a variable. Makes is clearer to see how and where it is used,
% as well as makes is easy to change if needed.
thres = 1;
% Extract x- and y-data for frequencies in the desired range of 1-6 Hz.
x = yourXData(yourXData>1 & yourXData<6); % x-values in the range
y = yourYData(yourXData>1 & yourXData<6)-thres; % y-values in range & with a threshold adjustment
% Set all values that are <0 (these were originally all values less than the threshold) to zero.
y(y<0) = 0;
% Use trapz function to calculate the area under the curve.
area = trapz(x, y )

  2 Comments

Bhanu Pratap Singh
Bhanu Pratap Singh on 20 Dec 2019
Yes, I need the threshold to be 1. I think I can do just the following, to make all values less than the threshold to be zero. Thanks a lot for helping me out.
% Set all values that are < 1 (these were originally all values less than the threshold) to zero.
y(y<1) = 0;
Looky
Looky on 20 Dec 2019
Hey Bhanu,
no, this is not the same! Just take @Allen's Solution, it has a variable threshold via the thres variable, that is what you want.
Your solution will give you the area under the curve but enclosed with x-axis and not your threshhold at 1 !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!