Left Riemman Sum: How do I approach this problem?

21 ビュー (過去 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 Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by