-
Notifications
You must be signed in to change notification settings - Fork 0
/
solvers.c
53 lines (45 loc) · 1.3 KB
/
solvers.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef double afunc(double);
double root(afunc *f, afunc *g, double a, double b, double eps, int *counter);
double integral(afunc *f, double a, double b, double eps);
//finding the root of right and left part of the equation by the method of chords
double root(afunc *f, afunc *g, double a, double b, double eps, int *counter) {
if (counter!=NULL) (*counter)++;
double c=(a*(f(b)-g(b))-b*(f(a)-g(a)))/((f(b)-g(b))-(f(a)-g(a)));
int flag1 = (f(a)-g(a)) < 0;
int flag2 = (f((a+b)/2)-g((a+b)/2)) < ((f(a)-g(a))+(f(b)-g(b)))/2;
if (flag1==flag2) {
if ((f(c)-g(c)) * (f(c+eps)-g(c+eps)) >= 0) {
return root(f,g,c,b,eps, counter);
}
else return c;
} else {
if ((f(c)-g(c)) * (f(c-eps)-g(c-eps)) >= 0) {
return root(f,g,a,c,eps, counter);
}
else return c;
}
}
//finding the integral by the method of Simpson
double integral(afunc *f, double a, double b, double eps) {
int n = 2;
double h = (b-a)/n;
double ilast = 0;
double i = 0;
do {
ilast = i;
i = 0;
n *= 2;
h = (b-a)/n;
i+=f(a);
i+=f(b);
for (int j=1; j<n; j++) {
i += f(a+j*h)*(j%2==0?2:4);
}
i*=h;
i/=3;
} while (fabs(i-ilast)/15 > eps);
return i;
}