Trapezoidal rule with unevenly spaced data points

I am having a lot of trouble using the trapezoidal method on matlab to determine the area under the curve with no function, just unevenly spaced data points:
(0,0), (1,5), (5,8), (9,6), (3,10), (0,11)
I have absolutely no idea where to start.

6 Comments

What have you discussed in class? What do you know about the area of a trapezoid? Maybe you could start by plotting the points that you have, divide the plot up into individual trapezoids, and then ask yourself how you would sum up the area of all those individual trapezoids. Once you have the algorithm figured out on paper, start writing code. You might look at the formulas and pictures in these links:
You are looking for the area of the green section below ?
x = [0, 1, 5, 9, 3, 0]
x = 1×6
0 1 5 9 3 0
y = [0, 5, 8, 6, 10, 11]
y = 1×6
0 5 8 6 10 11
fill(x, y, 'g')
sorry data points were wrong, they are actually:
x = [0, 1, 5, 9, 10, 11];
y = [0, 6, 8, 6, 3, 0];
fill(x, y, 'g')
But yes, that's exactly what I'm trying to do. I know how to do it by hand, but unsure how to put into matlab.
x = [0, 1, 5, 9, 10, 11];
y = [0, 6, 8, 6, 3, 0];
fill(x, y, 'g')
area = trapz(x, y)
area = 65
@Georgia Platt If you know how to do it by hand, why can't you code that method into MATLAB? Especially once you look at the formulas in the links above. For two points (x1,y1) and (x2,y2) with the ordering x1<x2, the area formed by the trapezoid is simply (x2-x1)*(y1+y2)/2, so why can't you write a loop to sum these up?
You can decompose into shapes. There is an initial triangle, and you can get the area for that as 1/2 base times height. There are then a series of trapazoids, and you can calculate their areas. Then there is a final triangle and 1/2 base times height for it. Add the areas all together.
... which is what trapz() will do for you.

Sign in to comment.

Products

Release

R2022b

Asked:

on 22 Sep 2022

Commented:

on 22 Sep 2022

Community Treasure Hunt

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

Start Hunting!