-
Notifications
You must be signed in to change notification settings - Fork 0
/
taylor.c
65 lines (52 loc) · 1.51 KB
/
taylor.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
#include <stdio.h>
#include <math.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
}
// Compute the factorial of n
double factorial(int n) {
double result = 1.0;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Taylor series method for numerical integration
double taylor(double (*func)(double, double), double x0, double y0, double h, int steps) {
double y = y0;
double x = x0;
for (int i = 0; i < steps; i++) {
double slope = func(x, y);
double increment = h * slope;
// Taylor series expansion for y
for (int j = 2; j <= 4; j++) {
increment += (pow(h, j) / factorial(j)) * (func(x, y) * pow(h, j - 1));
}
y += increment;
x += h;
}
return y;
}
int main() {
// Input initial conditions
double x0, y0;
printf("Enter the initial value of x: ");
scanf("%lf", &x0);
printf("Enter the initial value of y: ");
scanf("%lf", &y0);
// Input step size
double h;
printf("Enter the step size (h): ");
scanf("%lf", &h);
// Input number of steps
int steps;
printf("Enter the number of steps: ");
scanf("%d", &steps);
// Perform integration
double result = taylor(f, x0, y0, h, steps);
printf("Result of integration: %.6f\n", result);
return 0;
}