Task 8 is done
This commit is contained in:
parent
7144337f0b
commit
c5ae5840ff
3 changed files with 40 additions and 65 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
break;
|
||||||
|
|
||||||
|
it++;
|
||||||
|
x_l = c;
|
||||||
|
y_l = y;
|
||||||
|
|
||||||
|
c += h;
|
||||||
y = f(c);
|
y = f(c);
|
||||||
|
} while (((y - y_l) - eps) > DBL_EPSILON);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (it >= m)
|
if (it >= m)
|
||||||
ret = RUN_TIME;
|
break;
|
||||||
|
|
||||||
*x = c;
|
if ((c - b) > DBL_EPSILON)
|
||||||
*m_it = it;
|
{
|
||||||
|
x_l = b;
|
||||||
return ret;
|
y_l = f(b);
|
||||||
|
break;
|
||||||
|
} else if ((a - c) > DBL_EPSILON)
|
||||||
|
{
|
||||||
|
x_l = a;
|
||||||
|
y_l = f(b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*x = x_l;
|
||||||
|
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue