Ivan Glagolev 4 months ago
commit
9c7a1fd6dd

+ 13 - 0
Lab1/GraphB/bisection_alg_B.csv

@@ -0,0 +1,13 @@
+1, 2.320508e-01, 1.500000
+2, 1.794919e-02, 1.750000
+3, 1.070508e-01, 1.625000
+4, 4.455081e-02, 1.687500
+5, 1.330081e-02, 1.718750
+6, 2.324192e-03, 1.734375
+7, 5.488308e-03, 1.726563
+8, 1.582058e-03, 1.730469
+9, 3.710674e-04, 1.732422
+10, 6.054951e-04, 1.731445
+11, 1.172138e-04, 1.731934
+12, 1.269268e-04, 1.732178
+13, 4.856494e-06, 1.732056

+ 15 - 0
Lab1/GraphB/bisection_algbrake_B.csv

@@ -0,0 +1,15 @@
+1, 2.294919e-02, 1.755000
+2, 9.955081e-02, 1.632500
+3, 3.830081e-02, 1.693750
+4, 7.675808e-03, 1.724375
+5, 7.636692e-03, 1.739687
+6, 1.955757e-05, 1.732031
+7, 3.808567e-03, 1.735859
+8, 1.894505e-03, 1.733945
+9, 9.374737e-04, 1.732988
+10, 4.589581e-04, 1.732510
+11, 2.197002e-04, 1.732271
+12, 1.000713e-04, 1.732151
+13, 4.025688e-05, 1.732091
+14, 1.034966e-05, 1.732061
+15, 4.603956e-06, 1.732046

+ 14 - 0
Lab1/GraphB/bisection_poly_B.csv

@@ -0,0 +1,14 @@
+1, 1.142801e-04, 1.000000
+2, 4.998857e-01, 1.500000
+3, 2.498857e-01, 1.250000
+4, 1.248857e-01, 1.125000
+5, 6.238572e-02, 1.062500
+6, 3.113572e-02, 1.031250
+7, 1.551072e-02, 1.015625
+8, 7.698220e-03, 1.007813
+9, 3.791970e-03, 1.003906
+10, 1.838845e-03, 1.001953
+11, 8.622824e-04, 1.000977
+12, 3.740011e-04, 1.000488
+13, 1.298605e-04, 1.000244
+14, 7.790196e-06, 1.000122

+ 5 - 0
Lab1/GraphB/secant_alg_B.csv

@@ -0,0 +1,5 @@
+1, 2.135323e-01, 1.518519
+2, 4.746056e-02, 1.684590
+3, 1.029138e-02, 1.742342
+4, 4.320754e-04, 1.731619
+5, 3.829350e-06, 1.732047

+ 8 - 0
Lab1/GraphB/secant_algbrake_B.csv

@@ -0,0 +1,8 @@
+1, 2.505503e-01, 1.982601
+2, 1.208524e-01, 1.611198
+3, 1.035074e-01, 1.835558
+4, 4.597377e-02, 1.778025
+5, 1.403497e-02, 1.718016
+6, 2.161506e-03, 1.734212
+7, 1.056473e-04, 1.732156
+8, 7.846451e-07, 1.732050

+ 6 - 0
Lab1/GraphB/secant_poly_B.csv

@@ -0,0 +1,6 @@
+1, 3.750143e-01, 0.625100
+2, 1.409977e-01, 0.859117
+3, 2.318478e-02, 1.023299
+4, 1.413939e-03, 0.998700
+5, 1.401972e-05, 1.000100
+6, 8.496879e-09, 1.000114

+ 15 - 0
Lab1/GraphC/bisection_alg_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 1.250000e-01, 1.687500
+1.000000e-02, 1.562500e-02, 1.726563
+1.000000e-03, 1.953125e-03, 1.731445
+1.000000e-04, 1.220703e-04, 1.731995
+1.000000e-05, 1.525879e-05, 1.732048
+1.000000e-06, 1.907349e-06, 1.732051
+1.000000e-07, 1.192093e-07, 1.732051
+1.000000e-08, 1.490116e-08, 1.732051
+1.000000e-09, 1.862645e-09, 1.732051
+1.000000e-10, 1.164153e-10, 1.732051
+1.000000e-11, 1.455192e-11, 1.732051
+1.000000e-12, 1.818989e-12, 1.732051
+1.000000e-13, 1.136868e-13, 1.732051
+1.000000e-14, 1.421085e-14, 1.732051
+1.000000e-15, 1.776357e-15, 1.732051

+ 15 - 0
Lab1/GraphC/bisection_algbrake_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 1.225000e-01, 1.693750
+1.000000e-02, 1.531250e-02, 1.732031
+1.000000e-03, 1.914063e-03, 1.732988
+1.000000e-04, 1.196289e-04, 1.732091
+1.000000e-05, 1.495361e-05, 1.732054
+1.000000e-06, 1.869202e-06, 1.732051
+1.000000e-07, 1.168251e-07, 1.732051
+1.000000e-08, 1.460314e-08, 1.732051
+1.000000e-09, 1.825392e-09, 1.732051
+1.000000e-10, 1.140870e-10, 1.732051
+1.000000e-11, 1.426081e-11, 1.732051
+1.000000e-12, 1.782574e-12, 1.732051
+1.000000e-13, 1.114664e-13, 1.732051
+1.000000e-14, 1.398881e-14, 1.732051
+1.000000e-15, 1.776357e-15, 1.732051

+ 15 - 0
Lab1/GraphC/bisection_poly_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 1.250000e-01, 1.062500
+1.000000e-02, 1.562500e-02, 1.007813
+1.000000e-03, 1.953125e-03, 1.000977
+1.000000e-04, 1.220703e-04, 1.000061
+1.000000e-05, 1.525879e-05, 1.000114
+1.000000e-06, 1.907349e-06, 1.000113
+1.000000e-07, 1.192093e-07, 1.000114
+1.000000e-08, 1.490116e-08, 1.000114
+1.000000e-09, 1.862645e-09, 1.000114
+1.000000e-10, 1.164153e-10, 1.000114
+1.000000e-11, 1.455192e-11, 1.000114
+1.000000e-12, 1.818989e-12, 1.000114
+1.000000e-13, 1.136868e-13, 1.000114
+1.000000e-14, 1.421085e-14, 1.000114
+1.000000e-15, 1.776357e-15, 1.000114

+ 15 - 0
Lab1/GraphC/secant_alg_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 5.775194e-02, 1.742342
+1.000000e-02, 4.282460e-04, 1.732047
+1.000000e-03, 4.282460e-04, 1.732047
+1.000000e-04, 3.830784e-06, 1.732051
+1.000000e-05, 3.830784e-06, 1.732051
+1.000000e-06, 1.433259e-09, 1.732051
+1.000000e-07, 1.433259e-09, 1.732051
+1.000000e-08, 1.433259e-09, 1.732051
+1.000000e-09, 4.662937e-15, 1.732051
+1.000000e-10, 4.662937e-15, 1.732051
+1.000000e-11, 4.662937e-15, 1.732051
+1.000000e-12, 4.662937e-15, 1.732051
+1.000000e-13, 4.662937e-15, 1.732051
+1.000000e-14, 4.662937e-15, 1.732051
+1.000000e-15, 2.220446e-16, 1.732051

+ 15 - 0
Lab1/GraphC/secant_algbrake_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 1.739893e-02, 1.982601
+1.000000e-02, 2.055859e-03, 1.732156
+1.000000e-03, 1.064320e-04, 1.732050
+1.000000e-04, 7.849305e-07, 1.732051
+1.000000e-05, 7.849305e-07, 1.732051
+1.000000e-06, 7.849305e-07, 1.732051
+1.000000e-07, 2.854119e-10, 1.732051
+1.000000e-08, 2.854119e-10, 1.732051
+1.000000e-09, 2.854119e-10, 1.732051
+1.000000e-10, 6.661338e-16, 1.732051
+1.000000e-11, 6.661338e-16, 1.732051
+1.000000e-12, 6.661338e-16, 1.732051
+1.000000e-13, 6.661338e-16, 1.732051
+1.000000e-14, 6.661338e-16, 1.732051
+1.000000e-15, 6.661338e-16, 1.732051

+ 15 - 0
Lab1/GraphC/secant_poly_c.csv

@@ -0,0 +1,15 @@
+1.000000e-01, 2.459872e-02, 0.998700
+1.000000e-02, 1.399919e-03, 1.000100
+1.000000e-03, 1.402822e-05, 1.000114
+1.000000e-04, 1.402822e-05, 1.000114
+1.000000e-05, 8.496930e-09, 1.000114
+1.000000e-06, 8.496930e-09, 1.000114
+1.000000e-07, 8.496930e-09, 1.000114
+1.000000e-08, 8.496930e-09, 1.000114
+1.000000e-09, 5.107026e-14, 1.000114
+1.000000e-10, 5.107026e-14, 1.000114
+1.000000e-11, 5.107026e-14, 1.000114
+1.000000e-12, 5.107026e-14, 1.000114
+1.000000e-13, 5.107026e-14, 1.000114
+1.000000e-14, 0.000000e+00, 1.000114
+1.000000e-15, 0.000000e+00, 1.000114

+ 206 - 0
Lab1/Lab3.c

@@ -0,0 +1,206 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+
+typedef struct {
+    double value, tolerance;
+    int iteraction;
+} solution;
+
+
+double poly(double x) {
+	return 0.25 * pow(x,3) + x - 1.2502;
+}
+
+
+double alg(double x) {
+	return pow(x,4) + 3 * pow(x,3) - 9 * x - 9;
+}
+
+
+double algbrake(double x) {
+	return -(pow(x,4) + 3 * pow(x,3) - 9 * x - 9)/(x-1.5);
+}
+
+double secant(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+	for (int i = 1; i < N; i++)
+	{
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return xk2;
+}
+
+solution secant_solution(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+    solution sol;
+    double secant_sol = secant(f, xk, xk1, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i++)
+	{
+        sol.iteraction = i;
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+        sol.value = xk2;
+        sol.tolerance = fabs(xk2 - secant_sol);
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return sol;
+}
+
+void SolutionSecantComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < N; i++) {
+		solu = secant_solution(f, a, b, eps, i);
+		fprintf(file, "%d, %e, %lf\n", solu.iteraction, solu.tolerance, solu.value);
+		if (solu.tolerance < eps) {
+			break;
+		}
+	}
+	fclose(file);
+}
+
+
+double bisection(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+	if (fa * fb >= 0) {
+		return 0;
+	}
+	for (int i = 0; i < N; i ++) 
+	{   
+		c = 0.5 * (a + b);
+
+		if ((b - a) < 2*eps) {
+			return c;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+	}
+	return c;
+}
+
+solution bisection_solution(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+    solution sol;
+    double bisection_sol = bisection(f, a, b, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i ++) 
+	{   
+        sol.iteraction = i;
+		c = 0.5 * (a + b);
+        sol.value = c;
+
+		if ((b - a) < 2*eps) {
+			return sol;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+
+        sol.tolerance = fabs(c - bisection_sol);
+	}
+	return sol;
+}
+
+void SolutionBisectionComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < N; i++) {
+		solu = bisection_solution(f, a, b, eps, i);
+		fprintf(file, "%d, %e, %lf\n", solu.iteraction, solu.tolerance, solu.value);
+		if (solu.tolerance < eps) {
+			break;
+		}
+	}
+	fclose(file);
+}
+
+
+
+void PrintResultSolution(solution result) {
+    printf("Value:     %.10lf\n", result.value);
+    printf("Tolerance: %.10lf\n", result.tolerance);
+    printf("Iteration: %d\n", result.iteraction);
+}
+
+void PrintResult (double eps) {
+    solution x1 = secant_solution(algbrake, 1.51, 2, eps, 1000);
+	solution x2 = bisection_solution(algbrake, 1.51, 2, eps, 1000);
+    printf("Function with a brake: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x1);
+	printf("Bisection method: \n");
+	PrintResultSolution(x2);
+	
+    solution x3 = secant_solution(alg, 1, 2, eps, 1000);
+	solution x4 = bisection_solution(alg, 1, 2, eps, 1000);
+    printf("Algebraic function: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x3);
+	printf("Bisection method: \n");
+	PrintResultSolution(x4);
+
+    solution x5 = secant_solution(poly, 0, 2, eps, 1000);
+	solution x6 = bisection_solution(poly, 0, 2, eps, 1000);
+    printf("Polynomial: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x5);
+	printf("Bisection method: \n");
+	PrintResultSolution(x6);
+}
+
+
+
+
+
+int main() {
+	PrintResult(pow(10, -15));
+    SolutionSecantComparison(algbrake, 1.51, 2, pow(10, -5), 1000,"secant_algbrake_B.csv");
+	SolutionSecantComparison(poly, 0, 2, pow(10, -5), 1000, "secant_poly_B.csv");
+	SolutionSecantComparison(alg, 1, 2, pow(10, -5), 1000, "secant_alg_B.csv");
+    SolutionBisectionComparison(algbrake, 1.51, 2, pow(10, -5), 1000,"bisection_algbrake_B.csv");
+	SolutionBisectionComparison(poly, 0, 2, pow(10, -5), 1000, "bisection_poly_B.csv");
+	SolutionBisectionComparison(alg, 1, 2, pow(10, -5), 1000, "bisection_alg_B.csv");
+}

+ 0 - 0
Lab1/eps_tol.m


+ 96 - 0
Lab1/lab.c

@@ -0,0 +1,96 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+
+double poly(double x) {
+	return 0.25 * pow(x,3) + x - 1.2502;
+}
+
+
+double alg(double x) {
+	return pow(x,4) + 3 * pow(x,3) - 9 * x - 9;
+}
+
+
+double algbrake(double x) {
+	return -(pow(x,4) + 3 * pow(x,3) - 9 * x - 9)/(x-1.5);
+}
+
+double secant(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+	for (int i = 1; i < N; i++)
+	{
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return xk2;
+}
+
+double bisection(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+	if (fa * fb >= 0) {
+		return 0;
+	}
+	for (int i = 0; i < N; i ++) 
+	{   
+		c = 0.5 * (a + b);
+        
+		if ((b - a) < 2*eps) {
+			return c;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+	}
+	return c;
+}
+
+void PrintResult (double eps) {
+    double x1 = secant(algbrake, 1.51, 2, eps, 1000);
+	double x2 = bisection(algbrake, 1.51, 2, eps, 1000);
+    printf("Function with a brake: \n");
+    printf("Secant method: \n");
+	printf("x = %lf\n", x1);
+	printf("Bisection method: \n");
+	printf("x = %lf\n", x2);
+	
+    double x3 = secant(alg, 1, 2, eps, 1000);
+	double x4 = bisection(alg, 1, 2, eps, 1000);
+    printf("Algebraic function: \n");
+    printf("Secant method: \n");
+	printf("x = %lf\n", x3);
+	printf("Bisection method: \n");
+	printf("x = %lf\n", x4);
+
+    double x5 = secant(poly, 0, 2, eps, 1000);
+	double x6 = bisection(poly, 0, 2, eps, 1000);
+    printf("Polynomial: \n");
+    printf("Secant method: \n");
+	printf("x = %lf\n", x5);
+	printf("Bisection method: \n");
+	printf("x = %lf\n", x6);
+}
+
+
+int main() {
+	PrintResult(pow(10, -5));
+}

+ 0 - 0
Lab1/lab1.m


BIN
Lab1/lab1_otchet.pdf


+ 201 - 0
Lab1/lab4.c

@@ -0,0 +1,201 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+
+typedef struct {
+    double value, tolerance, epsilon; 
+} solution;
+
+
+double poly(double x) {
+	return 0.25 * pow(x,3) + x - 1.2502;
+}
+
+
+double alg(double x) {
+	return pow(x,4) + 3 * pow(x,3) - 9 * x - 9;
+}
+
+
+double algbrake(double x) {
+	return -(pow(x,4) + 3 * pow(x,3) - 9 * x - 9)/(x-1.5);
+}
+
+double secant(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+	for (int i = 1; i < N; i++)
+	{
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return xk2;
+}
+
+solution secant_solution(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+    solution sol;
+    double secant_sol = secant(f, xk, xk1, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i++)
+	{
+		sol.epsilon = eps;
+		if (fabs(xk1 - xk) < eps) {
+			sol.tolerance = fabs(xk1 - xk);
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+        sol.value = xk2;
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return sol;
+}
+
+void SolutionSecantComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < 16; i++) {
+		solu = secant_solution(f, a, b, pow(10,-i), 1000);
+		fprintf(file, "%e, %e, %lf\n", solu.epsilon, solu.tolerance, solu.value);
+	}
+	fclose(file);
+}
+
+
+double bisection(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+	if (fa * fb >= 0) {
+		return 0;
+	}
+	for (int i = 0; i < N; i ++) 
+	{   
+		c = 0.5 * (a + b);
+
+		if ((b - a) < 2*eps) {
+			return c;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+	}
+	return c;
+}
+
+solution bisection_solution(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+    solution sol;
+    double bisection_sol = bisection(f, a, b, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i ++) 
+	{   
+        sol.epsilon = eps;
+		c = 0.5 * (a + b);
+        sol.value = c;
+
+		if ((b - a) < 2*eps) {
+			sol.tolerance = fabs((b - a));
+			return sol;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+
+        
+	}
+	return sol;
+}
+
+void SolutionBisectionComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < 16; i++) {
+		solu = bisection_solution(f, a, b, pow(10,-i), 1000);
+		fprintf(file, "%e, %e, %lf\n", solu.epsilon, solu.tolerance, solu.value);
+		
+	}
+	fclose(file);
+}
+
+
+
+void PrintResultSolution(solution result) {
+    printf("Value:     %.10lf\n", result.value);
+    printf("Tolerance: %.10lf\n", result.tolerance);
+    printf("Epsilon: %d\n", result.epsilon);
+}
+
+void PrintResult (double eps) {
+    solution x1 = secant_solution(algbrake, 1.51, 2, eps, 1000);
+	solution x2 = bisection_solution(algbrake, 1.51, 2, eps, 1000);
+    printf("Function with a brake: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x1);
+	printf("Bisection method: \n");
+	PrintResultSolution(x2);
+	
+    solution x3 = secant_solution(alg, 1, 2, eps, 1000);
+	solution x4 = bisection_solution(alg, 1, 2, eps, 1000);
+    printf("Algebraic function: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x3);
+	printf("Bisection method: \n");
+	PrintResultSolution(x4);
+
+    solution x5 = secant_solution(poly, 0, 2, eps, 1000);
+	solution x6 = bisection_solution(poly, 0, 2, eps, 1000);
+    printf("Polynomial: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x5);
+	printf("Bisection method: \n");
+	PrintResultSolution(x6);
+}
+
+
+
+
+
+int main() {
+	PrintResult(pow(10, -15));
+    SolutionSecantComparison(algbrake, 1.51, 2, pow(10, -5), 1000,"secant_algbrake_C.csv");
+	SolutionSecantComparison(poly, 0, 2, pow(10, -5), 1000, "secant_poly_c.csv");
+	SolutionSecantComparison(alg, 1, 2, pow(10, -5), 1000, "secant_alg_c.csv");
+    SolutionBisectionComparison(algbrake, 1.51, 2, pow(10, -5), 1000,"bisection_algbrake_c.csv");
+	SolutionBisectionComparison(poly, 0, 2, pow(10, -5), 1000, "bisection_poly_c.csv");
+	SolutionBisectionComparison(alg, 1, 2, pow(10, -5), 1000, "bisection_alg_c.csv");
+}

+ 202 - 0
Lab1/lab5.c

@@ -0,0 +1,202 @@
+#include<stdio.h>
+#include<stdlib.h>
+#include<math.h>
+
+typedef struct {
+    double value, tolerance;
+    int iteraction;
+} solution;
+
+
+double poly(double x) {
+	return 0.25 * pow(x,3) + x - 1.2502;
+}
+
+
+double alg(double x) {
+	return pow(x,4) + 3 * pow(x,3) - 9 * x - 9;
+}
+
+
+double algbrake(double x) {
+	return -(pow(x,4) + 3 * pow(x,3) - 9 * x - 9)/(x-1.5);
+}
+
+double secant(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+	for (int i = 1; i < N; i++)
+	{
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return xk2;
+}
+
+solution secant_solution(double(*f)(double), double xk, double xk1, double eps, int N) {
+	double xk2;
+	double fxk = f(xk);
+	double fxk1 = f(xk1);
+    solution sol;
+    double secant_sol = secant(f, xk, xk1, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i++)
+	{
+        sol.iteraction = i;
+		if (fabs(xk1 - xk) < eps) {
+			break;
+		}
+		xk2 = xk1 - fxk1 * ((xk1 - xk) / (fxk1 - fxk));
+        sol.value = xk2;
+        sol.tolerance = fabs(xk2 - secant_sol);
+		xk = xk1;
+		fxk = fxk1;
+		xk1 = xk2;
+		fxk1 = f(xk2);
+	}
+	return sol;
+}
+
+void SolutionSecantComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < N; i++) {
+		solu = secant_solution(f, a, b, eps, i);
+		fprintf(file, "%d, %e, %lf\n", solu.iteraction, solu.tolerance, solu.value);
+		if (solu.tolerance < eps) {
+			break;
+		}
+	}
+	fclose(file);
+}
+
+
+double bisection(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+	if (fa * fb >= 0) {
+		return 0;
+	}
+	for (int i = 0; i < N; i ++) 
+	{   
+		c = 0.5 * (a + b);
+
+		if ((b - a) < 2*eps) {
+			return c;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+	}
+	return c;
+}
+
+solution bisection_solution(double(*f)(double), double a, double b, double eps, int N) {
+	double fa = f(a);
+	double fb = f(b);
+	double c;
+    solution sol;
+    double bisection_sol = bisection(f, a, b, pow(10, -15), 1000);
+
+	for (int i = 1; i < N+1; i ++) 
+	{   
+        sol.iteraction = i;
+		c = 0.5 * (a + b);
+        sol.value = c;
+
+		if ((b - a) < 2*eps) {
+			return sol;
+		}
+
+		double fc = f(c);
+
+		if (fa * fc > 0) {
+			a = c;
+			fa = fc;
+		}
+
+		else {
+			b = c;
+			fb = fc;
+		}
+
+        sol.tolerance = fabs(c - bisection_sol);
+	}
+	return sol;
+}
+
+void SolutionBisectionComparison(double(*f)(double), double a, double b, double eps, int N, char const *filename) {
+	solution solu;
+	FILE* file;
+	file = fopen(filename, "w+");
+	for (int i = 1; i < N; i++) {
+		solu = bisection_solution(f, a, b, eps, i);
+		fprintf(file, "%d, %e, %lf\n", solu.iteraction, solu.tolerance, solu.value);
+		if (solu.tolerance < eps) {
+			break;
+		}
+	}
+	fclose(file);
+}
+
+
+
+void PrintResultSolution(solution result) {
+    printf("Value:     %.10lf\n", result.value);
+    printf("Tolerance: %.10lf\n", result.tolerance);
+    printf("Iteration: %d\n", result.iteraction);
+}
+
+void PrintResult (double eps) {
+    solution x1 = secant_solution(algbrake, 1.51, 2, eps, 1000);
+	solution x2 = bisection_solution(algbrake, 1.51, 2, eps, 1000);
+    printf("Function with a brake: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x1);
+	printf("Bisection method: \n");
+	PrintResultSolution(x2);
+	
+    solution x3 = secant_solution(alg, 1, 2, eps, 1000);
+	solution x4 = bisection_solution(alg, 1, 2, eps, 1000);
+    printf("Algebraic function: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x3);
+	printf("Bisection method: \n");
+	PrintResultSolution(x4);
+
+    solution x5 = secant_solution(poly, 0, 2, eps, 1000);
+	solution x6 = bisection_solution(poly, 0, 2, eps, 1000);
+    printf("Polynomial: \n");
+    printf("Secant method: \n");
+	PrintResultSolution(x5);
+	printf("Bisection method: \n");
+	PrintResultSolution(x6);
+}
+
+
+
+
+
+int main() {
+	PrintResult(pow(10, -10));
+    printf("\n\n");
+    PrintResult(2*pow(10, -11));
+}

+ 71 - 0
Lab1/n_tol.m

@@ -0,0 +1,71 @@
+data=csvread('bisection_poly_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Bisection method');
+title("$\bf Tolerance(Iteration) \; 0.25 \cdot x^3 + x - 1.2502 = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')
+
+data=csvread('secant_poly_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Secant method');
+title("$\bf Tolerance(Iteration) \; 0.25 \cdot x^3 + x - 1.2502 = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')
+
+data=csvread('bisection_alg_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Bisection method');
+title("$\bf Tolerance(Iteration) \; x^4 - 3 \cdot x^3 - 9 \cdot x - 9 = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')
+
+data=csvread('secant_alg_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Secant method');
+title("$\bf Tolerance(Iteration) \; x^4 - 3 \cdot x^3 - 9 \cdot x - 9 = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')
+
+data=csvread('bisection_algbrake_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Bisection method');
+title("$\bf Tolerance(Iteration) \; -\frac{x^4 + 3 \cdot x^3 - 9 \cdot x - 9}{x - 1.5} = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')
+
+data=csvread('secant_algbrake_B.csv'); % reading csv file 
+X = data(:,1);
+Y = data(:,2); 
+figure;
+hold on;
+grid on;
+plot(X, Y);
+legend('Secant method');
+title("$\bf Tolerance(Iteration) \; -\frac{x^4 + 3 \cdot x^3 - 9 \cdot x - 9}{x - 1.5} = 0$", 'Interpreter','latex');
+xlabel('Iteration')
+ylabel('Tolerance')