# How to calculate the area under the curve with data given on an excel file, and not using the function "trapz"?

7 views (last 30 days)
Commented: Wan Ji on 10 Aug 2021
Create a MATLAB program to calculate the area under the curve for the strain-stress. The goal of the project is to have a MATLAB script that automatically reads the experimental data set. The MATLAB script must not use native functions like “trapz” in the calculation of the area. The goal is to calculate the area under the stress-strain curve.
Yes, indeed. Everything from scratch. Thank you.

Scott MacKenzie on 3 Aug 2021
Edited: Scott MacKenzie on 3 Aug 2021
T(end,:) = []; % remove last row, outlier
x = T.Strain;
y = T.Stress;
plot(x,y)
% compute area the hard way
a0 = 0;
for i=2:length(x)
a0 = a0 + (x(i)-x(i-1)) * (y(i)+y(i-1))/2;
end
fprintf('Area: %f\n', a0);
Area: 163.079188
% verify result using trapz
a1 = trapz(x, y);
fprintf('Area using trapz: %f\n', a1);
Area using trapz: 163.079188
Wan Ji on 10 Aug 2021
By using 'type trapz' command, one can see the vectorized trapz method, the code is simplified as:
z = diff(x,1,1).' * (y(1:end-1,:) + y(2:end,:))/2;