Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lab_4 / prog

.cpp
Скачиваний:
52
Добавлен:
28.01.2022
Размер:
1.05 Кб
Скачать
#include <iostream>
#include <cmath>
using namespace std;

double f(double x, double y) {
	return pow(y, 2) * exp(x);
}

double RK(double x0, double y0, double h0, double m) {
	double x = x0, y = y0, h = h0 / m;
	double k1 = 0, k2 = 0, k3 = 0, k4 = 0;
	for (int i = 1; i <= m; i++) {
		k1 = f(x, y);
		k2 = f(x + h / 2, y + h * k1 / 2);
		k3 = f(x + h / 2, y + h * k2 / 2);
		k4 = f(x + h, y + h * k3);
		y += (h / 6) * (k1 + 2 * k2 + 2 * k3 + k4);
		x = x0 + i * h;
	}

	return y;
}

double Runge(double x0, double y0, double h0, double a, double b, double eps) {
	double y, y1, d = eps * (pow(2, 4) - 1), m = 1;
	int n = int((b - a) / h0);
	y = RK(x0, y0, h0, m);
	cout << "\t" << x0 << "\t" << y0 << endl;
	for (int i = 0; i <= n; i++) {
		y1 = y;
		if (abs(y1 - y) < d) {
			y1 = y;
			m *= 2;
			y = RK(x0, y0, h0, m);
		}
		x0 += h0;
		y0 = y;
		cout << "\t" << x0 << "\t" << y0 << endl;
	}

	return m;
}

int main() {
	double a = 0, b = 1.2;
	double x0 = 0, y0 = -2, h0 = 0.4, eps = pow(10, -4);
	Runge(x0, y0, h0, a, b, eps);

	return 0;
}
Соседние файлы в папке lab_4