How to numerically calculate volume under the curve?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Young Chan Jung
am 23 Okt. 2024
Bearbeitet: Young Chan Jung
am 23 Okt. 2024
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.
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 23 Okt. 2024
Bearbeitet: John D'Errico
am 23 Okt. 2024
You have a surface. (Not a curve as I might say.) How do you compute the area under that surface? Simple. Just call trapz twice. Thus once in x, once in y. And trapz can do the job easily enough.
First, I'll make up some data to form a surface, where I know the true answer just so we can see if it works. (Well, I know it works...)
x = linspace(0,pi,20);
y = linspace(0,pi,30)';
z = sin(x).*sin(y);
surf(x,y,z)
Now, I expect the area under the surface to be 4.
syms X Y
int(int(sin(X)*sin(Y),X,[0,pi]),Y,[0,pi])
ans =
4
WHEW! The memory still works. You lose it if you don't use it. ;-)
dx = x(2) - x(1);
dy = y(2) - y(1);
Now, if you use trapz to integrate along one of the dimensions (first I did x), we get a vector. So ONE result for each value of y.
trapz(dx,z,2)
It is exactly as we did before, with nested calls to int. Integrate on x and then y.
trapz(dy,trapz(dx,z,2),1)
Nd that worked quite well. Not exact, but then, this is just trapezoidal rule.
1 Kommentar
Weitere Antworten (1)
Sahas
am 23 Okt. 2024
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!
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!