-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.Func_Integration_Trapezoid_Method.c
More file actions
55 lines (38 loc) · 1.26 KB
/
17.Func_Integration_Trapezoid_Method.c
File metadata and controls
55 lines (38 loc) · 1.26 KB
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
/**
* This Code Takes in the number of divisions in which the domain is to be divided into and gives the area under the function
* by applying Trapezoid method in each of these divisions in the domain
**/
//Header files and initial declarations
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double step;
double integral=0.0;
double a=0.0; //Lower Limit of Integration
double b=4.0; //Upper Limit of Integration
//INTEGRATED ANSWER : 0.5947328188387356
//NO NEED TO DOWNSAMPLE. SINCE WE ARE WORKING WITH FUNCTIONS
double func(double x) //The Function whose area under curve we're finding
{
double value = exp(-x) * pow(cos(x),2);
return value;
}
double Trapezoid_Integrate(int sub_step_t)
{
integral = 0.0;
step = (b-a)/sub_step_t;
for(double i=a ; i< b ; i=i+step) //or (int i=0;i<sub_step_t;i++)
{
integral = integral + (((func(i)+func(i+step))/2)*(step));
}
printf("%le %le\n",step,integral);
return 0;
}
int main(int argc,char *argv[]) //taking filepath arguement NOTE:Code works even for oversized Data file
{
int divisions = atoi(argv[1]); //stores the number of divisions the domain is to be divided into to calculate integration
Trapezoid_Integrate(divisions);
return 0;
}