-
Notifications
You must be signed in to change notification settings - Fork 0
/
zadanie_2.cpp
executable file
·54 lines (48 loc) · 981 Bytes
/
zadanie_2.cpp
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
#include <iostream>
#include <cmath>
using namespace std;
// Tomasz Targiel
// Lista 4
// Zadanie 2
double find_root(double (*f)(double), double a, double b, double tolerance = 1e-6)
{
if (f(a) * f(b) > 0)
{
cout << "Funkcja nie posiada miejsc zerowych w podanym przedziale" << endl;
}
while (abs(a - b) > tolerance)
{
double x1 = (a + b) / 2;
if (abs(f(x1)) <= tolerance)
{
return x1;
}
else if (f(x1) * f(a) < 0)
{
b = x1;
}
else
{
a = x1;
}
}
return 0;
}
double f(double x)
{
return cos(x) - x;
}
double f2(double x)
{
return cos(x) - 0.5;
}
int main()
{
double a = 0;
double b = 2;
cout << "a. cos(x) = x w przedziale [0, 2], m.zer: " << find_root(f, a, b) << endl;
a = 0;
b = 1.5;
cout << "b. cos(x) = 1/2 w przedziale [0, 1.5], m.zer: " << find_root(f2, a, b) << endl;
return 0;
}