Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <math.h>
- using namespace std;
- double xx0, xx1; // промежуток в ответе шагового метода
- double a = 1, b = 3, h = 0.1, e = 0.001; // исходные данные
- double function(double x)
- {
- return (3 * pow(log(x), 2) + 6 * log(x) - 5); // Сюда короч нашу исходную функцию из варианта своего вбиваем.
- }
- double derivative(double x)
- {
- return ((6 * (log(x) + 1)) / x); // Сюда короч производную от этой исходной функции.
- }
- double S(double x)
- {
- return x - 0.005 * function(x); // Эт я сам не понял(lol), но короче, это типо по такой схеме - g(x) = x + b*f(x), b - константа.
- }
- void step_method() // Шаговый метод.
- {
- double x0 = a, x1 = x0 + h;
- while (x1 <= b)
- {
- x1 = x0 + h;
- if (function(x0) * function(x1) < 0)
- {
- cout << "Step method:\n";
- cout << "x on int. [" << x0 << "," << x1 << "]\n" << endl;
- xx0 = x0;
- xx1 = x1;
- }
- x0 = x1;
- }
- }
- void bisection_method() // Метод половинного деления.
- {
- double x = xx0;
- a = xx0;
- b = xx1;
- while (fabs(function(x)) > e)
- {
- x = (a + b) / 2;
- if (function(a) * function(x) < 0)
- b = x;
- else a = x;
- }
- cout << "Bisection method:\nx= " << x << "\n" << endl;
- }
- void newton_method() // Метод Ньютона.
- {
- double x2 = xx0;
- while (fabs(function(x2)) > e)
- {
- x2 = x2 - function(x2) / derivative(x2);
- }
- cout << "Newton's method:\nx= " << x2 << "\n" << endl;
- }
- void fixed_point_iteration() // Метод простой итерации.
- {
- double x3 = xx0;
- while (fabs(function(x3)) > e)
- {
- x3 = S(x3);
- }
- cout << "Fixed-point iteration method:\nx= " << x3 << "\n" << endl;
- }
- int main()
- {
- setlocale(LC_ALL,"");
- step_method();
- bisection_method();
- newton_method();
- fixed_point_iteration();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement