#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "header.h"

int main() {
	srand(time(NULL));
	double a = 1, b = 3;
	double initial_a = 1; // задача Коши y(1) = 1;
	char path[100];
	double ugly_initial;
	int ugly_n = 1000;
	double* start_points = malloc(sizeof(double)*2);
	if (start_points == NULL) {
		exit(16);
	}
	start_points[0] = initial_a;
	double* yy;
	double* x;




	for (int n = 1000;n <= 10;n++) {
		FILE* file;
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab13/res/%d.txt",n);
		file = fopen(path, "w+");
		double h = (b-a)/2;

		double y_1 = initial_a + h * f(a + h, initial_a);
		double y_12 = initial_a + (h / 2) * f(a + h, initial_a);
		double y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);

		while (fabs(y_2 - y_1) > 0.0001) {
			h /= 2;
			y_1 = initial_a + h * f(a + h, initial_a);
			y_12 = initial_a + (h / 2) * f(a + h, initial_a);
			y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);
		}//вычисление стартовых точек методом Эйлера (достижение заданой точности по правило Рунге)

		start_points[1] = y_2;
		h = (b - a) / (n-1);
		

		x = create_grid_even(n, a, b);
		yy = adams_moulten(start_points, a, b, f, n);
		for (int i = 0;i < n;i++) {
			fprintf(file, "%.16lf %.16lf", x[i], yy[i]);
			if (i != n - 1) {
				fprintf(file, "\n");
			}
		}

		free(yy);
		free(x);
		fclose(file);
	}
	



	double** ans_;
	// Выичсление решения с заданой точностью
	for (int power = 1;power <= 6;power++) {
		FILE* file, * file_h;
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab13/res/runge_%i.txt", power);
		file = fopen(path, "w+");
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab13/res/runge_h_%i.txt", power);
		file_h = fopen(path, "w+");

		int n, len_h;


		ans_ = runge_adams(pow(10, -power), initial_a, a, b, f, &n, &len_h);

		for (int i = 0;i < n;i++) {
			fprintf(file, "%.16lf %.16lf %.20lf", ans_[0][i], ans_[1][i], ans_[3][i]);
			if (i != n - 1) {
				fprintf(file, "\n");

			}
		}
		for (int k = 0;k < len_h;k++) {
			fprintf(file_h, "%.20lf", ans_[2][k]);
			if (k != len_h - 1) {
				fprintf(file_h, "\n");
			}
		}
		free(ans_[0]);
		free(ans_[1]);
		free(ans_[2]);
		free(ans_[3]);
		free(ans_);
		fclose(file);
		fclose(file_h);
	}




	// Доп. задание
	for (int procent = 1;procent <= 0;procent++) {

		for (int i = 0;i < 20; i++) {
			sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab13/res_additional/ugly_%i_%i.txt", procent, i + 1);
			FILE* file;
			file = fopen(path, "w+");

			ugly_initial = uglify_initial(initial_a, procent, procent - 1);


			start_points[0] = ugly_initial;
			double h = (b - a) / 2;
			double y_1 = initial_a + h * f(a + h, initial_a);
			double y_12 = initial_a + (h / 2) * f(a + h, initial_a);
			double y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);

			while (fabs(y_2 - y_1) > 0.0001) {
				h /= 2;
				y_1 = initial_a + h * f(a + h, initial_a);
				y_12 = initial_a + (h / 2) * f(a + h, initial_a);
				y_2 = y_12 + (h / 2) * f(a + h / 2, y_12);
			}//вычисление стартовых точек методом Эйлера (достижение заданой точности по правило Рунге)

			start_points[1] = y_2;
			h = (b - a) / ugly_n;



			
			double* x = create_grid_even(ugly_n, a, b);
			double* y = adams_moulten(start_points, a, b, f, ugly_n);
			for (int i = 0;i < ugly_n;i++) {
				fprintf(file, "%.16lf %.16lf", x[i], y[i]);
				if (i != ugly_n - 1) {
					fprintf(file, "\n");
				}
			}
			fclose(file);
			free(x);
			free(y);

		}
	}
	free(start_points);
	return 0;
}