Yurttas/PL/OOL/Cplusplus/F/07/06/02/00/01/integrate.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/*
 2   Copyright(C) 2003
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9
10   date   : September 1, 2003.
11   author : Salih Yurttas.
12
13   purpose : integration of f(x) = x*x;
14
15   integrate.cpp
16*/
17
18
19double fx(double x) {
20  return x * x;
21}
22
23double integrate(const double a,
24                 const double b) {
25
26// evaluates approximately the integral of F(x)
27// between limits a and b,
28// using the trapezoidal rule with 8 intervals
29
30  int n = 8;
31
32  double w,
33         sum;
34
35  w = (b-a) / (double) n;
36
37  sum = (fx(a) + fx(b)) / 2.0;
38
39  for(int i=1; i<n; i++)
40    sum += fx((a + (double) i * w));
41
42  return w*sum;
43
44}