

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A programming assignment for cs 2073, computer programming with engineering applications, in which students are required to write a program for numerical integration using three methods: trapezoid, simpson's, and monte-carlo. The assignment involves finding the integral of two specific functions, f(x) and g(x), for given intervals and numbers of intervals.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS 2073, Engineering Programming Assign. 5, Integration 03:20:21 CST, Mon Mar 26 2001
For this assignment, we want a program that will do numerical integration. You don’t really
need to know any calculus, since for us the integral of a function will just be the area under its
graph, or its average value. This assignment will use three numerical integration methods:
We will be finding the value of the integral of a function f ( x ), for x from a to b. You will also
start with an integer n representing the number of intervals to divide the segment from a to b
into. The size of each interval is h = ( b - a )/ n. Given these starting values, the trapezoid method
uses the formula
Similarly, Simpson’s method uses the formula
Finally, a Monte-Carlo method might use
) + f ( x 2
) + f ( x 3
) +... + f ( x n -
) + f ( x n
Here the numbers x 1
, x 2
,... , x n
are randomly chosen from the interval from a to b.
You should use the two specific functions
f ( x ) = 1/(1 + x
), for x from 0 to 1, and
Page 1
CS 2073, Engineering Programming Assign. 5, Integration 03:20:21 CST, Mon Mar 26 2001
g ( x ) = e
-x
, for x from -2 to 2.
Finally for each of the two functions, and for each of the three integration methods (6 cases), you
should try n = 10, and n = 1000. Thus you should have 12 answers altogether, and your answers
should be clearly labeled with the function ( f or g above), the values of a and b , the value of n ,
and the integration method.
You must use Pascal functions to calculate f and g as above.
You must use a procedure generate_f that will take as inputs the numbers a , b , and n , and
will return (as a reference parameter) an array of function values funcval, with f ( a ), f ( a + h ),
f ( a +2 h ),... , f ( a +( n -1) h ), f ( b ) stored in array locations 0 through n. Similarly for a procedure
generate_g:
const Maxval = 1000;
type funcvaltype = array[0..Maxval] of real;
procedure generate_f (var funcval: funcvaltype; a, b: real; n: integer);
procedure generate_g (var funcval: funcvaltype; a, b: real; n: integer);
Another Pascal procedure generate_fm should take a , b , and n as inputs and generate n
function values f (x 1
), f ( x 2
), f ( x 3
),... , f ( x n -
), f ( x n
) in the array funcval, and similarly for a
procedure generate_gm:
procedure generate_fm(var funcval: funcvaltype; a, b: real; n: integer);
procedure generate_gm(var funcval: funcvaltype; a, b: real; n: integer);
Then you must have three Pascal functions trap, simp and monte that use the array
funcval and the values a , b , and n , to calculate the integral according to the above formulas:
function trap (funcval: funcvaltype; a, b: real; n: integer): real;
function simp (funcval: funcvaltype; a, b: real; n: integer): real;
function monte (funcval: funcvaltype; a, b: real; n: integer): real;
The random numbers can be generated using a random number generator that will be separately
distributed and discussed in class. Let’s use type double for all real numbers, rather than
real. Print answers with 16 significant digits.
(Note: for efficiency sake above, one might want to make the funcval parameters to the three
functions above reference parameters.)
Extras:
In addition to the above, find an approximate value for the integral of g ( x ) from -∞ to +∞.
Page 2