-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3.cpp
More file actions
156 lines (117 loc) · 4.94 KB
/
Copy pathLab3.cpp
File metadata and controls
156 lines (117 loc) · 4.94 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Napisz program w języku „C/C++”, realizujący metody:
* (a) Picarda
* (b) bisekcji
* (c) Newtona
* (d) siecznych
* rozwiązywania pojedynczych algebraicznych równań nieliniowych. Zastosuj program do
* przykładów z zadania 1. Zastosuj trzy niezależne kryteria zakończenia iteracji. Zadbaj o to, aby
* wyprowadzać na konsolę wyniki pośrednie obliczeń dla każdej iteracji, tak aby możliwe było
* obserwowanie zbieżności kolejnych przybliżeń pierwiastków i porównanie liczby iteracji
* niezbędnych do uzyskania rozwiązania o zadanej dokładności przez każdą z metod. W szczególności
* oblicz jak zmienia się estymator błędu rozwiązania oraz residuum równania w trakcie iteracji.
*/
#include <iostream>
#include <cmath>
#include <functional>
#define N_MAX 100
#define TOLX 1e-12
#define TOLF 1e-12
double function_tanh(double);
double function_sinh(double);
double derivative_tanh(double);
double derivative_sinh(double);
double phi_tanh(double);
double phi_sinh(double);
void picard(const std::function<double(double)> &, const std::function<double(double)> &);
void bisection(double, double, const std::function<double(double)> &);
void newton(double, const std::function<double(double)> &, const std::function<double(double)> &);
void secant(double, double, const std::function<double(double)> &);
int main() {
std::cout << "FUNKCJA: f(x) = tanh(x) + 2(x - 1)" << std::endl;
picard(function_tanh, phi_tanh);
bisection(0.0, 2.0, function_tanh);
newton(5.0, function_tanh, derivative_tanh);
secant(-1.0, 5.0, function_tanh);
std::cout << "FUNKCJA: f(x) = sinh(x) + x/4 - 1" << std::endl;
bisection(0.0, 2.0, function_sinh);
newton(5.0, function_sinh, derivative_sinh);
secant(-1.0, 5.0, function_sinh);
return 0;
}
double function_tanh(double x) {
return std::tanh(x) + 2.0 * (x - 1.0);
}
double function_sinh(double x) {
return std::sinh(x) + x / 4.0 - 1.0;
}
double derivative_tanh(double x) {
return 1.0 / (std::cosh(x) * std::cosh(x)) + 2.0;
}
double derivative_sinh(double x) {
return std::cosh(x) + 0.25;
}
double phi_tanh(double x) {
return 0.5 * (2.0 - std::tanh(x));
}
double phi_sinh(double x) {
return 4.0 * (1.0 - std::sinh(x));
}
void picard(const std::function<double(double)> &f, const std::function<double(double)> &phi) {
std::cout << "---------------------- Picard ----------------------" << std::endl;
std::printf("%-5s%-17s%-17s%-17s\n", "i", "Wartosc", "Estymator", "Residuum");
double x_0 = 1.0;
for (int i = 1; i <= N_MAX; i++) {
const double x_1 = phi(x_0);
const double estimator = std::fabs(x_1 - x_0);
const double residuum = std::fabs(f(x_1));
std::printf("%-3d %-10.12f %-10.12f %-10.12f\n", i, x_1, estimator, residuum);
if (estimator < TOLX && residuum < TOLF) break;
x_0 = x_1;
}
std::cout << std::endl;
}
void bisection(double a, double b, const std::function<double(double)> &f) {
std::cout << "--------------------- Bisekcja ---------------------" << std::endl;
std::printf("%-5s%-17s%-17s%-17s\n", "i", "Wartosc", "Estymator", "Residuum");
double x_n;
for (int i = 1; i <= N_MAX; i++) {
x_n = (a + b) / 2.0;
const double estimator = std::fabs(b - a) / 2.0;
const double residuum = std::fabs(f(x_n));
std::printf("%-3d %-10.12f %-10.12f %-10.12f\n", i, x_n, estimator, residuum);
if (estimator < TOLX && residuum < TOLF) break;
if ((f(a) < 0 && f(x_n) > 0) || (f(a) > 0 && f(x_n) < 0))
b = x_n;
else
a = x_n;
}
std::cout << std::endl;
}
void newton(double x_1, const std::function<double(double)> &f, const std::function<double(double)> &fd) {
std::cout << "---------------------- Newton ----------------------" << std::endl;
std::printf("%-5s%-17s%-17s%-17s\n", "i", "Wartosc", "Estymator", "Residuum");
for (int i = 1; i <= N_MAX; i++) {
const double x_n = x_1 - f(x_1) / fd(x_1);
const double estimator = std::fabs(x_n - x_1);
const double residuum = std::fabs(f(x_n));
std::printf("%-3d %-10.12f %-10.12f %-10.12f\n", i, x_n, estimator, residuum);
if (estimator < TOLX && residuum < TOLF) break;
x_1 = x_n;
}
std::cout << std::endl;
}
void secant(double x_1, double x_2, const std::function<double(double)> &f) {
std::cout << "---------------------- Sieczne ---------------------" << std::endl;
std::printf("%-5s%-17s%-17s%-17s\n", "i", "Wartosc", "Estymator", "Residuum");
for (int i = 1; i <= N_MAX; i++) {
const double x_n = x_2 - f(x_2) * (x_2 - x_1) / (f(x_2) - f(x_1));
const double estimator = std::fabs(x_n - x_2);
const double residuum = std::fabs(f(x_n));
std::printf("%-3d %-10.12f %-10.12f %-10.12f\n", i, x_n, estimator, residuum);
if (estimator < TOLX && residuum < TOLF) break;
x_1 = x_2;
x_2 = x_n;
}
std::cout << std::endl;
}