Started task 6
This commit is contained in:
parent
47a464aa9e
commit
af90d91e29
9 changed files with 225 additions and 102 deletions
|
|
@ -1,103 +1,57 @@
|
|||
#include "solve.h"
|
||||
#include "comp.h"
|
||||
#include "polynom.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
int t1_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
double eps, int m, double *x
|
||||
) {
|
||||
int it = 0;
|
||||
uint64_t bits;
|
||||
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
|
||||
bool sgn_a, sgn_b, sgn_c;
|
||||
|
||||
memcpy(&bits, &y_a, sizeof(bits));
|
||||
sgn_a = (bits >> 63) & 1;
|
||||
memcpy(&bits, &y_b, sizeof(bits));
|
||||
sgn_b = (bits >> 63) & 1;
|
||||
|
||||
if (fabs(y_a) - eps < DBL_EPSILON)
|
||||
{
|
||||
*x = a;
|
||||
return 1;
|
||||
} if (fabs(y_b) - eps < DBL_EPSILON)
|
||||
{
|
||||
*x = b;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sgn_a == sgn_b)
|
||||
{
|
||||
*x = DBL_MAX;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (it = 1; it <= m; ++it)
|
||||
{
|
||||
c = (a + b) * 0.5;
|
||||
y = f(c);
|
||||
|
||||
memcpy(&bits, &y, sizeof(bits));
|
||||
sgn_c = (bits >> 63) & 1;
|
||||
|
||||
if (fabs(y) - eps < DBL_EPSILON)
|
||||
break;
|
||||
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
|
||||
it = m+1;
|
||||
else if (sgn_c == sgn_a)
|
||||
{
|
||||
a = c;
|
||||
y_a = y;
|
||||
} else if (sgn_c == sgn_b)
|
||||
{
|
||||
b = c;
|
||||
y_b = y;
|
||||
}
|
||||
}
|
||||
|
||||
if (it > m)
|
||||
it = -1;
|
||||
|
||||
*x = c;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int t6_solve (
|
||||
double (*f) (double),
|
||||
const int m, double *d
|
||||
const int m, double *d,
|
||||
double a, double b,
|
||||
const double eps, const int M, double *x
|
||||
const double eps, const int M, double *res
|
||||
) {
|
||||
const int len = m + 1;
|
||||
double *maximum = d;
|
||||
|
||||
const int len = m + 1;
|
||||
|
||||
double *y_lst = d;
|
||||
double *x_lst = d + len;
|
||||
double *t_lst = d + (len << 1);
|
||||
|
||||
double last_x = a;
|
||||
double last_y = f(a);
|
||||
const int h = (b - a) / m;
|
||||
|
||||
y_lst[0] = last_y;
|
||||
x_lst[0] = last_x;
|
||||
t_lst[0] = last_x;
|
||||
|
||||
for (int i = 1, double t_x = a ; i < len ; i++, t_x += h)
|
||||
int it;
|
||||
for (it = 1; it <= M; ++it)
|
||||
{
|
||||
double t_y = f(t_x);
|
||||
double x = construct_poly(0, len, y_lst, x_lst);
|
||||
double y = f(x);
|
||||
|
||||
y_lst[i] = t_y;
|
||||
x_lst[i] = t_x;
|
||||
if (is_eps(y, eps))
|
||||
{
|
||||
*res = x;
|
||||
return it;
|
||||
}
|
||||
|
||||
|
||||
// Можно возвращение значений функции в y_lst можно было встроить в суммирование полинома, но мало толку
|
||||
for (int i = 0; i < len; ++i)
|
||||
{
|
||||
double yi = t_lst[i];
|
||||
y_lst[i] = yi;
|
||||
|
||||
if (is_equal(y, yi))
|
||||
return -1;
|
||||
|
||||
if ((fabs(yi) - fabs(*maximum)) > DBL_EPSILON)
|
||||
maximum = &yi;
|
||||
}
|
||||
|
||||
*maximum = y;
|
||||
*(maximum + len) = x;
|
||||
*(maximum + (len << 1)) = y;
|
||||
}
|
||||
|
||||
(void)a;
|
||||
(void)b;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue