#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* ans;
	double ugly_initial;
	int ugly_n = 1000;


	for (int n = 10;n <= 0;n++) {
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab12/res/%i.txt",n);
		FILE* file;
		file = fopen(path, "w+");
		double* x = create_grid_even(n, a, b);
		double* y = euler_method(initial_a, a, b, f, n);
		for (int i = 0;i < n;i++) {
			fprintf(file, "%.16lf %.16lf", x[i], y[i]);
			if (i != n - 1) {
				fprintf(file, "\n");
			}
		}
		fclose(file);
		free(x);
		free(y);
	}

	double** ans_;


	for (int power = 1;power <= 6;power++) {

		FILE* file,*file_h;
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab12/res/runge_%i.txt", power);
		file = fopen(path, "w+");
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab12/res/runge_h_%i.txt", power);
		file_h = fopen(path, "w+");

		int n,len_h;
		

		ans_ = runge(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_);
		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/lab12/res_additional/ugly_%i_%i.txt", procent,i+1);
			FILE* file;
			file = fopen(path, "w+");

			ugly_initial = uglify_initial(initial_a, procent, procent - 1);
			file = fopen(path, "w+");
			double* x = create_grid_even(ugly_n, a, b);
			double* y = euler_method(ugly_initial, 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);

		}
	}
	return 0;
}