MATH 166 SCW, 7.7 - Example 1: Using Mathematica to Compute Riemann Sums
MATH 166 SCW, 7.7 - Example 1: Using Mathematica to Compute Riemann Sums
Here are some commands you can define in Mathematica to estimate the definite integral f(x)x:
b
∫
a
In[]:=
LeftSum[f_,a_,b_,n_]:=f[a+(i-1)*(b-a)/n]*(b-a)/n
n
∑
i=1
In[]:=
RightSum[f_,a_,b_,n_]:=f[a+i*(b-a)/n]*(b-a)/n
n
∑
i=1
In[]:=
MidpointSum[f_,a_,b_,n_]:=f[a+(2*i-1)*(b-a)/(2*n)]*(b-a)/n
n
∑
i=1
In[]:=
Trapezoid[f_,a_,b_,n_]:=(1/2)(f[a+(i-1)*(b-a)/n]+f[a+i*(b-a)/n])*(b-a)/n
n
∑
i=1
HerearesomeapproximationsofthedefiniteintegralSin[x]x:
1
∫
0
RightSum[Sin,0,1,4]
Out[]=
1
4
1
4
1
4
1
2
1
4
3
4
Sin[1]
4
N[%]
Out[]=
0.562485
RightSum[Sin,0,1,10]
Out[]=
1
10
1
10
1
10
1
5
1
10
3
10
1
10
2
5
1
10
1
2
1
10
3
5
1
10
7
10
1
10
4
5
1
10
9
10
Sin[1]
10
N[%]
Out[]=
0.501388
LeftSum[Sin,0,1,10]
Out[]=
1
10
1
10
1
10
1
5
1
10
3
10
1
10
2
5
1
10
1
2
1
10
3
5
1
10
7
10
1
10
4
5
1
10
9
10
N[%]
Out[]=
0.417241
MidpointSum[Sin,0,1,10]
Out[]=
1
10
1
20
1
10
3
20
1
10
1
4
1
10
7
20
1
10
9
20
1
10
11
20
1
10
13
20
1
10
3
4
1
10
17
20
1
10
19
20
N[%]
Out[]=
0.459889
Trapezoid[Sin,0,1,10]
Out[]=
1
20
1
10
1
20
1
10
1
5
1
20
1
5
3
10
1
20
3
10
2
5
1
20
2
5
1
2
1
20
1
2
3
5
1
20
3
5
7
10
1
20
7
10
4
5
1
20
4
5
9
10
1
20
9
10
N[%]
Out[]=
0.459315
Toestimateanintegralsuchasx^2x,definethefunctionf[x_]:=x^2andusetheabovecommandsthisway:
2
∫
-1
In[]:=
f[x_]:=x^2
N[LeftSum[f,-1,2,100]]
Out[]=
2.95545
Here is the actual answer:
2
∫
-1
Out[]=
3