CIS071
Lab09 – Numerical Approximation of a Definite Integral DUE: noon, Mar 27, 2007
Objectives:
Implementing loops to solve a
mathematical problem.
Definite integrals:
The topic of Lab 9 is
calculating a definite integral of the form:
![]()
where f(x) is a function of a
single variable. There are two ways to calculate I – analytical and numerical. In the analytical approach, the antiderivative function F is derived for function f. Then, value of I is calculated as:
![]()
For example, for function f(x)
= x, the antiderivative function is F(x)
= x2/2. Calculus provides
many nice methods for deriving the antiderivative functions F for many types of functions f. However, there are functions f for which it is very difficult to
derive F. In such cases, numerical
approach can be used to calculate I.
The basic idea for the numerical approach comes from the
interpretation that definite integral is the area below function f, bounded by a and b (see the figure
on the left). This area can be approximated by the area covered by
rectangles (see the figure on the right) by using the following formula:
![]()
where
![]()
and n is number of rectangles.
You should observe that the
accuracy of the approximation should increase with n (i.e. should decrease with the width of the rectangles)


Task:
You should write a program
that explores how the accuracy of the numerical approach changes with the
choice of n. To do this, we will
experiment with the following simple function:
f(x) = 2×x
The antiderivative of this
function is
F(x) = x2
CIS071
Lab09 – Numerical Approximation of a Definite Integral DUE: noon, Mar 27, 2007
Implementation:
1. Main function. The main function should prompt user to enter values
of n (integer), a (double), and b
(double). Then, it should calculate value of I analytically and numerically, and display the results to user.
For example, if user entered n = 2, a = 0.0, b = 2.0, your program should display:
Analytical I is 4.0, and its numerical approximation
is 3.0
Therefore, the error of numerical approximation is
1.0
Note: remember to use
placeholder %f to display the double values.
2. Integration functions. The main function should be calling the
following two functions:
double analytical_integral(double a, double b)
double numerical_integral(double a, double b, int n)
Function analytical_integral should return value F(b) - F(a)
Function numerical_integral should return value
, where D is defined as
. Hint: this function can be written by using the for loop - quite similar in flavor to the way we
calculated sum of array elements in class.
3. Function f and its
antiderivative. Function analytical_integral should
be using function
double F(double x)
that simply returns value x2.
Function numerical_integral should be using function
double f(double x)
that simply returns value 2×x.
Testing:
Make sure your program
compiles. To test if it works correctly, you can enter n = 2, a = 0.0, b = 2.0, and check if the display is
exactly the same as in the example above (see Step 1 of the implementation).
Once you are convinced your program is correct, you should play with choice of
n. For example, you can fix a = 0.0, b = 2.0, and check what is the outcome
for n = 1, 2, 4, 10, 50, 100, 1000,
10000, … What happens with accuracy of numerical approximation? How fast is
your program?
Deliverables:
Submit your program; attach
results of test runs of the program, provide brief answers about the accuracy
of the numerical approximation as a function of n, and about the speed of your program as n increases.