A program for approximating definite integrals, written for MATH 613 - Applied Linear Algebra course at Indiana State University. The program uses Simpson's one-third rule for the approximation:
This program uses the extended form of Simpson's rule for approximating n-dimensional integrals.
The following are some implementation details that are of note.
The program contains a custom tokenizer (scanner.c) and parser
(parser.c) that converts an infix input equation to an abstract
syntax tree (ast.c). Representing the equation as an AST allows
for computing the equation on many values efficiently. It is also a
simple matter to extend the scanner and parser to handle new
operators and functions.
The program uses an adaptive approach for choosing the mesh
size. Starting at a low value, the mesh size is doubled until the
approximation converges to within four decimal places (simpson.c). The
mesh size is part of the output of the program.
The program was built from the ground up to accomadate
n-dimensional integrals. The program accepts as input the integral
dimension and then a list of integral specifications that consist
of the start and end of the integral, as well as the variable the
integral should be performed on (args.c, simpson.c). See the
Usage section for more details.
The program accepts as input an infix equation that can contain numbers, functions, one-character variables, and operators.
+(addition)-(subtraction)*(multiplication)/(division)^(exponentiation)((open parenthesis))(closing paranthesis)
sin(sine)cos(cosine)tan(tangent)ln(natrual logarithm)sqrt(square root)
To build the project, run the following command in the root directory of the project:
make integrate
This command produces the target executable integrate.
- -n : Specifies the integral dimension as .
- -p : Specifies the list of integral
specifications. A colon (
:) separated list of comma (,) separated specifications e.g.0,3,x:0,3,yspecifies a double integral in which the variablex(the outer integral) is integrates over the range [0, 3] and variabley(the inner integral) is integrated over the range [0, 3]. - -e : Specifies the equation to integrate.
If no command line arguments are given, the program will expect the
user to first input the integral specification, then the
equation. One or the other may be passed as command line arguments,
while the other is read from stdin. For example,
./integrate
0 3.14 x
sin(x)
is the stdin way of specifying (\int_0^\pi \sin(x) dx). The
command line way would be:
./integrate -n 1 -p `0,3.14,x' -e `sin(x)'
Run the interactive program run_tests.sh to perform some test
examples using the program. The test examples are as follows: