I'm new to Matlab...never used it before and I need help with calculating reimann sums

Hi,
I'm given x and y values and are asked to make a plot and calculate the right reimann sum and the left reimann sum. I managed to find a command that allows me to create a plot but I have no idea how to calculate the left and right reimann sums.
Any help would be greatly appreciated.

Answers (2)

It depends on whether you use the left (lower) values or the right (higher) values of the intervals to calculate the Riemann sum. There is no one command that will do this for you. You have to write your own code. Probably the easiest way to go about this is to first define the intervals, then calculate the function values for each, depending on whether you choose the right or left intervals, then sum them. See the documentation on Matrices and Arrays, and for loops and the sum function to start with. There are many ways to do what you want. Experiment!
Assuming x and y are vectors of the same length with corresponding x- and y-values, do this:
Ls = sum(y(1:end-1).*diff(x)); % Left Riemann sum (<--- Corrected)
Rs = sum(y(2:end).*diff(x)); % Right Riemann sum
Either of these would be an approximation of the integral of y with respect to x. Their average would be the same value given by matlab's 'trapz' function.
(Note: I originally had division instead of multiplication in the above sums. It has been corrected.)

Asked:

on 2 Feb 2015

Edited:

on 2 Feb 2015

Community Treasure Hunt

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

Start Hunting!