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

double f(double x) {
	return 1 - sin(x);
}

double p(double x) {
	return cos(x);
}
double q(double x) {
	return sin(x);
}

double F(double x, vector y) {
	return f(x) - p(x) * y.y2 - q(x) * y.y1;
}

double F_0(double x, vector y) {
	return -p(x) * y.y2 - q(x) * y.y1;
}

vector F_vect(double x, vector y) {
	vector ans = { y.y2,F(x,y) };
	return ans;
}
vector F_vect_0(double x, vector y) {
	vector ans = { y.y2,F_0(x,y) };
	return ans;
}







int main() {
	double A = 0, B = 1, alpha_0 = 1, alpha_1 = 0, betta_0 = 1, betta_1 = 1,a =0,b=M_PI_2;
	
	FILE* file;
	vector* initials = find_initials(A, alpha_0, alpha_1);
	char path[100];

	//task1
	int n = 3;
	double h = (b - a) / (n - 1);
	double* task1 = solve(initials, betta_0, betta_1, B, a, b, F_vect, F_vect_0, n);
	sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab14/res/%i.txt", n);
	file = fopen(path, "w+");
	for (int i = 0;i < n;i++) {
		fprintf(file, "%lf %lf", a+i*h,task1[i]);
		if (i != n - 1) {
			fprintf(file, "\n");
		}
	}
	fclose(file);
	n = 10000;
	h = (b - a) / (n - 1);
	task1 = solve(initials, betta_0, betta_1, B, a, b, F_vect, F_vect_0, n);
	sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab14/res/%i.txt", n);
	file = fopen(path, "w+");
	for (int i = 0;i < n;i++) {
		fprintf(file, "%lf %lf", a + i * h, task1[i]);
		if (i != n - 1) {
			fprintf(file, "\n");
		}
	}
	fclose(file);
	free(task1);
	//task2
	double eps;
	int n_=0, len_h=0;
	double** ans;
	for (int power = 0;power <= 5;power++) {
		eps = pow(10, -power);
		
		FILE* file, * file_h;
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab14/res/runge_%i.txt", power);
		file = fopen(path, "w+");
		sprintf(path, "C:/Users/egorl/source/repos/gitbranch/num_methods_sem2/lab14/res/runge_h_%i.txt", power);
		file_h = fopen(path, "w+");

		


		ans = solve_runge(initials, betta_0, betta_1, B, a, b, F_vect, F_vect_0, eps, &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);
	}

	return 0;
}