N atija :
Dastur kodi :
// Trapetsiyalar usuli
#include
using namespace std;
double Function(double x) {
return sqrt((18 - x) / (x - 6));
}
int main() {
double a = 8, b = 10, x, s = 0;
int n = 100;
double h = (b - a) / n;
for (int i = 1; i < n; i++) {
if ((i + 1) % 10 == 0) {
cout << "n = " << i + 1 << "\t\t";
cout << "x = " << x << "\t";
cout << "f(x) = " << Function(x) << "\t\t";
cout << "f(x) * h = " << Function(x) * h << endl;
}
x = a + i * h;
s += Function(x);
}
double integral = (h / 2) * (Function(a) + Function(b) + 2 * s);
cout << "\nIntegral = " << integral << endl;
}
Natija :
Dastur kodi :
// Simpson usuli
#include
using namespace std;
double Function(double x) {
return sqrt((18 - x) / (x - 6));
}
int main() {
double a = 8, b = 10, x, s = 0, integral;
int n = 100;
double h = (b - a) / n;
for (int i = 1; i < n; i++) {
if ((i + 1) % 10 == 0) {
cout << "n = " << i + 1 << "\t\t";
cout << "x = " << x << "\t";
cout << "f(x) = " << Function(x) << "\t\t";
cout << "f(x) * h = " << Function(x) * h << endl;
}
x = a + i * h;
if (i % 2 == 0) {
s += 2 * Function(x);
} else {
s += 4 * Function(x);
}
}
integral = (h / 3) * (Function(a) + Function(b) + s);
cout << "\nIntegral = " << integral << endl;
}
Natija :
n
|
10
|
20
|
30
|
40
|
50
|
60
|
70
|
80
|
90
|
100
|
S(yuza)
|
To‘g‘ri to‘rtburchaklar usuli
|
x
|
8.19
|
8.39
|
8.59
|
8.79
|
8.99
|
9.19
|
9.39
|
9.59
|
9.79
|
9.99
|
3.47947
|
f(x)
|
2.12
|
2.00
|
1.90
|
1.81
|
1.74
|
1.66
|
1.59
|
1.53
|
1.47
|
1.42
|
f(x)*h
|
0.04
|
0.04
|
0.04
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.02
|
Trapetsiyalar usuli
|
x
|
8.16
|
8.36
|
8.56
|
8.76
|
8.96
|
9.16
|
9.36
|
9.56
|
9.76
|
9.96
|
3.52408
|
f(x)
|
2.13
|
2.02
|
1.92
|
1.83
|
1.75
|
1.67
|
1.60
|
1.54
|
1.48
|
1.42
|
f(x)*h
|
0.04
|
0.04
|
0.04
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.02
|
Simpson usuli
|
x
|
8.16
|
8.36
|
8.56
|
8.76
|
8.96
|
9.16
|
9.36
|
9.56
|
9.76
|
9.96
|
3.52406
|
f(x)
|
2.13
|
2.02
|
1.92
|
1.83
|
1.75
|
1.67
|
1.60
|
1.54
|
1.48
|
1.42
|
f(x)*h
|
0.04
|
0.04
|
0.04
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.03
|
0.02
|
Tahlil qilish va xulosa.
Aniq integralni o’zimiz hisoblaganimizda natija :
Xulosa qilib aytganda dasturimiz aniq integralni deyarli xatosiz hisoblab berdi.
Topshiriq 3
Masalaning berilishi. Berilgan algebraik va transsendent tenglamalarni yechishda oraliqni teng ikkiga bo‘lish va vatarlar usullaridan foydalanib tenglamaning taqribiy ildizini 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001 aniqliklarda hisoblansin. Olingan natijalar quyidagi jadvalga to’ldirilib tahlil qilinsin.
Masalaning algoritmi.
Dastur kodi :
// Kesmani 2 ga bo'lish
#include
using namespace std;
double Function(double x) {
return atan(x * x) - 1 - 1 / (1 + x * x);
}
int main() {
double a = 1.6, b = 1.8, c = (a + b) / 2, e = 0.001;
if (Function(a) * Function(b) > 0) {
cout << "Bu oraliqda ildizi yo'q";
return 0;
}
while ((b - a) / 2 > e) {
c = (a + b) / 2;
if (Function(a) * Function(c) <= 0)
b = c;
else a = c;
}
cout << c;
}
Natija :
Dastur kodi :
// Vatarlar
#include
using namespace std;
double Function(double x) {
return atan(x * x) - 1 - 1 / (1 + x * x);
}
int main() {
double a = 1.6, b = 1.8, e = 0.01;
float xm, x0, c;
if (Function(a) * Function(b) > 0) {
cout << "Bu oraliqda ildizi yo'q";
return 0;
}
do {
x0 = (a * Function(b) - b * Function(a)) / (Function(b) - Function(a));
c = Function(a) * Function(x0);
a = b;
b = x0;
if (c == 0) break;
xm = (a * Function(b) - b * Function(a)) / (Function(b) - Function(a));
} while (fabs(xm - x0) >= e);
cout << x0;
}
Natija :
Dastur kodi :
// Nyuton(urunma)
#include
using namespace std;
double Function(double x) {
return atan(x * x) - 1 - 1 / (1 + x * x);
}
double Function1(double x) {
double u = x * x;
return 2 * x / (1 +pow(x, 4)) + 2 * x / ((1 + u) * (1 + u));
}
int main() {
double a = 1.6, b = 1.8, e = 0.001;
double h = Function(b) / Function1(b);
if (Function(a) * Function(b) > 0) {
cout << "Bu oraliqda ildizi yo'q";
return 0;
}
while (abs(h) >= e) {
h = Function(b) / Function1(b);
b = b - h;
}
cout << b;
}
Natija :
epsilon
|
0.1
|
0.01
|
0.001
|
0.0001
|
0.00001
|
0.000001
|
Kesmani 2 ga bo’lish
|
1.7
|
1.7375
|
1.73281
|
1.73379
|
1.73375
|
1.73375
|
Vatarlar
|
1.73974
|
1.73974
|
1.73347
|
1.73375
|
1.73375
|
1.73375
|
Nyuton (urunma)
|
1.8
|
1.73374
|
1.73375
|
1.73375
|
1.73375
|
1.73375
|
Tahlil qilish va xulosa : Ikki funksiya grafigi chizilganda [1.6;1.8] oraqaliqda 1 ta yechimi bor
Xulosa qilib aytganda dasturimiz 0.000001 aniqlikda xatolikka yo’l qo’yilgan.
Do'stlaringiz bilan baham: |