/
levg
/
numm
Обзор
Документация
Войти
/
levg
/
numm
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
10/code/Chisl2sem2homework.cpp
116 строк
4 KB
Лев Черняховский
10new
05 ноя 2024, 03:49
05 ноя 2024, 03:49
93e34f3
Код
Авторство
О чём код?
#include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <vector> double f(double x) { return std::pow(x, 4) - 2.2 * std::pow(x, 3) + 7.5 * std::pow(x, 2) - 7 * x - 3.9; } double rightRectanglesRule(double a, double b, int n, std::vector<double>& func_values) { double h = (b - a) / n; double integral = 0.0; if (func_values.empty()) { // Если массив пуст, вычисляем значения функции для первой итерации double x = a + h; for (int i = 1; i <= n; i++) { func_values.push_back(f(x)); integral += func_values.back(); x += h; } } else { // Увеличиваем количество точек в два раза, вычисляя только новые int prev_n = n / 2; double x = a + h; for (int i = 1; i <= prev_n; i++) { double new_x = x + h; func_values.push_back(f(new_x)); integral += func_values[2 * i - 2] + func_values.back(); x += 2 * h; } } return integral * h; } double adaptiveRightRectangles(double a, double b, double epsilon, int& iterations) { int n = 1; std::vector<double> func_values; double integral = rightRectanglesRule(a, b, n, func_values); double integral_half = integral; do { integral = integral_half; n *= 2; integral_half = rightRectanglesRule(a, b, n, func_values); iterations++; } while (std::abs(integral_half - integral) > epsilon && iterations < 1000); return integral_half; } void errorVsEpsilon(double a, double b) { std::ofstream file("error_vs_epsilon.txt"); file << "Epsilon\tError\n"; for (double epsilon = 1e-1; epsilon >= 1e-8; epsilon /= 10) { int iterations = 0; double exactValue = adaptiveRightRectangles(a, b, epsilon / 10, iterations); double approxValue = adaptiveRightRectangles(a, b, epsilon, iterations); double error = std::abs(approxValue - exactValue); std::cout << epsilon << std::endl; file << epsilon << "\t" << error << "\n"; } file.close(); } void iterationsVsEpsilon(double a, double b) { std::ofstream file("iterations_vs_epsilon.txt"); file << "Epsilon\tIterations\n"; for (double epsilon = 1e-1; epsilon >= 1e-8; epsilon /= 10) { int iterations = 0; adaptiveRightRectangles(a, b, epsilon, iterations); file << epsilon << "\t" << iterations << "\n"; } file.close(); } void errorVsSegmentLength(double a, double b) { std::ofstream file("error_vs_segment_length.txt"); file << "Segment_Length\tError\n"; int n = 1; std::vector<double> func_values; // Создаем вектор для хранения значений функции double exactValue = adaptiveRightRectangles(a, b, 1e-9, n); for (int i = 1; i <= 10; i++) { int segments = std::pow(2, i); // Передаем func_values в вызов функции double approxValue = rightRectanglesRule(a, b, segments, func_values); double error = std::abs(approxValue - exactValue); file << 1.0 / segments << "\t" << error << "\n"; } file.close(); } int main() { double a = 0; double b = 1; int iterations = 0; double epsilon = 1e-6; double result = adaptiveRightRectangles(a, b, epsilon, iterations); std::cout << "epsilon : result\t" << epsilon << ": " << result << "\n"; std::cout << "Iterations: " << iterations << "\n\n"; errorVsEpsilon(a, b); iterationsVsEpsilon(a, b); errorVsSegmentLength(a, b); return 0; }