Task 9 is done

This commit is contained in:
AZEN-SGG 2025-05-02 05:09:42 +03:00
parent a8b4e4358d
commit cdc405551d
11 changed files with 328 additions and 1 deletions

View file

@ -293,3 +293,49 @@ int t8_solve (
return it;
}
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
const double gdrt = 2 / (1 + sqrt(5));
int it = 0;
double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
double y_1 = f(x_1), y_2 = f(x_2);
for (it = 1; it <= m; ++it)
{
if (y_1 - y_2 > DBL_EPSILON)
{
b = x_2;
x_2 = x_1;
y_2 = y_1;
x_1 = b + (a - b) * gdrt;
y_1 = f(x_1);
} else
{
a = x_1;
x_1 = x_2;
y_1 = y_2;
x_2 = a + (b - a) * gdrt;
y_2 = f(x_2);
}
if (fabs(b - a) < eps)
{
*x = x_1;
break;
}
}
if (it > m)
it = -1;
return it;
}