Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <complex>
- #include<conio.h>
- #include <vector>
- #include <iomanip>
- using namespace std;
- const int n = 5;
- complex<double> k[n] = {
- complex<double>(0, 1),
- complex<double>(15, 0),
- complex<double>(10, 0),
- complex<double>(12, 0),
- complex<double>(10, 0),
- };
- double
- coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
- int k);
- complex<double> gorner1f(double x, double y, complex<double> koef[]);
- double gornerf(double x, double y, complex<double> koef[]);
- void gornerd(double x, double y, complex<double> *a);
- //временные корни
- vector<double> x1t;
- vector<double> x2t;
- //нормальные корни
- vector<double> x1c;
- vector<double> x2c;
- //счетчик функций
- vector<double> fc;
- int fco = 0;
- int main() {
- double eps = 0.01, Eps = 0.0000001;
- complex<double> temp[n];
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- cout << "COORD DESCENT: " << endl;
- cout << "Finding root 1 :" << endl;
- coordDescent(0, 0, Eps, 1, 1, temp, 1);
- gornerd(x1c[0], x2c[0], temp);
- cout << endl;
- cout << "fc: " << fco << endl;
- fc.push_back(fco);
- cout << endl;
- cout << endl;
- for (int i = 1; i < n - 2; i++) {
- eps *= 2;
- fco = 0;
- cout << "Finding root " << i + 1 << ": " << endl;
- coordDescent(0, 0, eps, 1, 1, temp, 2);
- coordDescent(x1t[i - 1], x2t[i - 1], Eps, 1, 1, k, 1);
- gornerd(x1c[i], x2c[i], temp);
- cout << endl;
- cout << "fc: " << fco << endl;
- fc.push_back(fco);
- cout << endl;
- cout << endl;
- }
- fco = 0;
- cout << "Finding root 4 :" << endl;
- x1t.push_back((-temp[0] / temp[1]).real());
- x2t.push_back((-temp[0] / temp[1]).imag());
- coordDescent(x1t[2], x2t[2], Eps, 1, 1, k, 1);
- cout << endl;
- cout << "fc: " << fco << endl;
- fc.push_back(fco);
- cout << endl;
- cout << endl;
- cout << "fcall: " << fc[0] + fc[1] + fc[2] + fc[3] << endl;
- return 0;
- }
- // считает значение функции в точке
- double gornerf(double x, double y, complex<double> koef[]) {
- return pow(gorner1f(x, y, koef).real(), 2) + pow(gorner1f(x, y, koef).imag(), 2);
- }
- // считает значение исходного полинома в точке
- complex<double> gorner1f(double x, double y, complex<double> koef[]) {
- complex<double> z(x, y);
- complex<double> s = koef[n - 1];
- for (int i = 1; i <= n - 1; ++i) {
- s *= z;
- s += koef[(n - 1) - i];
- }
- return s;
- }
- // деление полинома на бином
- void gornerd(double x, double y, complex<double> *a) {
- complex<double> c(x, y);
- complex<double> b[n];
- b[n - 1] = 0;
- for (int i = n - 2; i >= 0; i--) {
- b[i] = a[i + 1] + c * b[i + 1];
- }
- for (int j = 0; j < n; j++) {
- a[j] = b[j];
- }
- }
- // координатный спуск
- double
- coordDescent(double x01, double x02, const double E, const double alp01, const double alp02, complex<double> *koef,
- int k) {
- double x1 = x01;
- double x2 = x02;
- double alp1 = alp01;
- double alp2 = alp02;
- int count2 = 0;
- int count1 = 0;
- int n = 2;
- double xu = 0, xn = 1;
- double f1, f2, f;
- f = (gornerf(x1, x2, koef));
- fco++;
- double eps = 0.00000001;
- cout << "x1 " << x1 << " x2 " << x2 << " f " << f << " a1 " << alp1 << " a2 " << alp2 << endl;
- while (abs(f) > E) {
- alp1 = abs(alp1);
- alp2 = abs(alp2);
- f1 = gornerf(x1 + eps, x2, koef);
- f2 = gornerf(x1 - eps, x2, koef);
- fco = fco + 2;
- cout << endl;
- cout << setprecision(20) << "x1 " << x1 - eps << " x2 " << x2 << " f " << f2 << " a1 " << alp1 << " a2 " << alp2
- << endl;
- cout << setprecision(20) << "x1 " << x1 + eps << " x2 " << x2 << " f " << f1 << " a1 " << alp1 << " a2 " << alp2
- << endl;
- if (f1 <= f2) {
- x1 = x1 + eps;
- f = f1;
- cout << "Xplus" << endl;
- } else {
- x1 = x1 - eps;
- f = f2;
- cout << "Xminus" << endl;
- alp1 *= -1;
- }
- count1 = 0;
- count2 = 0;
- while (count1 < n && count2 < 1) {
- f1 = gornerf(x1 + alp1, x2, koef);
- fco++;
- while (f1 < f) {
- x1 = x1 + alp1;
- cout << "OkX" << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1 << " a2 "
- << alp2 << endl;
- count2 += 1;
- f = f1;
- if (count2 > 1)cout << "Second step with this ax" << endl;
- f1 = gornerf(x1 + alp1, x2, koef);
- }
- if (f1 > f) {
- alp1 *= 0.5;
- cout << "NeokX " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1
- << " a2 " << alp2 << endl;
- count1++;
- }
- }
- count1 = 0;
- count2 = 0;
- f1 = gornerf(x1, x2 + eps, koef);
- f2 = gornerf(x1, x2 - eps, koef);
- fco = fco + 2;
- cout << endl;
- cout << setprecision(20) << "x1 " << x1 << " x2 " << x2 - eps << " f " << f2 << " a1 " << alp1 << " a2 " << alp2
- << endl;
- cout << setprecision(20) << "x1 " << x1 << " x2 " << x2 + eps << " f " << f1 << " a1 " << alp1 << " a2 " << alp2
- << endl;
- if (f1 <= f2) {
- x2 = x2 + eps;
- f = f1;
- cout << "Yplus" << endl;
- } else {
- x2 = x2 - eps;
- f = f2;
- cout << "Yminus" << endl;
- alp2 *= -1;
- }
- while (count2 < n && count1 < 1) {
- f1 = gornerf(x1, x2 + alp2, koef);
- fco++;
- while (f1 < f) {
- x2 = x2 + alp2;
- cout << "OkY " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1 << " a2 "
- << alp2 << endl;
- count2 += 1;
- f = f1;
- if (count2 > 1)cout << "Second step with this ay" << endl;
- f1 = gornerf(x1, x2 + alp2, koef);
- }
- if (f1 > f) {
- alp2 *= 0.5;
- cout << "NeokY " << "x1 " << x1 << " x2 " << x2 << " f " << f << " f1 " << f1 << " a1 " << alp1
- << " a2 " << alp2 << endl;
- count2++;
- }
- }
- }
- if (k == 1) {
- cout << endl;
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1c.push_back(x1);
- x2c.push_back(x2);
- }
- if (k == 2) {
- cout << endl;
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1t.push_back(x1);
- x2t.push_back(x2);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement