-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrapezoidal Rule.c
More file actions
28 lines (28 loc) · 897 Bytes
/
Copy pathTrapezoidal Rule.c
File metadata and controls
28 lines (28 loc) · 897 Bytes
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
#include <stdio.h>
#include <math.h>
// Function to integrate
double function(double x)
{
return (4 * exp(x) / (1 + pow(x, 3))); // Replace with your desired function
}
// Trapezoidal rule for numerical integration
double trapezoidalRule(double a, double b, int n)
{
double h = (b - a) / n; // Step size
double sum = (function(a) + function(b)) / 2.0; // finding sum of 1st term and last term
for (int i = 1; i < n; i++)
{
double x = a + i * h;
sum += function(x); // finding sum of intermediate terms.
}
return h * sum;
}
int main()
{
double a = 0.0; // Lower limit of integration
double b = 2.0; // Upper limit of integration
int n = 100; // Number of subintervals
double result = trapezoidalRule(a, b, n);
printf("Numerical integration result: %lf\n", result);
return 0;
}