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

조회 수: 51 (최근 30일)
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.
  댓글 수: 4
James Tursa
James Tursa 2021년 8월 3일
I meant are you supposed to write your own area calculator function from scratch? (be it Euler or trapezoid or whatever)

댓글을 달려면 로그인하십시오.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 8월 3일
편집: Scott MacKenzie 2021년 8월 3일
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/702117/Steel6150Tens.csv';
T = readtable(f);
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
  댓글 수: 8
Wan Ji
Wan Ji 2021년 8월 10일
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;

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by