Last active
January 2, 2016 21:49
-
-
Save w495/8365863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
typedef float real_t; | |
typedef real_t (*function_t) (real_t); | |
const real_t e = 0.000001; | |
/** | |
* @fn Возвращает значение целевой функции \sqrt(1 - x) - \tan(x) | |
*/ | |
real_t target_5(real_t x) { | |
if ((1 - x) >= 0) | |
return sqrt(1 - x) - tan(x); | |
} | |
/** | |
* @fn Возвращает значение целевой функции x + cos(pow(x, 0.52) + 2) | |
*/ | |
real_t target_6(real_t x) { | |
return x + cos(pow(x, 0.52) + 2); | |
} | |
/** | |
* Можно пробовать 1 - tan(x)*tan(x), | |
* но в такой формулировке не выполнено достаточное условие сходимости. | |
* |f(x)'| = |-2 * tan(x) /(cos(x) * cos(x))| < 1, x ∈ [a, b] = [0, 1] | |
* Т.к | |
* |f(0.5)'| = |-2 * tan(0.5) /(cos(0.5) * cos(0.5))| = 1.41 > 1 | |
* | |
* Если выражать функцию через atan(sqrt(1 - x)), то все получатся. | |
* Условие сходимости выполнено: | |
* |f(x)'| = |1/(2 * sqrt(1-x) * (x-2))| < 1, x ∈ [0, 1] | |
* | |
*/ | |
real_t target_5_xfx(real_t x) { | |
return atan(sqrt(1 - x)); | |
} | |
/** | |
* Нужно проверять условие сходимости, для метода итераций и для этой функции. | |
* Вообще везде не плохо бы проверять условие сходимости. | |
*/ | |
real_t target_6_xfx(real_t x) { | |
return -cos(pow(x, 0.52) + 2); | |
} | |
real_t target_5_derivative(real_t x) { | |
if ((1 - x) >= 0) | |
return (-1 / (2 * sqrt(1 - x))) - (1 / (pow(cos(x), 2))); | |
} | |
real_t target_6_derivative(real_t x) { | |
return 1 - 0.52 * pow(x, - 0.48) * sin(pow(x, 0.52) + 2); | |
} | |
real_t dich(function_t function, real_t a0, real_t b0) { | |
real_t a, b; | |
a = a0; | |
b = b0; | |
while (fabs(a - b) > e) { | |
if (function(a) * function((a + b) / 2) > 0) | |
a = (a + b) / 2; | |
if (function(b) * function((a + b) / 2) > 0) | |
b = (a + b) / 2; | |
} | |
return (a + b) / 2; | |
} | |
real_t iter(function_t function, real_t a0, real_t b0) { | |
real_t a, b, x, x0; | |
a = a0; | |
b = b0; | |
x0 = (a + b) / 2; | |
x = function(x0); | |
while (fabs(x - x0) >= e) { | |
x0 = x; | |
x = function(x0); | |
} | |
return x; | |
} | |
real_t netw(function_t function, function_t derivative, real_t a0, real_t b0) { | |
real_t a, b, x, x0; | |
a = a0; | |
b = b0; | |
x0 = (a + b) / 2; | |
x = x0 - (function(x0) / derivative(x0)); | |
while (fabs(x - x0) >= e) { | |
x0 = x; | |
x = x0 - (function(x0) / derivative(x0)); | |
} | |
return x; | |
} | |
int main() { | |
printf( | |
"dich(target_5) = %f\n", | |
dich(target_5, 0.0, 1.0) | |
); | |
printf( | |
"iter(target_5) = %f\n", | |
iter(target_5_xfx, 0.0, 1.0) | |
); | |
printf( | |
"netw(target_5) = %f\n", | |
netw(target_5, target_5_derivative, 0.0, 1.0) | |
); | |
printf( | |
"dich(target_6) = %f\n", | |
dich(target_6, 0.5, 1.0) | |
); | |
printf( | |
"iter(target_6) = %f\n", | |
iter(target_6_xfx, 0.5, 1.0) | |
); | |
printf( | |
"netw(target_6) = %f\n", | |
netw(target_6, target_6_derivative, 0.5, 1.0) | |
); | |
return (0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment