-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
85 lines (69 loc) · 1.83 KB
/
main.c
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
#include <stdio.h>
#include <math.h>
#include "main.h"
#include "area_approx_config.h"
int main(void)
{
printf("Version %d.%d\n",
AREA_APPROX_MAJOR_VERSION,
AREA_APPROX_MINOR_VERSION);
double(*funcPtr)(double) = func;
double a = 0,
b = 10,
lram = LRAM(funcPtr, a, b, 10),
rram = RRAM(funcPtr, a, b, 10),
sim = simpson(funcPtr, a, b),
simSum = simpsonStrips(funcPtr, a, b, 10);
printf("LRAM = %lf\nRRAM = %lf\nsim = %lf\nsimSum = %lf",
lram, rram, sim, simSum);
getchar();
return 0;
}
//define a math function here
double func(double x)
{
return pow(x, 2.0);
}
//uses left rectangles to estimate the area under a curve from [a,b]
//n is the number of rectangles to use
double LRAM(double(*funcPtr)(double), double a, double b, int n)
{
double width = (b - a) / n,
height = 0;
for (int i = 0; i < n; i++)
{
height += (*funcPtr)(a + i * width);
}
return width*height;
}
//uses right rectangles to estimate the area under a curve from [a,b]
//n is the number of rectangles to use
double RRAM(double(*funcPtr)(double), double a, double b, int n)
{
double width = (b - a) / n,
height = 0;
for (int i = 1; i <= n; i++)
{
height += (*funcPtr)(a + i * width);
}
return width*height;
}
//uses simpson rule to estimate the area under the curve from [a,b]
double simpson(double(*funcPtr)(double), double a, double b)
{
double float1 = (*funcPtr)(a)+4 * (*funcPtr)((a + b) / 2) + (*funcPtr)(b);
double float2 = (b - a) / 6;
return float1 * float2;
}
//uses many simpson rules to estimate the area under the curve from [a,b]
//strips is the number of simpson rules to use
double simpsonStrips(double(*funcPtr)(double), double a, double b, int strips)
{
double simpSum = 0,
width = (b - a) / strips;
for (int i = 0; i < strips; i++)
{
simpSum += simpson(funcPtr, a + i * width, a + (i + 1) * width);
}
return simpSum;
}