How to calculate partial area under the curve from an excel sheet and automate it for 100s of excel sheets.

10 views (last 30 days)
Hello,
I have data from nanoindentation testing for which I want to calculate the area under the curve for specific length and then automate it for 100s of curve.
Given below is the full range of the curve (leftside graph) and the specific length for which area under the curve needs to be calculated (rightside graph)
The main concerns while using trapz function: 1. the data is not increasing continuously that means some of the values of trapz will be negative and I am not sure how much it will affect the final value of area under the curve. When comparing the values from excel it is different. I am not sure which one is correct
Reading table and plotting curve
T = readtable("316.csv");
T(end,:) = []; % remove last row, outlier
x = T.Strain;
y = T.Stress;
plot(x,y)
Select the cells for area under the curve
lims = (x >= 0) & (x <= 0.17);
a1 = trapz(x(lims),y(lims));
fprintf('Area using trapz: %f\n', a1);

Answers (1)

Nipun
Nipun on 16 Apr 2024 at 10:52
Edited: Nipun on 16 Apr 2024 at 10:53
Hi Soumya,
I understand that you want to calculate the area under the curve for a plot depicted by empirical samples. I also understand that you intend to calculate the area only pertaining to the right hand side of the plot.
I recommend using the "trapz" function in MATLAB which uses trapezoidal numerical integral approximation to calculate area under the curve of a given dataset. Note that this function is specially useful for discontinuous data points (all samples are discontinuous, only symbolic functions can be continuous).
In your code, where you have set the limits for the area range, you might want to set "y>=0" so that no negative value is taken while calculating the area under the curve. Below is the code where I calculate the area under the curve of a sine wave from 0 to 2*pi with and without "y>=0"
x = linspace(0,2*pi,20);
y = sin(x);
plot(x,y, '-o');
lims = x>0 & x<7;
area_without_y_lim = trapz(x(lims), y(lims))
area_without_y_lim = -0.0537
lims = x>0 & x<7 & y>=0;
area_with_y_lim = trapz(x(lims), y(lims))
area_with_y_lim = 1.9145
In this first output, the negative and positive values cancel each other, yielding almost zero area. In the second, I hard set the sample points to be positive and hence get an area of 2 (approximately) which is same as area under curve of a sine function from 0 to pi.
For more information on "trapz" function in MATLAB, refer to the following MathWorks documentation:
Hope this helps.
Regards,
Nipun

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!