4. Algoritmni to’g’riligini tekshirish. Har bir formulani amaliyotda sinovdan o'tkazish juda muhimdir.
Dastur Kodi:
#To’rtburchaklar usuli:
import 'dart:math';
void main() {
int n = 1000; // Tartiblangan taqiqlashlar soni (n>0)
double lowerLimit = 0; // Integrallash diapazoni: past chegreni
double upperLimit = 3; // Integrallash diapazoni: yuqori chegreni
double approximateResult = approximateIntegral(n, lowerLimit, upperLimit);
print('Taqribiy natija: $approximateResult');
}
double approximateIntegral(int n, double lowerLimit, double upperLimit) {
double sum = 0;
double deltaX = (upperLimit - lowerLimit) / n;
for (int i = 0; i < n; i++) {
double x = lowerLimit + (i + 0.5) * deltaX;
sum += 3.pow(x) * cos(3 + x);
}
return sum * deltaX;
}
# Trapetsiya usuli:
void main() {
int n = 1000; // Tartiblangan taqiqlashlar soni (n>0)
double lowerLimit = 0; // Integrallash diapazoni: past chegreni
double upperLimit = 3; // Integrallash diapazoni: yuqori chegreni
double approximateResult = approximateIntegral(n, lowerLimit, upperLimit);
print('Taqribiy natija: $approximateResult');
}
double approximateIntegral(int n, double lowerLimit, double upperLimit) {
double sum = 0;
double deltaX = (upperLimit - lowerLimit) / n;
for (int i = 0; i < n; i++) {
double x1 = lowerLimit + i * deltaX;
double x2 = lowerLimit + (i + 1) * deltaX;
double f1 = 3.pow(x1) * cos(3 + x1);
double f2 = 3.pow(x2) * cos(3 + x2);
sum += (f1 + f2) / 2;
}
return sum * deltaX;
}
# Simpson usuli:
import 'dart:math';
void main() {
int n = 1000;
double lowerLimit = 0;
double upperLimit = 3;
double approximateResult = approximateIntegral(n, lowerLimit, upperLimit);
print('Taqribiy natija: $approximateResult');
}
double approximateIntegral(int n, double lowerLimit, double upperLimit) {
if (n % 2 != 0) {
throw Exception('Taqiqlashlar soni juft bo\'lishi kerak.');
}
double sum = 0;
double deltaX = (upperLimit - lowerLimit) / n;
for (int i = 0; i < n; i += 2) {
double x0 = lowerLimit + i * deltaX;
double x1 = lowerLimit + (i + 1) * deltaX;
double x2 = lowerLimit + (i + 2) * deltaX;
double f0 = 3.pow(x0) * cos(3 + x0);
double f1 = 3.pow(x1) * cos(3 + x1);
double f2 = 3.pow(x2) * cos(3 + x2);
sum += (f0 + 4 * f1 + f2);
}
return sum * (deltaX / 3);
}
Natija:
|