How can I make a integration with trapz?
Ältere Kommentare anzeigen
Hi everyone,
I have x and y vectors, and ploted y verses x as below:
v=[2.2696;2.2696; 2.4646;2.4646;2.6775;2.6775; 2.9101;2.9101;3.1644];
x=[0 , -0.0476 , -0.0476 , -0.0952 , -0.0952 , -0.1429 , -0.1429 , -0.1905 ,-0.1905];
plot (x,v)
I want to find the inegral (The area under the curve) of curve in range of x=[0 and -0.08]. How can I do that?
Thank you in advance,
Akzeptierte Antwort
Weitere Antworten (2)
Davide Masiello
am 21 Apr. 2022
Bearbeitet: Davide Masiello
am 21 Apr. 2022
I don't think you really need trapz for this function
clear,clc
x=[0,-0.0476,-0.0476,-0.0952,-0.0952,-0.1429,-0.1429,-0.1905,-0.1905];
y=[2.2696;2.2696; 2.4646;2.4646;2.6775;2.6775; 2.9101;2.9101;3.1644];
plot (x,y)
x1 = [0,-0.0476,-0.0476,-0.08];
y1 = [2.2696;2.2696; 2.4646;2.4646];
dx = diff(unique(x1,'stable'))
dy = unique(y1,'stable')'
area = abs(sum(dx.*dy))
4 Kommentare
Bruno Luong
am 21 Apr. 2022
Bearbeitet: Bruno Luong
am 21 Apr. 2022
Doesn't seem to be correct, with unique you replace the original step function by a linear function. The correct result is
2.2696*(0.0476-0)+2.4646*(0.08-0.0476)
Davide Masiello
am 21 Apr. 2022
unique shouldn't be a problem since I calculate the area as sum of rectangles.
It is actually necessary in this sense.
Bruno Luong
am 21 Apr. 2022
Bearbeitet: Bruno Luong
am 21 Apr. 2022
Check again. There are 2 steps I give the manual formula and value above: 0.1879
Davide Masiello
am 21 Apr. 2022
The answer was indeed wrong because unique sorts the output unless specified otherwise. I fixed it and now it works. I also implemented to calculate the absolute value so the area comes out positive.
N/A
am 25 Apr. 2022
0 Stimmen
2 Kommentare
Bruno Luong
am 25 Apr. 2022
@shahin sharafi Only you can decide
"I want to find the inegral (The area under the curve)"
integral has sign changing with x direction, area has not.
You also stated you want using trapz.
N/A
am 27 Apr. 2022
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
