-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpson38.c
113 lines (87 loc) · 2.68 KB
/
simpson38.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double f(double x) {
}
double simpson_3_8(double (*func)(double), double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
if (i % 3 == 0)
sum += 2 * f(x);
else
sum += 3 * f(x);
}
return (3 * h / 8) * sum;
}
int main() {
// Input integration range
double a, b;
printf("Enter the lower limit of integration: ");
scanf("%lf", &a);
printf("Enter the upper limit of integration: ");
scanf("%lf", &b);
// Input number of divisions (must be a multiple of 3)
int n;
printf("Enter the number of divisions (must be a multiple of 3): ");
scanf("%d", &n);
// Check if n is a multiple of 3
if (n % 3 != 0) {
printf("Error: Number of divisions must be a multiple of 3.\n");
return 1;
}
// Perform integration
double result = simpson_3_8(f, a, b, n);
printf("Result of integration: %.6f\n", result);
return 0;
}
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Define the function f(x, y)
double f(double x, double y) {
// Define your function here, for example:
// return x * y;
// or any other function of x and y
}
// Simpson's 3/8 rule for integration
double simpson_3_8(double (*func)(double, double), double x0, double xn, double y0, double yn, int n) {
double hx = (xn - x0) / n;
double hy = (yn - y0) / n;
double sum = f(x0, y0) + f(xn, yn);
for (int i = 1; i < n; i++) {
double x = x0 + i * hx;
double y = y0 + i * hy;
if (i % 3 == 0)
sum += 2 * f(x, y);
else
sum += 3 * f(x, y);
}
return (3 * hx * hy / 8) * sum;
}
int main() {
// Input integration range
double x0, xn, y0, yn;
printf("Enter the lower limit of x: ");
scanf("%lf", &x0);
printf("Enter the upper limit of x: ");
scanf("%lf", &xn);
printf("Enter the lower limit of y: ");
scanf("%lf", &y0);
printf("Enter the upper limit of y: ");
scanf("%lf", &yn);
// Input number of divisions (must be a multiple of 3)
int n;
printf("Enter the number of divisions (must be a multiple of 3): ");
scanf("%d", &n);
// Check if n is a multiple of 3
if (n % 3 != 0) {
printf("Error: Number of divisions must be a multiple of 3.\n");
return 1;
}
// Perform integration
double result = simpson_3_8(f, x0, xn, y0, yn, n);
printf("Result of integration: %.6f\n", result);
return 0;
}