Calculating a sum (i.e. sigma notation)
Mathematica
Use the Sum[]
function. The first argument is the expression, and it is followed by a vector which contains the iteration variable (the one that changes over the sum) and the lower and upper index.
In[1]:= Sum[ i^2, {i,1,5} ]
Out[1]= 55
In[2]:= Sum[ i^2, {i,1,n} ]
n (1 + n) (1 + 2 n)
Out[2]= -------------------
6
Maxima
Use the sum()
function. The first argument is the expression, and it is followed by the iteration variable (the one that changes over the sum) and the lower and upper index.
(C1) sum(i^2, i, 1, 5);
(D1) 55
(C2) sum(i^2,i,1,n);
n
====
\ 2
(D2) > i
/
====
i = 1
The variable simpsum
controls whether Maxima will perform simplifications to sums. It is false by default. Setting it to true will make Maxima returned a closed form for the sum above.
(C3) simpsum : true;
(D3) TRUE
(C4) sum(i^2,i,1,n);
3 2
2 n + 3 n + n
(D4) ---------------
6
Axiom
The sum() function calculates sums in Axiom.
(1) -> sum( i^2, i=1..5 )
(1) ->
(1) 55
Type: Fraction Polynomial Integer
(2) -> sum( i^2, i=1..n )
(2) ->
3 2
2n + 3n + n
(2) -------------
6
Type: Fraction Polynomial Integer
Octave
As long as you take care to use the componentwise arithematic operators when constructing functions, it is easy to get quick sum.
octave:1> i = [1:5];
octave:2> sum( i.^2 )
ans = 55
Calculating the left-hand or right-hand Riemann sum of a function
Mathematica
This is straightforward using the Sum[]
function. For example, to calculate the Riemann sum for x^2
on the interval [0,1] using 10 rectangles,
Define a function f to find the area under.
In[1]:= f[x_] := x^2;
Set a to the left end-point of the interval and b to the right.
In[2]:= a := 0
In[3]:= b := 1
Here, n is the number of rectangles to use, and dx is the width of each rectangle.
In[4]:= n := 10
In[5]:= dx := (b-a)/10
Putting it all together gives the Riemann sum as a fraction, and then as a decimal.
In[6]:= Sum[ f[a + i*dx]*dx, {i,0,n-1} ]
57
Out[6]= ---
200
In[7]:= N[%]
Out[7]= 0.285
To calculate the right-hand Riemann sum, use instead
In[8]:= Sum[ f[a + i*dx]*dx, {i,1,n} ]
77
Out[8]= ---
200
In[9]:= N[%]
Out[9]= 0.385
Maxima
This is straightforward using the sum()
function. For example, to calculate the Riemann sum for x^2
on the interval [0,1] using 10 rectangles,
First set sum simplifying on.
(%i1) simpsum : true;
(%o1) TRUE
Define a function f to find the area under.
(%i2) f(x) := x^2;
2
(%o2) f(x) := x
Set a to the left end-point of the interval and b to the right.
(%i3) a : 0;
(%o3) 0
(%i4) b : 1;
(%o4) 1
Here, n is the number of rectangles to use, and dx is the width of each rectangle.
(%i5) n : 10;
(%o5) 10
(%i6) dx : (b-a)/n;
1
(%o6) --
10
Putting it all together gives the Riemann sum as a fraction, and then expanded as a decimal:
(%i7) sum( f(a + i*dx)*dx, i, 0, n-1 );
57
(%o7) ---
200
(%i8) %,numer;
(%o8) 0.285
We see that using 10 rectangles gives, in scientific notation, an (under-)estimate of 0.285 for the area under the curve. To calculate the right-hand Riemann sum, use instead of (%i7):
(%i9) sum( f(a + i*dx)*dx, i, 1, n );
77
(%o9) ---
200
(%i10) %,numer;
(%o10) 0.385
This gives an over-estimate of 0.385 for the area under the curve.
Axiom
This is straightforward using the sum()
function. For example, to calculate the Riemann sum for x^2
on the interval [0,1] using 10 rectangles,
Define a function f to find the area under.
(1) ->
(1) -> f(x) == x^2
Type: Void
Set a to the left end-point of the interval and b to the right.
(2) -> a := 0
(2) ->
(2) 0
Type: NonNegativeInteger
(3) -> b := 1
(3) ->
(3) 1
Type: PositiveInteger
Here, n is the number of rectangles to use, and dx is the width of each rectangle.
(4) -> n := 10
(4) ->
(4) 10
Type: PositiveInteger
(5) -> dx := (b-a)/n
(5) ->
1
(5) --
10
Type: Fraction Integer
Putting it all together gives the Riemann sum as a fraction, and then expanded as a decimal:
(6) -> sum( f(a + i*dx)*dx, i=0..(n-1) )
Compiling function f with type Polynomial Fraction Integer ->
Polynomial Fraction Integer
57
(6) ---
200
Type: Fraction Polynomial Fraction Integer
(7) -> % :: Float
(7) ->
(7) 0.285
Type: Float
We see that using 10 rectangles gives, in scientific notation, an (under-)estimate of 0.285 for the area under the curve. To calculate the right-hand Riemann sum, use instead of (6),
(8) -> sum( f(a + i*dx)*dx, i=1..n )
(8) ->
77
(8) ---
200
Type: Fraction Polynomial Fraction Integer
(9) -> % :: Float
(9) ->
(9) 0.385
Type: Float
This gives an over-estimate of 0.385 for the area under the curve.
Octave
This is straight-forward using the sum technique from above. For example, to calculate the left sum
of y = x^2
on the interval [0,1] using 10 rectangles:
Define a function f to find the area under.
octave:1> function y = f(x); y = x.^2; endfunction
Set a to the left end-point of the interval and b to the right.
octave:2> a = 0;
octave:3> b = 1;
Here, n is the number of rectangles to use, and dx is the width of each rectangle.
octave:4> n = 10;
octave:5> dx = (b-a)/n;
Putting it all together gives the Riemann sum
octave:6> i = [ 0:n-1 ];
octave:7> sum( f(a + i*dx)*dx )
ans = 0.28500
The right sum is also easy to obtain. Redefine the index variable and calculate the sum again:
octave:8> i = [ 1:n ];
octave:9> sum( f(a + i*dx)*dx )
ans = 0.38500
Calculating the limit of a Riemann sum
Mathematica
To calculate the limit of a Riemann sum, we use the same techniques as above for calculating a Riemann sum except we leave the number of intervals (n) unspecified. The Limit[]
function then allows us to calculate the limit of the sum as n goes to Infinity.
Putting it all together, we are able to determine the exact area under the function x^2
over the interval [0,1].
In[1]:= f[x_] := x^2;
In[2]:= a := 0
In[3]:= b := 1
In[4]:= dx := (b-a)/n
In[5]:= s := Sum[ f[a + i*dx]*dx, {i,0,n-1} ]
In[6]:= Limit[s, n->Infinity ]
1
Out[6]= -
3
Maxima
To calculate the limit of a Riemann sum, we use the same techniques as above for calculating a Riemann sum except we leave the number of intervals (n) unspecified. We also forego defining a function f and instead type it directly into the sum ourselves (this helps Maxima be able to simplify the sum correctly). The limit()
function then allows us to calculate the limit of the sum as n goes to infinity.
Putting it all together, we are able to determine the exact area under the function x^2
over the interval [0,1].
(C1) simpsum : true;
(D1) TRUE
(C2) a : 0;
(D2) 0
(C3) b : 1;
(D3) 1
(C4) dx : (b-a)/n;
1
(D4) -
n
(C5) s : sum( (a + i*dx)^2*dx, i, 0, n-1 );
3 2
n + 2 (n - 1) + 3 (n - 1) - 1
(D5) -------------------------------
3
6 n
(C6) limit(s,n,inf);
1
(D6) -
3
Axiom
To calculate the limit of a Riemann sum, we use the same techniques as above for calculating a Riemann sum except we leave the number of intervals (n) unspecified. The limit()
function then allows us to calculate the limit of the sum as n goes to %plusInfinity.
Putting it all together, we are able to determine the exact area under the function x^2
over the interval [0,1].
(1) ->
(1) -> f(x) == x^2
Type: Void
(2) -> a := 0
(2) ->
(2) 0
Type: NonNegativeInteger
(3) -> b := 1
(3) ->
(3) 1
Type: PositiveInteger
(4) -> dx := (b-a)/n
(4) ->
1
(4) -
n
Type: Fraction Polynomial Integer
(5) -> s := sum( f(a + i*dx)*dx, i=0..(n-1) )
Compiling function f with type Fraction Polynomial Integer ->
Fraction Polynomial Integer
2
2n - 3n + 1
(5) ------------
2
6n
Type: Union(Fraction Polynomial Integer,...)
(6) -> limit( s, n=%plusInfinity )
(6) ->
1
(6) -
3
Type: Union(OrderedCompletion Fraction Polynomial Integer,...)