Left Riemman Sum: How do I approach this problem?

조회 수: 19 (최근 30일)
Kevin Carty
Kevin Carty 2020년 3월 8일
답변: David Hill 2020년 3월 8일
Hi everyone.
I am stuck on how to approach this problem. Here is the problem statement.
Create a function called “leftRiemann” that takes the inputs “xVals” and “yVals” and outputs the integral from xVals(1) to xVals(end) using the left Riemann sum approximation method. The output should be called “sumArea”. Do not use the function “trapz” to create your own function but do use it to check your answer. Hint: Use diff and sum functions. (2)
I know that I should use the diff function to find the change in X values. I guess what is throwing me off is that I am not given an equation so I don't know what to do with the y values.
I know that typically you would define delta and then define the x axis increments as x(1):delta:end(x) - delta or something like that and then you would have an equation which I will just call y and then you say like y = sin(pi*t) and then the area would be area = y* delta and then you just take the sum of all the areas.
What do I do if I already have the y values? I am very lost this is what I have written but I don't really know what I am doing.
function sumArea = leftRiemann(xVals, yVals)
delta = diff(xVals);
area = y * delta
area_sum = sum(area)

답변 (1개)

David Hill
David Hill 2020년 3월 8일
function sumArea = leftRiemann(xVals, yVals)
sumArea=sum(diff(xVals).*yVals(1:end-1));%Sum up all the area rectangles. Height is yVals (on left). Width is diff(xVals).
end

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by