forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler.c
35 lines (29 loc) · 859 Bytes
/
euler.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
#include <stdio.h>
#include <math.h>
void solve_euler(double timestep, double *result, size_t n) {
if (n != 0) {
result[0] = 1;
for (size_t i = 1; i < n; ++i) {
result[i] = result[i-1] - 3.0 * result[i-1] * timestep;
}
}
}
int check_result(double *result, size_t n, double threshold, double timestep) {
int is_approx = 1;
for (size_t i = 0; i < n; ++i) {
double solution = exp(-3.0 * i * timestep);
if (fabs(result[i] - solution) > threshold) {
printf("%f %f\n", result[i], solution);
is_approx = 0;
}
}
return is_approx;
}
int main() {
double result[100];
double threshold = 0.01;
double timestep = 0.01;
solve_euler(timestep, result, 100);
printf("%d\n", check_result(result, 100, threshold, timestep));
return 0;
}