## `student::simpson` -- numerical approximation to an integral using Simpson's rule

#### Introduction

`student::simpson``(f, x=a..b, n)` computes a numerical approximation to the integral int(f(x),x=a..b) using Simpson's rule.

#### Call(s)

`student::simpson(f, x=a..b <, n>)`

#### Parameters

`f` | - | arithmetical expression or a function in `x`
`x` | - | identifier
`a, b` | - | arithmetical expressions
`n` | - | a positive integer (number of stripes to use)

#### Returns

an arithmetical expression.

#### Related Functions

`freeze`, `int`, `numeric::int`, `numeric::quadrature`, `student::plotSimpson`, `student::riemann`, `student::trapezoid`

#### Details

- `student::simpson``(f, x=a..b, n)` computes a numerical approximation to the integral int(f(x),x=a..b) using Simpson's rule.
- `n` is the number of stripes to use. The default value is 4.
- The result of `student::simpson` is an arithmetical expression which consists of frozen subexpressions of type `"sum"`.

  Use `unfreeze` to force the evaluation of the result.

#### Example 1

The numerical approximation to the integral int(sin(x), x=0..1) using Simpson's rule and 10 stripes is:

    >> student::simpson(sin(x), x = 0..1, 10)

                      /    / i1 \            \
                   sum| sin| -- |, i1 = 1..4 |
          sin(1)      \    \ 5  /            /
          ------ + --------------------------- +
            30                 15

                  /    / i1        \            \
             2 sum| sin| -- - 1/10 |, i1 = 1..5 |
                  \    \ 5         /            /
             ------------------------------------
                              15

We got an unevaluated expression, the formula for the corresponding approximation. Use `unfreeze` to force the evaluation of the result:

    >> unfreeze(%)

          sin(1)   2 sin(1/2)   sin(1/5)   sin(2/5)   sin(3/5)
          ------ + ---------- + -------- + -------- + -------- +
            30         15          15         15         15

             sin(4/5)   2 sin(1/10)   2 sin(3/10)   2 sin(7/10)
             -------- + ----------- + ----------- + ----------- +
                15          15            15            15

             2 sin(9/10)
             -----------
                 15

Let us compute a floating-point approximation of the result:

    >> float(%)

                                   0.4596979498

and compare it with the exact value of the definite integral int(sin(x),x=0..1):

    >> F:= int(sin(x), x = 0..1); float(F)

                                    1 - cos(1)

                                   0.4596976941

#### Example 2

The general formula of Simpson's rule (using 4 stripes):

    >> F:= student::simpson(f(x), x = a..b)

          / b    a  \ /                  / a   b \
          | -- - -- | | f(a) + f(b) + 2 f| - + - | +
          \ 12   12 / \                  \ 2   2 /

                  /  /                / b   a \ \            \ \
             4 sum| f| a + (2 i3 - 1) | - - - | |, i3 = 1..2 | |
                  \  \                \ 4   4 / /            / /

To expand the frozen sum, enter:

    >> F:= unfreeze(F)

          / b    a  \ /                  / a   b \      / a   3 b \
          | -- - -- | | f(a) + f(b) + 2 f| - + - | + 4 f| - + --- | +
          \ 12   12 / \                  \ 2   2 /      \ 4    4  /

                / 3 a   b \ \
             4 f| --- + - | |
                \  4    4 / /

You may even expand this product:

    >> expand( F )

                                                 / a   b \
                                              a f| - + - |
          b f(a)   a f(b)   a f(a)   b f(b)      \ 2   2 /
          ------ - ------ - ------ + ------ - ------------ +
            12       12       12       12          6

                / a   b \      / a   3 b \      / 3 a   b \
             b f| - + - |   a f| - + --- |   a f| --- + - |
                \ 2   2 /      \ 4    4  /      \  4    4 /
             ------------ - -------------- - -------------- +
                  6               3                3

                / a   3 b \      / 3 a   b \
             b f| - + --- |   b f| --- + - |
                \ 4    4  /      \  4    4 /
             -------------- + --------------
                   3                3

#### Changes

- new function

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000