Add Matvej's solve

This commit is contained in:
AZEN-SGG 2025-05-02 01:18:28 +03:00
parent c5ae5840ff
commit 3e90cc7ebb
22 changed files with 916 additions and 105 deletions

View file

@ -20,7 +20,7 @@ else
CLEAN = rm -f
endif
TARGET = a04.$(EXE)
TARGET = a02.$(EXE)
OBJ = main.o solve.o init_f.o
%.o: %.c

View file

@ -3,59 +3,112 @@
#include <math.h>
static int cl = 0;
static int cl_f = 0;
static int cl_d = 0;
int get_call_count (void)
int get_call_function_count (void)
{
return cl;
return cl_f;
}
int get_call_derivative_count (void)
{
return cl_d;
}
double f0 (double x)
{
cl++;
cl_f++;
(void)x;
return 1;
}
double d0 (double x)
{
cl_d++;
(void)x;
return 0;
}
double f1 (double x)
{
cl++;
return (x * 1e-100) - 1.0;
cl_f++;
return ((x * 1e-100) - 1.0) * 1e100;
}
double d1 (double x)
{
cl_d++;
return x;
}
double f2 (double x)
{
cl++;
cl_f++;
return 4 - x * x;
}
double d2 (double x)
{
cl_d++;
return - 2 * x;
}
double f3 (double x)
{
double x_2 = x * x;
cl++;
cl_f++;
return x * x_2 + 3 * x_2 + 16;
}
double d3 (double x)
{
cl_d++;
return 3 * x * x + 6 * x;
}
double f4 (double x)
{
double x_2 = x * x;
cl++;
cl_f++;
return 3 - 2 * x_2 - x_2 * x_2;
}
double d4 (double x)
{
double x_2 = x * x;
cl_d++;
return -4 * x - 4 * ((x * x) * x);
}
double f5 (double x)
{
cl++;
cl_f++;
return sqrt(fabs(x) + 1) - 2;
}
double d5 (double x)
{
cl_d++;
return (((signbit(x)) ? -1 : 1) * (1.0 / (sqrt(fabs(x) + 1))) * 0.5;
}
double f6 (double x)
{
cl++;
cl_f++;
return sqrt(sqrt(fabs(x) + 1) + 1) - 2;
}
double d6 (double x)
{
double sqrt_x = sqrt(fabs(x) + 1);
cl_d++;
return (((signbit(x)) ? -1 : 1) * (1.0 / (sqrt(sqrt_x + 1) * sqrt_x))) * 0.25;
}

View file

@ -2,12 +2,21 @@
#define INIT_F_H
int get_call_count (void);
int get_call_derivative_count (void);
double f0 (double x);
double d0 (double x);
double f1 (double x);
double d1 (double x);
double f2 (double x);
double d2 (double x);
double f3 (double x);
double d3 (double x);
double f4 (double x);
double d4 (double x);
double f5 (double x);
double d5 (double x);
double f6 (double x);
double d6 (double x);
#endif

View file

@ -3,62 +3,48 @@
#include <math.h>
#include "init_f.h"
#include "status.h"
#include "solve.h"
/* ./a.out a b eps M k */
/* ./a.out x_0 eps M k */
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 4;
status ret;
double t, x_0, eps, x = 0;
int m, k, cl, it, task = 2;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
double (*d) (double);
double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6, cos}; // TODO: Remove cos
if (
!((argc == 6) &&
sscanf(argv[1], "%lf", &a) == 1 &&
sscanf(argv[2], "%lf", &b) == 1 &&
(a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
!((argc == 5) &&
sscanf(argv[1], "%lf", &x_0) == 1 &&
(sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) &&
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]);
return -1;
}
f = f_lst[k];
t = clock();
ret = t4_solve(f, a, b, eps, m, &x, &it);
it = t2_solve(f, d_lst[k], x_0, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x));
do {
switch (ret)
{
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case EQUAL:
fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret);
break;
}
if (it >= m)
{
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret;
} while (0);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
return -2;
} else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0;
}
}

View file

@ -1,62 +1,33 @@
#include "solve.h"
#include "status.h"
#include <math.h>
#include <float.h>
status t4_solve (
int t7_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double x_0, double eps,
int m, double *x
) {
int it = 0;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
double y = f(x_0);
if (fabs(y_a) - eps < DBL_EPSILON)
if (fabs(y - x_0) - eps < DBL_EPSILON)
{
*x = a;
return SUCCESS;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return SUCCESS;
} if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON)
{
*x = a;
return EQUAL;
*x = x_0;
return 1;
}
for (it = 0; it < m; ++it)
{
c = b - ((b - a) / (y_b - y_a)) * y_b;
y = f(c);
x_0 = y;
y = f(x_0);
if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
if (fabs(y - x_0) - eps < DBL_EPSILON)
break;
} else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
{
a = c;
y_a = y;
} else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON)
{
b = c;
y_b = y;
} else
{
ret = EQUAL;
break;
}
}
if (it >= m)
ret = RUN_TIME;
*x = c;
*m_it = it;
*x = x_0;
return ret;
return it;
}

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
#include "status.h"
status t4_solve (
int t7_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x, int *m_it
double x_0, double eps,
int m, double *x
);
#endif

View file

@ -1,10 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
RUN_TIME,
EQUAL,
} status;
#endif

44
2025.05.02/dist/Ulyanov_MT/Makefile vendored Normal file
View file

@ -0,0 +1,44 @@
FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -Wmaybe-uninitialized -O3
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out a09.out
a01.out: a01.o functions.o comparison.o
gcc a01.o functions.o comparison.o -lm -o a01.out
a02.out: a02.o functions.o comparison.o
gcc a02.o functions.o comparison.o -lm -o a02.out
a03.out: a03.o functions.o comparison.o
gcc a03.o functions.o comparison.o -lm -o a03.out
a04.out: a04.o functions.o comparison.o
gcc a04.o functions.o comparison.o -lm -o a04.out
a05.out: a05.o functions.o comparison.o
gcc a05.o functions.o comparison.o -lm -o a05.out
a06.out: a06.o functions.o comparison.o
gcc a06.o functions.o comparison.o -lm -o a06.out
a07.out: a07.o functions.o comparison.o
gcc a07.o functions.o comparison.o -lm -o a07.out
a08.out: a08.o functions.o comparison.o
gcc a08.o functions.o comparison.o -lm -o a08.out
a09.out: a09.o functions.o comparison.o
gcc a09.o functions.o comparison.o -lm -o a09.out
functions.o:
gcc -c $(FLAGS) -o functions.o functions.c
comparison.o:
gcc -c $(FLAGS) -o comparison.o comparison.c
a01.o:
gcc -c $(FLAGS) -o a01.o task01.c
a02.o:
gcc -c $(FLAGS) -o a02.o task02.c
a03.o:
gcc -c $(FLAGS) -o a03.o task03.c
a04.o:
gcc -c $(FLAGS) -o a04.o task04.c
a05.o:
gcc -c $(FLAGS) -o a05.o task05.c
a06.o:
gcc -c $(FLAGS) -o a06.o task06.c
a07.o:
gcc -c $(FLAGS) -o a07.o task07.c
a08.o:
gcc -c $(FLAGS) -o a08.o task08.c
a09.o:
gcc -c $(FLAGS) -o a09.o task09.c
clean:
rm -f *.o *.out

View file

@ -0,0 +1,8 @@
#include <math.h>
#include "comparison.h"
int equal(double a, double b)
{
if (fabs(a - b) <= EPS * fmax(fabs(a), fabs(b))) return 1;
return 0;
}

View file

@ -0,0 +1,3 @@
#define EPS (1e-9)
int equal(double, double);

104
2025.05.02/dist/Ulyanov_MT/functions.c vendored Normal file
View file

@ -0,0 +1,104 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
static int count = 0;
double f0(double x)
{
count++;
(void) x;
return 1;
}
double f1(double x)
{
count++;
return x - 1e100;
}
double f2(double x)
{
count++;
return 4 - x * x;
}
double f3(double x)
{
count++;
return x * x * x + 3 * x * x + 16;
}
double f4(double x)
{
count++;
return 3 - 2 * x * x - x * x * x * x;
}
double f5(double x)
{
count++;
return sqrt(fabs(x) + 1) - 2;
}
double f6(double x)
{
count++;
return sqrt(sqrt(fabs(x) + 1) + 1) - 2;
}
double der_f0(double x)
{
(void) x;
return 0;
}
double der_f1(double x)
{
(void) x;
return 1;
}
double der_f2(double x)
{
return -2 * x;
}
double der_f3(double x)
{
return 3 * x * x + 6 * x;
}
double der_f4(double x)
{
return -4 * x - 4 * x * x * x;
}
double der_f5(double x)
{
if (x > 0) return 1 / (2 * sqrt(x + 1));
if (x < 0) return -1 / (2 * sqrt(-x + 1));
return 0;
}
double der_f6(double x)
{
if (x > 0)
{
double sqrtp = sqrt(x + 1);
return 1 / (4 * sqrt(x * sqrtp + sqrtp + x + 1));
}
if (x < 0)
{
double sqrtn = sqrt(-x + 1);
return -1 / (4 * sqrt(-x * sqrtn + sqrtn - x + 1));
}
return 0;
}
int get_count(void)
{
return count;
}

15
2025.05.02/dist/Ulyanov_MT/functions.h vendored Normal file
View file

@ -0,0 +1,15 @@
double f0(double);
double f1(double);
double f2(double);
double f3(double);
double f4(double);
double f5(double);
double f6(double);
double der_f0(double);
double der_f1(double);
double der_f2(double);
double der_f3(double);
double der_f4(double);
double der_f5(double);
double der_f6(double);
int get_count(void);

View file

@ -0,0 +1,7 @@
typedef enum io_status_
{
SUCCESS,
ERROR_OPEN,
ERROR_READ,
ERROR_FUNCTION,
} io_status;

77
2025.05.02/dist/Ulyanov_MT/task01.c vendored Normal file
View file

@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve1(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 1;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -1;
}
f = func[k];
t = clock();
it = solve1(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve1(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double fa = (*f)(a), fb = (*f)(b);
double c = 0, fc = 0;
if (fabs(fa) < eps) {*x = a; return 0;}
if (fabs(fb) < eps) {*x = b; return 0;}
for (it = 0; it < maxit; it++)
{
c = (a + b) / 2;
if ((c >= a) && (c <= a)) {*x = b; break;}
if ((c >= b) && (c <= b)) {*x = a; break;}
fc = (*f)(c);
if (fa * fc <= 0)
{
b = c;
fb = fc;
}
if (fb * fc <= 0)
{
a = c;
fa = fc;
}
if (fabs(b - a) < eps)
{
*x = a;
break;
}
}
if ((a >= b) && (a <= b))
{
*x = a;
return it;
}
if (it >= maxit) return -1;
return it;
}

60
2025.05.02/dist/Ulyanov_MT/task02.c vendored Normal file
View file

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve2(double, double, int, double*, double (*)(double), double (*)(double));
int main(int argc, char* argv[])
{
int task = 2;
double x0, eps;
int maxit, k;
double t;
double (*f)(double);
double (*der_f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double (*der_func[])(double) = {der_f0, der_f1, der_f2, der_f3, der_f4, der_f5, der_f6};
double x = 0, fx;
int it, c;
if (!(argc == 5 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%le", &eps) == 1 && sscanf(argv[3], "%d", &maxit) == 1 && sscanf(argv[4], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s x0 eps M k\n", argv[0]);
return -1;
}
f = func[k];
der_f = der_func[k];
t = clock();
it = solve2(x0, eps, maxit, &x, f, der_f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve2(double x0, double eps, int maxit, double* x, double (*f)(double), double (*der_f)(double))
{
int it;
double fx;
double der;
for (it = 0; it < maxit; it++)
{
fx = (*f)(x0);
if (fabs(fx) < eps) break;
der = der_f(x0);
if (!(equal(der, 0))) x0 = x0 - fx / der;
else return -1;
}
if (it >= maxit) return -1;
*x = x0;
return it;
}

60
2025.05.02/dist/Ulyanov_MT/task03.c vendored Normal file
View file

@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve3(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 3;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -2;
}
f = func[k];
t = clock();
it = solve3(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve3(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double c, fc;
double fa = (*f)(a), fb = (*f)(b);
if (fa * fb > 0) return -1;
if (fabs(fa) < eps) {*x = a; return 0;}
if (fabs(fb) < eps) {*x = b; return 0;}
for (it = 0; it < maxit; it++)
{
if (!equal(fa, fb)) c = a - ((a - b) / (fa - fb)) * fa;
else return -1;
fc = (*f)(c);
if (fabs(fc) < eps) {*x = c; break;}
if (fc * fa > 0) {a = c; fa = fc;}
if (fc * fb > 0) {b = c; fb = fc;}
}
if (it >= maxit) return -1;
return it;
}

59
2025.05.02/dist/Ulyanov_MT/task04.c vendored Normal file
View file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve4(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 4;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -1;
}
f = func[k];
t = clock();
it = solve4(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve4(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double c, fc;
double fa = (*f)(a), fb = (*f)(b);
if (fabs(fa) < eps) {*x = a; return 0;}
if (fabs(fb) < eps) {*x = b; return 0;}
for (it = 0; it < maxit; it++)
{
if (!equal(fa, fb)) c = a - ((a - b) / (fa - fb)) * fa;
else return -1;
fc = (*f)(c);
if (fabs(fc) < eps) {*x = c; break;}
if (fabs(fa) < fabs(fb)) {b = c; fb = fc;}
else {a = c; fa = fc;}
}
if (it >= maxit) return -1;
return it;
}

66
2025.05.02/dist/Ulyanov_MT/task05.c vendored Normal file
View file

@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve5(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 5;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -2;
}
f = func[k];
t = clock();
it = solve5(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve5(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double c = (a + b) / 2, xi, fxi;
double fa = (*f)(a), fb = (*f)(b), fc = (*f)(c);
double der_ac, der_cb, der_acb;
if (fabs(fa) < eps) {*x = a; return 0;}
if (fabs(fb) < eps) {*x = b; return 0;}
for (it = 0; it < maxit; it++)
{
if (!equal(fa, fc)) der_ac = (a - c) / (fa - fc);
else return -1;
if (!equal(fc, fb)) der_cb = (c - b) / (fc - fb);
else return -1;
if (!equal(fa, fb)) der_acb = (der_cb - der_ac) / (fb - fa);
else return -1;
xi = a - fa * der_ac + fa * fc * der_acb;
fxi = (*f)(xi);
if (fabs(fxi) < eps) {*x = xi; break;}
if ((fabs(fa) >= fabs(fc)) && (fabs(fa) >= fabs(fb))) {a = xi; fa = fxi;}
else if ((fabs(fc) >= fabs(fa)) && (fabs(fc) >= fabs(fb))) {c = xi; fc = fxi;}
else {b = xi; fb = fxi;}
}
if (it >= maxit) return -1;
return it;
}

124
2025.05.02/dist/Ulyanov_MT/task06.c vendored Normal file
View file

@ -0,0 +1,124 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve6(int, double, double, double, int, double*, double*, double*, double*, double (*)(double));
io_status build(double*, double*, int);
double comp(double*, double*, int);
void swap_max(double, double, double*, double*, double*, int);
int main(int argc, char* argv[])
{
int task = 6;
double a, b, eps;
int maxit, k, m;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
double* z;
double* y;
double* s;
int it, c;
if (!(argc == 7 && sscanf(argv[1], "%d", &m) == 1 && sscanf(argv[2], "%lf", &a) == 1 && sscanf(argv[3], "%lf", &b) == 1 && sscanf(argv[4], "%le", &eps) == 1 && sscanf(argv[5], "%d", &maxit) == 1 && sscanf(argv[6], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s m a b eps M k\n", argv[0]);
return -2;
}
z = (double*) malloc((m + 1) * sizeof(double));
y = (double*) malloc((m + 1) * sizeof(double));
s = (double*) malloc((m + 1) * sizeof(double));
if (!z || !y || !s)
{
if (z) free(z);
if (y) free(y);
if (s) free(s);
}
f = func[k];
t = clock();
it = solve6(m, a, b, eps, maxit, &x, z, y, s, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
free(z);
free(y);
free(s);
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve6(int m, double a, double b, double eps, int maxit, double* x, double* z, double* y, double* s, double (*f)(double))
{
int i;
int it;
double tmp, xi, fxi;
io_status res;
for (i = 0; i <= m; i++)
{
tmp = a + (b - a) * i / m;
z[i] = tmp;
y[i] = (*f)(tmp);
s[i] = tmp;
}
for (it = 0; it < maxit; it++)
{
res = build(z, y, m);
if (res != SUCCESS) return -1;
xi = comp(z, y, m);
fxi = (*f)(xi);
if (fabs(fxi) < eps * fabs(xi)) {*x = xi; break;}
swap_max(xi, fxi, z, y, s, m);
}
if (it >= maxit) return -1;
return it;
}
io_status build(double* z, double* y, int m)
{
int i, j;
double el_i, el_prev;
for (j = 0; j <= m; j++)
{
for (i = m; i > j; i--)
{
el_i = y[i];
el_prev = y[i - j - 1];
if (equal(el_i, el_prev)) return ERROR_FUNCTION;
z[i] = (z[i] - z[i - 1]) / (el_i - el_prev);
}
}
return SUCCESS;
}
double comp(double* z, double* y, int m)
{
int i;
double sum = 0;
for (i = m; i >= 0; i--) sum = z[i] - y[i] * sum;
return sum;
}
void swap_max(double xi, double fxi, double* z, double* y, double* s, int m)
{
int i, maxind = 0;
double max = fabs(y[0]), mod_yi;
for (i = 0; i <= m; i++)
{
z[i] = s[i];
mod_yi = fabs(y[i]);
if (mod_yi > max) {max = mod_yi; maxind = i;}
}
z[maxind] = xi;
y[maxind] = fxi;
s[maxind] = xi;
}

54
2025.05.02/dist/Ulyanov_MT/task07.c vendored Normal file
View file

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve7(double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 7;
double x0, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 5 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%le", &eps) == 1 && sscanf(argv[3], "%d", &maxit) == 1 && sscanf(argv[4], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s x0 eps M k\n", argv[0]);
return -1;
}
f = func[k];
t = clock();
it = solve7(x0, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve7(double x0, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double fx;
for (it = 0; it < maxit; it++)
{
fx = (*f)(x0);
if (fabs(x0 - fx) < eps) break;
x0 = fx;
}
if (it >= maxit) return -1;
*x = x0;
return it;
}

58
2025.05.02/dist/Ulyanov_MT/task08.c vendored Normal file
View file

@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve8(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 8;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -2;
}
f = func[k];
t = clock();
it = solve8(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve8(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double h = 0.1;
double xi = a, fxi = (*f)(a), fx;
for (it = 0; it < maxit; it++)
{
xi += (b - a) * h;
fx = (*f)(xi);
if (fx < fxi) h *= -0.1;
else if (equal(xi, a)) {*x = xi; break;}
else if (equal(xi, b)) {*x = xi; break;}
if (fabs(h) < eps) {*x = xi; break;}
fxi = fx;
}
if (it >= maxit) return -1;
return it;
}

65
2025.05.02/dist/Ulyanov_MT/task09.c vendored Normal file
View file

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "io_status.h"
#include "functions.h"
#include "comparison.h"
int solve9(double, double, double, int, double*, double (*)(double));
int main(int argc, char* argv[])
{
int task = 9;
double a, b, eps;
int maxit, k;
double t;
double (*f)(double);
double (*func[])(double) = {f0, f1, f2, f3, f4, f5, f6};
double x = 0, fx;
int it, c;
if (!(argc == 6 && sscanf(argv[1], "%lf", &a) == 1 && sscanf(argv[2], "%lf", &b) == 1 && sscanf(argv[3], "%le", &eps) == 1 && sscanf(argv[4], "%d", &maxit) == 1 && sscanf(argv[5], "%d", &k) == 1 && k >= 0 && k <= 6))
{
printf("Usage: %s a b eps M k\n", argv[0]);
return -2;
}
f = func[k];
t = clock();
it = solve9(a, b, eps, maxit, &x, f);
t = (clock() - t) / CLOCKS_PER_SEC;
c = get_count();
if (it < 0)
{
printf("%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, c, t);
return -1;
}
fx = (*f)(x);
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, fx, it, c, t);
return 0;
}
int solve9(double a, double b, double eps, int maxit, double* x, double (*f)(double))
{
int it;
double phi = 2 / (1 + sqrt(5));
double x1 = b - (b - a) * phi, x2 = a + (b - a) * phi;
double fx1 = (*f)(x1), fx2 = (*f)(x2);
for (it = 0; it < maxit; it++)
{
if (fx1 < fx2)
{
a = x1;
x1 = x2; fx1 = fx2;
x2 = a + (b - a) * phi; fx2 = (*f)(x2);
}
else
{
b = x2;
x2 = x1; fx2 = fx1;
x1 = b - (b - a) * phi; fx1 = (*f)(x1);
}
if (fabs(a - b) < eps) {*x = x1; break;}
}
if (it >= maxit) return -1;
return it;
}