aboutsummaryrefslogtreecommitdiff
path: root/integral.c
blob: 950962435d05f74b6f4cf44064059157d1dbcca0 (plain)
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
#include <stdio.h>
#include <math.h>

#define PI 3.14159


const float range_bottom = PI / 2;
const float range_top = 2 * PI / 3;
const float intervals = 100000;

float f(float x)
{
	return (1 / sin(x));
}

int main(void)
{
	float oversum, undersum, average, steps;
	steps = (range_top - range_bottom) / intervals;

	for(float i = range_bottom; i <= range_top; i += steps)
	{
		oversum += steps * f(i);
		undersum += steps * f(i - steps);
	}

	average = (oversum + undersum) / 2;
	
	printf("Area under f(x) between %f and %f:\n", range_bottom, range_top);
	printf("%f\n", average);
	printf("Interval: %f\n", steps); 
}