This code computes Riemann sums for any number of rectangles we wish. First, define the function you want to integrate. Be sure to hit enter after you type information into each cell.
In[]:=
f[x_]:=x^2
Now enter your lower and upper limits of integration. Don’t delete those semicolons!
In[]:=
lower=1;upper=5;
How many rectangles do you want to use?
In[]:=
rectangles=4;
If you want a decimal answer, make the following variable 1.0. If you want an exact answer, leave it a 1.
In[]:=
decimal=1.0;
The code below computes the sum using left endpoints!
In[]:=
width=(upper-lower)/rectangles;total=Sum[decimal*width*f[lower+(i-1)*width],{i,1,rectangles}];Print[total]
30.
The code below computes the sum using right endpoints!
In[]:=
width=(upper-lower)/rectangles;total=Sum[decimal*width*f[lower+(i)*width],{i,1,rectangles}];Print[total]
54.
The code below computes the sum using midpoints!
In[]:=
width=(upper-lower)/rectangles;total=Sum[decimal*width*f[lower+(i-1)*width+width/2],{i,1,rectangles}];Print[total]
41.
The command below computes the exact value using the Fundamental Theorem of Calculus.
In[]:=
Integrate[decimalf[x],{x,lower,upper}]
Out[]=
41.3333