student::trapezoid
--
numerical approximation to an integral using the Trapezoidal rulestudent::trapezoid
(f, x=a..b, n)
computes
a numerical approximation to the integral int(f(x),x=a..b)
using the Trapezoidal rule.
student::trapezoid(f, x=a..b <, n>)
f |
- | arithmetical expression or a function in
x |
x |
- | identifier |
a, b |
- | arithmetical expressions |
n |
- | a positive integer (number of trapezoids to use) |
an arithmetical expression.
freeze
, int
, numeric::int
, numeric::quadrature
, student::plotTrapezoid
,
student::riemann
,
student::simpson
student::trapezoid
(f, x=a..b, n)
computes
a numerical approximation to the integral int(f(x),x=a..b)
using the Trapezoidal rule.n
is the number of trapezoids to use. The default
value is 4.student::trapezoid
is an arithmetical
expression which consists of frozen subexpressions of type
"sum"
.
Use unfreeze
to
force the evaluation of the result.
The numerical approximation to the integral int(cos(x),x=0..PI/2) = 1 using the Trapezoidal rule and 10 trapezoids is:
>> student::trapezoid(cos(x), x = 0..PI/2, 10)
/ / / PI i1 \ \ \ PI | 2 sum| cos| ----- |, i1 = 1..9 | + 1 | \ \ \ 20 / / / ------------------------------------------- 40
We got an unevaluated expression, the formula for the
corresponding approximation. Use unfreeze
to force the evaluation of
the result:
>> unfreeze(%)
/ / | | / PI \ / 3 PI \ / 7 PI \ | PI | 2 cos| -- | + 2 cos| ---- | + 2 cos| ---- | + \ \ \ 20 / \ 20 / \ 20 / 1/2 1/2 1/2 / 9 PI \ 1/2 1/2 2 (5 + 5) 2 cos| ---- | + 2 + 5 + ------------------ + \ 20 / 2 1/2 1/2 1/2 \ \ 2 (5 - 5 ) | | ------------------ + 1 | | / 40 2 / /
Let us compute a floating-point approximation of the result:
>> float(%)
0.9979429864
The general formula of the Trapezoidal rule (using 4 trapezoids):
>> F:= student::trapezoid(f(x), x = a..b)
/ b a \ / / / / b a \ \ | - - - | | f(a) + f(b) + 2 sum| f| a + i2 | - - - | |, \ 8 8 / \ \ \ \ 4 4 / / \ \ i2 = 1..3 | | / /
To expand the frozen sum, enter:
>> F:= unfreeze(F)
/ b a \ / / a b \ / a 3 b \ | - - - | | f(a) + f(b) + 2 f| - + - | + 2 f| - + --- | + \ 8 8 / \ \ 2 2 / \ 4 4 / / 3 a b \ \ 2 f| --- + - | | \ 4 4 / /