Hello I need help with my left Riemann Sum loop
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Geoff Hayes
2016-8-6
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!