Task 8 is done

This commit is contained in:
AZEN-SGG 2025-05-01 22:49:14 +03:00
parent 7144337f0b
commit c5ae5840ff
3 changed files with 40 additions and 65 deletions

View file

@ -3,15 +3,13 @@
#include <math.h>
#include "init_f.h"
#include "status.h"
#include "solve.h"
/* ./a.out a b eps M k */
int main(int argc, char *argv[])
{
double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 8;
status ret;
int m, k, it, cl, task = 8;
double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin
@ -27,38 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1;
return -1;
}
f = f_lst[k];
t = clock();
ret = t8_solve(f, a, b, eps, m, &x, &it);
it = t8_solve(f, a, b, 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)
if (it >= m)
{
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;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret;
} while (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,8 +1,8 @@
#include "solve.h"
#include "status.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
int t8_solve (
@ -11,54 +11,46 @@ int t8_solve (
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 x_l = 0, y_l = 0, c = a, y = f(a);
if (fabs(y_a) - eps < DBL_EPSILON)
if (fabs(b - a) < 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;
return 1;
}
for (it = 0; it < m; ++it)
for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1)
{
double h = (b - a) * 0.1;
c = a + h;
do {
if (it >= m)
break;
it++;
x_l = c;
y_l = y;
c += h;
y = f(c);
if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
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;
}
}
} while (((y - y_l) - eps) > DBL_EPSILON);
if (it >= m)
ret = RUN_TIME;
break;
*x = c;
*m_it = it;
return ret;
if ((c - b) > DBL_EPSILON)
{
x_l = b;
y_l = f(b);
break;
} else if ((a - c) > DBL_EPSILON)
{
x_l = a;
y_l = f(b);
break;
}
}
*x = x_l;
return it;
}

View file

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