-
Notifications
You must be signed in to change notification settings - Fork 0
/
main1.cpp
103 lines (84 loc) · 2.16 KB
/
main1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cassert>
#include <math.h>
#define DEBUG
using namespace std;
// test function declaration
float y1(float x);
float y2(float x);
/*
* trapz() - This function computes an approximate definite integral using the
* Trapezoidal Rule with uniform grid spacing.
*
* Inputs:
* - float (*fn)(float): pointer to some function f(x)
* - const float a: integral lower bound
* - const float b: integral upper bound
* - const int N: number of computational grids between a and b
*
* Output:
* - float: approximate integral result
*
* Adopted from: https://www.geeksforgeeks.org/trapezoidal-rule-for-approximate-value-of-definite-integral
*/
float trapz(float (*fn)(float), const float a, const float b, const int N) {
// validate input
assert (b > a);
assert (N > 1);
// grid spacing
float h = (b-a)/N;
#ifdef DEBUG
cout << "Grid size: " << h << endl;
#endif //DEBUG
// compute value and sum first and last values in series
float sum = fn(a) + fn(b);
#ifdef DEBUG
cout << "f(" << a << ") + f(" << b << ") = " << sum << endl;
#endif //DEBUG
// add to cumulative sum using trapezoidal rule
for (int i=1; i < N; i++) {
sum += 2*fn(a + i*h);
#ifdef DEBUG
cout << "cumsum{f(" << static_cast<float>(i)*h << ")} = " << sum << endl;
#endif
}
return sum * h/2;
}
//
// define a set of test functions
float y1(float x){
// y(x) = 1/(1+x^2)
return 1.0/(1.0+x*x);
}
float y2(float x){
// y(x) = |x|
return fabs(x);
}
//
// main function
int main(int argc, char* argv[]) {
// define the default grid size
int N = 10;
// initialize integral range and number of uniform grid points
float a = 0;
float b = 1;
// override default arguments
switch (argc){
case 4:
N = atoi(argv[3]);
case 3:
b = atof(argv[2]);
a = atof(argv[1]);
case 1:
break;
default:
cout << "Invalid number of paramaters entered!" << endl;
return -1;
}
// compute the integral and send to std output
float y_int_a_b1 = trapz(y1,a,b,N);
cout << "Value of definite integral (y1) is " << y_int_a_b1 << endl << endl;
float y_int_a_b2 = trapz(y2,a,b,N);
cout << "Value of definite integral (y2) is " << y_int_a_b2 << endl;
return 0;
}