left riemann sum integration

조회 수: 16 (최근 30일)
Harjot Purewal
Harjot Purewal 2016년 12월 4일
댓글: Sam Zavala 2021년 4월 5일
so I have to make a numerical integrator using a few methods, just made a left Riemann sum calculator but I seem to be missing something
my code seems to be more accurate when I multiply my y array by dx in the for loop, but should I be multiplying dx at each iteration or at the very end?
also is it just my code or does Riemann sum require a lot of iteration to be accurate?
% code
clear;
clc;
x1 = 1; x2 = 3;
f=@(x) 2*x.^5 - 3*x.^2 - 5;
c=1;
n=500;
dx=(x2-x1)/n;
for i=0:dx:n-1
y(c)=dx*f(1+i*dx);
c=c+1;
s=sum(y)*dx
z=integral(f,x1,x2)
end

채택된 답변

Torsten
Torsten 2016년 12월 5일
편집: Torsten 2016년 12월 6일
Try
x1 = 1;
x2 = 3;
f=@(x) 2*x.^5 - 3*x.^2 - 5;
n=500;
dx=(x2-x1)/n;
summe=0.0;
for i=1:500
summe=summe+f(x1+dx*(i-1));
end
summe=summe*dx;
z=integral(f,x1,x2);
Best wishes
Torsten.
  댓글 수: 2
David Araujo
David Araujo 2020년 10월 20일
How can i plot this?
Sam Zavala
Sam Zavala 2021년 4월 5일
I hope this is somewhat of a helpful answer but what we have been doing is build an array using a for loop and our time as telling us how many times to repeat it. Then you simply plot the array :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by