Add Matvej's solve
This commit is contained in:
parent
c5ae5840ff
commit
3e90cc7ebb
22 changed files with 916 additions and 105 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef STATUS_H
|
||||
#define STATUS_H
|
||||
|
||||
typedef enum _status {
|
||||
SUCCESS,
|
||||
RUN_TIME,
|
||||
EQUAL,
|
||||
} status;
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue