-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.cpp
More file actions
82 lines (79 loc) · 1.97 KB
/
source.cpp
File metadata and controls
82 lines (79 loc) · 1.97 KB
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
//Illia Dovhaelnko
#include <cmath>
#include<iostream>
//Funkcja signum zwracająca znak liczby
int sgn(double val){
return (0. < val) - (val < 0.);
}
double SecantMethod(double(*f)(double), double a, double b, int M, double eps, double delta, const double tab[]){
double x0, x1, x2, f0, f1, f2;
x0=a;
x1=b;
int iter=0;
f0=tab[0];
f1=tab[1];
while(true){
if(fabs(f0)<eps) {
return x0;
}
if(fabs(f1)<eps) {
return x1;
}
x2 = x1 - (f1*(x1-x0))/(f1-f0);
f2 =f(x2);
if(std::fabs(f2)<eps|| fabs(x2-x1)<delta) {
return x2;
}
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
iter++;
}
return x2;
}
double findZero(double(*f)(double), double a, double b, int M, double eps, double delta){
double tab[3];
tab[0]=f(a);
tab[1]=f(b);
if(std::fabs(tab[0])<eps) return a;
if(std::fabs(tab[1])<eps) return b;
double e=b-a;
double c, fc;
if(tab[0]*tab[1]>0){
return SecantMethod(f, a, b, M, eps, delta, tab);
}else {
double fa=tab[0];
double fb=tab[1];
int i;
for (i = 3; i <= M; i++){
e = e / 2;
c = a + e;
fc = f(c);
if (std::fabs(e) <delta || std::fabs(fc) < eps) {
break;
return c;
}
if (sgn(fc)!=sgn(fa)) {
b = c;
fb = fc;
if(e<0.1) {
tab[0]=fa;
tab[1]=fb;
return SecantMethod(f, a, b, M, eps, delta, tab);
break;
}
} else {
a = c;
fa = fc;
if(e<0.1) {
tab[0]=fa;
tab[1]=fb;
return SecantMethod(f, a, b, M, eps, delta, tab);
break;
}
}
}
return c;
}
}