Docsity

Notes on Numerical Integration-Simplest Approximation | CSCD 543, Study notes of Computer Science

Toggle sidebar
bg1
CScD-543/443 Multiprocessor Example:
Numerical Integration — Simplest Approximation
Given a function in terms of (x,y) data points, one can approximate the integral of that function
over the interval represented by the x values several ways. As our specimen problem, we will
use the simplest approximation: integration by the trapezoid rule. In this approach, the interval
is considered to be constructed by a series of trapezoids, in which the base of the trapezoid is the
x-axis, the two sides being the line segments from the x axis up to the y values on either side of
the trapezoid, and the top line being a straight line connecting those two y values.
Let a represent the left y-value; b, the right y-value; and h, the x interval (xj – xj–1).
The total area can be computed as two parts: a rectangle and a triangle.
The rectangle: a · h
Note that this uses up part of b, so that the unused segment is in the triangle.
The triangle: ½ ( b – a ) · h
Total area:
hbahabha )(
2
1
)(
2
1
2
2
If we have evenly-spaced data points y0 through yn (n+1 data points for n
trapezoids), every point other than y0 and yn (the boundary points) appears twice. If we retain h
as the x-interval (evenly spaced, as noted), the formula adding up all the trapezoids simplifies to
this:
hyyy
n
n
j
j
2
1
2
1
1
1
0
If we do not have evenly spaced observations, then we need to explicitly handle the varying
increments:
n
j
jjjj
xxyy
1
11
2
1
This is an example of an “embarrassingly parallel” problem: forming the integral from x0 to xk is
completely separate from forming the integral from xk to xn (provided 0 < k < n, of course). Thus
we can compute in parallel the integral for several segments and then just add them together to
get the complete integral.
This is the problem that we will do in the various distributed multiprocessing environments that
we will study in this class as the initial parallel programming projects. That means that the bulk
of your time will not be spent on the integration algorithm, but instead will be spent on porting
that algorithm over to a new parallel processing environment.
Printed 2020–12 –月–02 at 10:56
a
h
b
b-a
h
pf3

Partial preview of the text

Download Notes on Numerical Integration-Simplest Approximation | CSCD 543 and more Study notes Computer Science in PDF only on Docsity!

CScD-543/443 Multiprocessor Example:

Numerical Integration — Simplest Approximation

Given a function in terms of (x,y) data points, one can approximate the integral of that function

over the interval represented by the x values several ways. As our specimen problem, we will

use the simplest approximation: integration by the trapezoid rule. In this approach, the interval

is considered to be constructed by a series of trapezoids, in which the base of the trapezoid is the

x-axis, the two sides being the line segments from the x axis up to the y values on either side of

the trapezoid, and the top line being a straight line connecting those two y values.

Let a represent the left y-value; b , the right y-value; and h , the x interval (xj – xj–1).

The total area can be computed as two parts: a rectangle and a triangle.

The rectangle: a · h

Note that this uses up part of b , so that the unused segment is in the triangle.

The triangle: ½ ( b – a ) · h

Total area: a^  h^  b  a  h  ( a  b ) h

If we have evenly-spaced data points y 0 through yn ( n +1 data points for n

trapezoids), every point other than y 0 and yn (the boundary points) appears twice. If we retain h

as the x -interval (evenly spaced, as noted), the formula adding up all the trapezoids simplifies to

this:

y y yn h

n j

j ^ 

1 1 0

If we do not have evenly spaced observations, then we need to explicitly handle the varying

increments:

^ ^ ^ ^ 

n j

yj yj xj x j

1

This is an example of an “embarrassingly parallel” problem: forming the integral from x 0 to xk is

completely separate from forming the integral from xk to xn (provided 0 < k < n , of course). Thus

we can compute in parallel the integral for several segments and then just add them together to

get the complete integral.

This is the problem that we will do in the various distributed multiprocessing environments that

we will study in this class as the initial parallel programming projects. That means that the bulk

of your time will not be spent on the integration algorithm, but instead will be spent on porting

that algorithm over to a new parallel processing environment.

Printed 2020–12 月–– 02 at 10:

a

h

b

b-a

h

Trapezoid Rule Implementations in C and Java

Trapezoid.c

  • Trapezoid-rule integration of a set of data points NOT evenly
  • spaced along the X-axis. They are assumed to be ordered along
  • that axis.
  • Input file:
  • Number of data points
  • Followed by that number of white-space delimited x y pairs
  • No data validation is done --- the file is presumed valid.
  • Author: Timothy Rolfe / #include <stdlib.h> #include <stdio.h> #include <math.h> double trapezoidRule (int size, double x, double* y) { double sum = 0.0, increment; int k; for ( k = 1; k < size; k++ ) {//Trapezoid rule: 1/2 h * (f0 + f1) increment = 0.5 * (x[k]-x[k-1]) * (y[k]+y[k-1]); sum += increment; } return sum; } int main ( int argc, char* argv[] ) { char* fileName = argc > 1? argv[1] : "InpData.txt"; FILE* inputFile = fopen (fileName, "r"); int k, size; double x, y; double integral; if ( inputFile == NULL ) { fprintf (stderr, "Open failed for %s\n", fileName); exit(666); } fscanf(inputFile, "%d", &size); printf ("Number of points: %d\n", size); x = (double) calloc (size, sizeof(x)); y = (double) calloc (size, sizeof(x)); for ( k = 0; k < size; k++ ) fscanf (inputFile, "%lg%lg", x+k, y+k); integral = trapezoidRule (size, x, y); printf ("Integral: %8.8lf\n", integral); printf ("Check: log(%2.2lf) = %8.8lf\n", x[size-1], log(x[size-1]) );

Page 2 Printed 2020 十二月– 02 at 10: