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

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;
}