How to numerically calculate volume under the curve?
Show older comments
Hello, I am plotting surface profile with x,y,z data.
I am now able to plot the surfaces, thank you for the help.
The surface was scanned in x-direction, z-direction is the height. After one x-z scan, the x-z data for the next y is recorded and so on.
I have been using trapazoidal rule (trapz command) to find area under the curve.
Is there a command or built in methods for the calculation of volume under the curve?
Thank you.
Accepted Answer
More Answers (1)
As per my understanding, you calculated the area under the curve using MATLAB's "trapz" function and would like to calculate the volume under the curve using other inbuilt functions.
Refer to the following sample code below which is using "trapz" and "integral2" functions and modify it as suitable to you:
% Define a grid for X and Y
x = [0, 1, 2, 3, 4]; % x-direction
y = [0, 1, 2]; % y-direction
% Define Z as a matrix, where each row corresponds to an x-z scan at a specific y
% For simplicity, let's assume Z = X^2 at each y
Z = [0, 1, 4, 9, 16; % y = 0
0, 1, 4, 9, 16; % y = 1
0, 1, 4, 9, 16];% y = 2
% Calculate the area under each x-z profile
area_x = trapz(x, Z, 2); % Integrate along each row (x-direction)
% Integrate the resulting areas over the y-direction
volume_trapz = trapz(y, area_x);
disp(volume_trapz);
% Define the function for Z
Z_fun = @(x, y) x.^2;
% Define the limits for x and y
x_min = 0;
x_max = 4;
y_min = 0;
y_max = 2;
% Calculate the volume using integral2
volume_integral2 = integral2(Z_fun, x_min, x_max, y_min, y_max);
disp(volume_integral2);
Refer to the following MathWorks documentation link for more information on the "trapz" and "integral2" functions:
- https://iwww.mathworks.com/help/matlab/ref/trapz.html
- https://www.mathworks.com/help/matlab/ref/integral2.html
Hope this is beneficial!
1 Comment
Young Chan Jung
on 23 Oct 2024
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!