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)

回答(2 个)

Geoff Hayes
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.
  1 个评论
Jesutofunmi Palmer
Thank you so much for the hints. My calculus professor just dumped this on the class saying we can ask for help when it came to coding since most people in the class never used matlab before, but I understand the moral intgrity that has to be exercised on this forum. I'll get back on campus to work on it.

请先登录,再进行评论。


MADISON OHARA
MADISON OHARA 2020-11-17
for k = 1:n-1
Lsum = a +dx*(3*(k)^2+4);
end
display(Lsum)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by