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 <math.h>
#include "init_f.h" #include "init_f.h"
#include "status.h"
#include "solve.h" #include "solve.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
double t, a, b, eps, x = 0; double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 8; int m, k, it, cl, task = 8;
status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin 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)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1; return -1;
} }
f = f_lst[k]; f = f_lst[k];
t = clock(); 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; t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count(); cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x)); if (it >= m)
{
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;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret; return -2;
} while (0); } 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); 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 0; }
} }

View file

@ -1,8 +1,8 @@
#include "solve.h" #include "solve.h"
#include "status.h"
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <stdbool.h>
int t8_solve ( int t8_solve (
@ -11,54 +11,46 @@ int t8_solve (
double eps, int m, double *x double eps, int m, double *x
) { ) {
int it = 0; int it = 0;
status ret = SUCCESS; double x_l = 0, y_l = 0, c = a, y = f(a);
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (fabs(y_a) - eps < DBL_EPSILON) if (fabs(b - a) < DBL_EPSILON)
{ {
*x = a; *x = a;
return SUCCESS; return 1;
} 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;
} }
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; do {
c = a + h; if (it >= m)
y = f(c); break;
it++;
x_l = c;
y_l = y;
c += h;
y = f(c);
} while (((y - y_l) - eps) > DBL_EPSILON);
if (fabs(y) - eps < DBL_EPSILON) if (it >= m)
{
ret = SUCCESS;
break; break;
} else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
if ((c - b) > DBL_EPSILON)
{ {
a = c; x_l = b;
y_a = y; y_l = f(b);
} else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON) break;
} else if ((a - c) > DBL_EPSILON)
{ {
b = c; x_l = a;
y_b = y; y_l = f(b);
} else
{
ret = EQUAL;
break; break;
} }
} }
if (it >= m) *x = x_l;
ret = RUN_TIME;
*x = c;
*m_it = it;
return ret; return it;
} }

View file

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