Hello I need help with my left Riemann Sum loop
31 views (last 30 days)
Show older comments
I'm trying to find the left riemann sum of 3x^2 +4 on [0,5] where I have n = 10 (rectangles). I would appreciate it deeply if someone could take a look at my program and tell me why I keep getting the wrong answer. It should be 126.875, but I keep getting 52. I know that left Riemann sum is a+(k-1)Δx, for k=1,....n.
a = 0;
b = 5;
n = 5;
dx = (b-a)/n;
for k = 1:n-1
Lsum = a +dx*(3*(k)^2+4);
end
display(Lsum)
0 Comments
Answers (2)
Geoff Hayes
on 6 Aug 2016
Jesutofunmi - look closely at your code
for k = 1:n-1
Lsum = a +dx*(3*(k)^2+4);
end
display(Lsum)
On each iteration of the for loop, you are ignoring what came previously so you end up with Lsum being assigned the a value from the last iteration of your for loop. You need to sum this value with what came previously as
Lsum = 0;
for k = 1:n-1
Lsum = Lsum + a +dx*(3*(k)^2+4);
end
display(Lsum)
This still won't give you the correct answer though. What is the function (curve) that you are calculating the area for? Define it as an anonymous function like
f = @(x)3*x^2 + 4;
The above is an example based upon what you have shown but it may not be the function that you are interested in. Then your sum would become
Lsum = Lsum + dx*f(k);
where k is an input. So what should that input be? Since this is homework, we can only give out hints. Review your class notes and try to figure out what it should be. See http://mathinsight.org/calculating_area_under_curve_riemann_sums for guidance as well.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!