Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nikirova Yuliya Group 205
- #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>(1, 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);
- double gornerf(double x, double y, complex<double> koef[]);
- void gornerd(double x, double y, complex<double> *a);
- vector<double> gradient(double x, double y, complex<double> *koef);
- double ConstGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k);
- double InAdvGrad(double x01, double x02, const double E, complex<double> *koef, int m);
- double DivGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k);
- double goldsec(double E, double a0, double b0, double x1, double x2, double alpha, complex<double> *koef);
- double funcForOneDimMinimize(double x1, double x2, double alpha, complex<double> *koef);
- double AccelGrad(double x01, double x02, const double E, complex<double> *koef, int k);
- complex<double> gorner1f(double x, double y, complex<double> koef[]);
- //Корни для итоговой таблицы для каждого метода
- vector<double> xk1;
- vector<double> yk1;
- vector<double> fk1;
- vector<double> xk2;
- vector<double> yk2;
- vector<double> fk2;
- vector<double> xk3;
- vector<double> yk3;
- vector<double> fk3;
- vector<double> xk4;
- vector<double> yk4;
- vector<double> fk4;
- vector<double> xk5;
- vector<double> yk5;
- vector<double> fk5;
- // Вектор точных корней
- vector<double> x1n;
- vector<double> x2n;
- vector<double> fn;
- // Вектор приблизительных корней
- vector<double> x1s;
- vector<double> x2s;
- vector<double> fs;
- // Вектор подсчетов иттераций, вызовов функции, вызовов градиента пометодно
- vector<double> ittv;
- vector<double> fcv;
- vector<double> gcv;
- // Переменная подсчетов иттераций, вызовов функции, вызовов градиента
- int itt = 1;
- int fc = 1;
- int gc = 1;
- int ff1=0;
- int ff2=0;
- int ff=0;
- int main() {
- const double E = 0.0001; //для точных поисков
- double eps = 0.0001; //для неточных поисков
- double x1 = 0;
- double x2 = 0;
- 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(x1, x2, E, 1, 1, temp, 1);
- cout << endl;
- cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
- << fn[0] << endl;
- ff = ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- cout << endl;
- xk1.push_back(x1n[0]);
- yk1.push_back(x2n[0]);
- fk1.push_back(fn[0]);
- gornerd(x1n[0], x2n[0], temp);
- for (int i = 1; i < n - 2; i++) {
- cout << "Finding root " << i + 1 << endl;
- coordDescent(x1, x2, eps, 1, 1, temp, 2);
- eps *= 2;
- coordDescent(x1s[i - 1], x2s[i - 1], E, 0.002, 0.002, k, 1);
- cout << endl;
- cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
- << fn[i] << endl;
- cout<<"f: "<<ff1+ff2<<endl;
- ff = ff+ff1+ff2;
- ff1 = 0;
- ff2 = 0;
- cout << endl;
- gornerd(x1n[i], x2n[i], temp);
- xk1.push_back(x1n[i]);
- yk1.push_back(x2n[i]);
- fk1.push_back(fn[i]);
- }
- x1s.push_back((-temp[0] / temp[1]).real());
- x2s.push_back((-temp[0] / temp[1]).imag());
- fs.push_back(0);
- cout << "Finding root " << n-1 << endl;
- cout << "Aprox root: " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
- << x2s[n - 3] << " " << "f:"
- << fs[n - 3] << endl;
- coordDescent(x1s[n - 3], x2s[n - 3], E/10, 0.002, 0.002, k, 1);
- cout << endl;
- cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
- << fn[n-2] << endl;
- cout<<"f: "<<ff1+ff2<<endl;
- ff = ff+ff1+ff2;
- ff1 = 0;
- ff2 = 0;
- xk1.push_back(x1n[n - 2]);
- yk1.push_back(x2n[n - 2]);
- fk1.push_back(fn[n - 2]);
- cout << endl;
- //cout << "Count of itterations: " << itt << endl;
- cout << "Count of fcalc: " << ff << endl;
- cout << endl;
- // Обнуляем показатели для следующего метода
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- ittv.push_back(itt);
- fcv.push_back(ff);
- ff = 0;
- ff1 = 0;
- ff2 = 0;
- gcv.push_back(0);
- x1n.clear();
- x2n.clear();
- fn.clear();
- itt = 1;
- fc = 1;
- gc = 1;
- eps = 0.01;
- // Градиентный метод с постоянным шагом
- cout << "CONST GRAD METHOD:" << endl;
- cout << "Finding root " << 1 << endl;
- ConstGrad(0, 0, E, 0.000428, temp, 1);
- cout << endl;
- cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
- << fn[0] << endl;
- ff = ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- gornerd(x1n[0], x2n[0], temp);
- xk2.push_back(x1n[0]);
- yk2.push_back(x2n[0]);
- fk2.push_back(fn[0]);
- for (int i = 1; i < n - 2; i++) {
- cout << endl;
- cout << "Finding root " << i + 1 << endl;
- ConstGrad(0, 0, eps, 0.000428, temp, 2);
- eps *= 2;
- ConstGrad(x1s[i - 1], x2s[i - 1], E, 0.00009, k, 1);
- cout << endl;
- cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
- << fn[i] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- gornerd(x1n[i], x2n[i], temp);
- xk2.push_back(x1n[i]);
- yk2.push_back(x2n[i]);
- fk2.push_back(fn[i]);
- }
- x1s.push_back((-temp[0] / temp[1]).real());
- x2s.push_back((-temp[0] / temp[1]).imag());
- fs.push_back(0);
- cout << "Finding root " << n-1 << endl;
- cout << "Aprox root: " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
- << x2s[n - 3] << " " << "f:"
- << fs[n - 3] << endl;
- eps *= 2;
- ConstGrad(x1s[n-3], x2s[n-3], E/10, 0.00009, k, 1);
- cout << endl;
- cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
- << fn[n-2] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- cout << endl;
- xk2.push_back(x1n[n - 2]);
- yk2.push_back(x2n[n - 2]);
- fk2.push_back(fn[n - 2]);
- //cout << "Count of itterations: " << itt << endl;
- cout << "Count of fcalc: " << ff << endl;
- // cout << "Count of gradcalc: " << gc << endl;
- cout << endl;
- // Обнуляем показатели для следующего метода
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- ittv.push_back(itt);
- fcv.push_back(ff);
- gcv.push_back(gc);
- x1n.clear();
- x2n.clear();
- fn.clear();
- itt = 1;
- fc = 1;
- gc = 1;
- eps = 0.01;
- ff =0;
- ff1 = 0;
- ff2 = 0;
- cout << "IN ADVANCE GRAD:" << endl;
- cout << "Finding root " << 1 << endl;
- InAdvGrad(x1, x2, E, temp, 1);
- cout << endl;
- cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
- << fn[0] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- cout << endl;
- xk3.push_back(x1n[0]);
- yk3.push_back(x2n[0]);
- fk3.push_back(fn[0]);
- gornerd(x1n[0], x2n[0], temp);
- for (int i = 1; i < n - 2; i++) {
- cout << endl;
- cout << "Finding root " << i + 1 << endl;
- InAdvGrad(x1, x2, eps, temp, 2);
- eps *= 2;
- InAdvGrad(x1s[i-1], x2s[i-1], E, k, 1);
- cout << endl;
- cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
- << fn[i] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- gornerd(x1n[i], x2n[i], temp);
- xk3.push_back(x1n[i]);
- yk3.push_back(x2n[i]);
- fk3.push_back(fn[i]);
- }
- x1s.push_back((-temp[0] / temp[1]).real());
- x2s.push_back((-temp[0] / temp[1]).imag());
- fs.push_back(0);
- cout << "Finding root " << n-1 << endl;
- cout << "Aprox root: " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
- << x2s[n - 3] << " " << "f:"
- << fs[n - 3] << endl;
- InAdvGrad(x1s[n-3], x2s[n-3], E/10, k, 1);
- cout << endl;
- cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
- << fn[n-2] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- xk3.push_back(x1n[n - 2]);
- yk3.push_back(x2n[n - 2]);
- fk3.push_back(fn[n - 2]);
- // cout << "Count of itterations: " << itt << endl;
- cout << "Count of fcalc: " << ff << endl;
- //cout << "Count of gradcalc: " << gc << endl;
- cout << endl;
- // Обнуляем показатели для следующего метода
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- ittv.push_back(itt);
- fcv.push_back(ff);
- gcv.push_back(gc);
- x1n.clear();
- x2n.clear();
- fn.clear();
- itt = 1;
- fc = 1;
- gc = 1;
- eps = 0.01;
- ff =0;
- ff1 = 0;
- ff2 = 0;
- //Градиентный метод с дроблением шага
- cout << "DIV GRAD METHOD:" << endl;
- cout << "Finding root " << 1 << endl;
- DivGrad(x1, x2, E, 0.05, temp, 1);
- cout << endl;
- cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
- << fn[0] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- gornerd(x1n[0], x2n[0], temp);
- xk4.push_back(x1n[0]);
- yk4.push_back(x2n[0]);
- fk4.push_back(fn[0]);
- for (int i = 1; i < n - 2; i++) {
- cout << endl;
- cout << "Finding root " << i + 1 << endl;
- DivGrad(x1, x2, eps, 0.05, temp, 2);
- eps *= 2;
- DivGrad(x1s[i - 1], x2s[i - 1], E, 0.05, k, 1);
- cout << endl;
- cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
- << fn[i] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- gornerd(x1n[i], x2n[i], temp);
- xk4.push_back(x1n[i]);
- yk4.push_back(x2n[i]);
- fk4.push_back(fn[i]);
- }
- x1s.push_back((-temp[0] / temp[1]).real());
- x2s.push_back((-temp[0] / temp[1]).imag());
- fs.push_back(0);
- cout << "Finding root " << n-1 << endl;
- cout << "Aprox root: " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
- << x2s[n - 3] << " " << "f:"
- << fs[n - 3] << endl;
- DivGrad(x1s[n-3], x2s[n-3], E/10, 0.05, k, 1);
- cout << endl;
- cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
- << fn[n-2] << endl;
- ff =ff+ ff1+ff2;
- cout<<"f: "<<ff1+ff2<<endl;
- ff1 = 0;
- ff2 = 0;
- xk4.push_back(x1n[n - 2]);
- yk4.push_back(x2n[n - 2]);
- fk4.push_back(fn[n - 2]);
- // cout << "Count of itterations: " << itt << endl;
- cout << "Count of fcalc: " << ff << endl;
- //cout << "Count of gradcalc: " << gc << endl;
- cout << endl;
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- // Обнуляем показатели для следующего метода
- for (int i = 0; i < n; i++) {
- temp[i] = k[i];
- }
- ittv.push_back(itt);
- fcv.push_back(ff);
- gcv.push_back(gc);
- x1s.clear();
- fs.clear();
- fs.clear();
- ff =0;
- ff1 = 0;
- ff2 = 0;
- x1n.clear();
- x2n.clear();
- fn.clear();
- itt = 1;
- fc = 1;
- gc = 1;
- eps = 0.01;
- // МНГС
- /*cout << "ACCEL GRAD METHOD:" << endl;
- cout << "Finding root " << 1 << endl;
- AccelGrad(x1, x2, E/2, temp, 1);
- cout << endl;
- cout << "Root " << 1 << ": " << "x1:" << " " << x1n[0] << " " << "x2:" << x2n[0] << " " << "f:"
- << fn[0] << endl;
- gornerd(x1n[0], x2n[0], temp);
- xk5.push_back(x1n[0]);
- yk5.push_back(x2n[0]);
- fk5.push_back(fn[0]);
- for (int i = 1; i < n - 2; i++) {
- cout << endl;
- cout << "Finding root " << i + 1 << endl;
- AccelGrad(x1, x2, eps/2, temp, 2);
- eps *= 2;
- AccelGrad(x1s[i-1], x2s[i-1], E/2, k, 1);
- cout << endl;
- cout << "Root " << i + 1 << ": " << "x1:" << " " << x1n[i] << " " << "x2:" << x2n[i] << " " << "f:"
- << fn[i] << endl;
- gornerd(x1n[i], x2n[i], temp);
- xk5.push_back(x1n[i]);
- yk5.push_back(x2n[i]);
- fk5.push_back(fn[i]);
- }
- x1s.push_back((-temp[0] / temp[1]).real());
- x2s.push_back((-temp[0] / temp[1]).imag());
- fs.push_back(0);
- cout<<endl;
- cout << "Finding root " << n-1 << endl;
- cout << "Aprox root: " << ": " << "x1:" << " " << x1s[n - 3] << " " << "x2:"
- << x2s[n - 3] << " " << "f:"
- << fs[n - 3] << endl;
- AccelGrad(x1s[n-3], x2s[n-3], E, k, 1);
- cout << endl;
- cout << "Root " << n - 1 << ": " << "x1:" << " " << x1n[n-2] << " " << "x2:" << x2n[n-2] << " " << "f:"
- << fn[n-2] << endl;
- cout << endl;
- xk5.push_back(x1n[n - 2]);
- yk5.push_back(x2n[n - 2]);
- fk5.push_back(fn[n - 2]);
- cout << "Count of itterations: " << itt << endl;
- cout << "Count of fcalc: " << fc << endl;
- cout << "Count of gradcalc: " << gc << endl;
- cout << endl;
- // Обнуляем показатели
- ittv.push_back(itt);
- fcv.push_back(fc);
- gcv.push_back(gc);
- x1n.clear();
- x2n.clear();
- fn.clear();
- cout<<"essss"<<endl;
- AccelGrad(1.31, -0.025, E/100, k, 1);*/
- cout<<"COORD DESCENT:"<<setw(50)<<"CONST GRAD METHOD:"<<setw(40)<<"IN ADVANCE GRAD:"<<setw(40)<< "DIV GRAD METHOD:"<<endl;
- cout << setw(12) << left << "x" << setw(12) << "y" << setw(15) << "f"
- << setw(12) << "x" << setw(12) << "y" << setw(15) << "f"
- << setw(12) << "x" << setw(12) << "y" << setw(15) << "f"
- << setw(12) << "x" << setw(12) << "y" << setw(15) << "f"<<endl;
- cout << setw(12) << left << xk1[0] << setw(12) << yk1[0] << setw(15) << fk1[0]
- << setw(12) << xk2[0] << setw(12) << yk2[0] << setw(15) << fk2[0]
- << setw(12) << xk3[0] << setw(12) << yk3[0] << setw(15) << fk3[0]
- << setw(12) << xk4[0] << setw(12) << yk4[0] << setw(15) << fk4[0]
- << endl;
- cout << setw(12) << left << xk1[1] << setw(12) << yk1[1] << setw(15) << fk1[1]
- << setw(12) << xk2[1] << setw(12) << yk2[1] << setw(15) << fk2[1]
- << setw(12) << xk3[1] << setw(12) << yk3[1] << setw(15) << fk3[1]
- << setw(12) << xk4[1] << setw(12) << yk4[1] << setw(15) << fk4[1]
- << endl;
- cout << setw(12) << left << xk1[2] << setw(12) << yk1[2] << setw(15) << fk1[2]
- << setw(12) << xk2[2] << setw(12) << yk2[2] << setw(15) << fk2[2]
- << setw(12) << xk3[2] << setw(12) << yk3[2] << setw(15) << fk3[2]
- << setw(12) << xk4[2] << setw(12) << yk4[2] << setw(15) << fk4[2]
- <<endl;
- cout << setw(12) << left << xk1[3] << setw(12) << yk1[3] << setw(15) << fk1[3]
- << setw(12) << xk2[3] << setw(12) << yk2[3] << setw(15) << fk2[3]
- << setw(12) << xk3[3] << setw(12) << yk3[3] << setw(15) << fk3[3]
- << setw(12) << xk4[3] << setw(12) << yk4[3] << setw(15) << fk4[3]
- << endl;
- //cout<<setw(20)<<"count of itt: "<<setw(35)<<ittv[0]<<setw(35)<<ittv[1]<<setw(35)<<ittv[2]<<setw(35)<<ittv[3]<<setw(35)<<endl;
- cout<<setw(20)<<"count of fcalc: "<<setw(35)<<fcv[0]<<setw(35)<<fcv[1]<<setw(35)<<fcv[2]<<setw(35)<<fcv[3]<<setw(35)<<endl;
- // cout<<setw(12)<<"count of gradcalc: "<<setw(35)<<gcv[0]<<setw(35)<<gcv[1]<<setw(35)<<gcv[2]<<setw(35)<<gcv[3]<<setw(45)<<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 i = 2;
- int j = 1;
- double f1, f2, f;
- f = (gornerf(x1, x2, koef));
- cout << 1 << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- while (abs(f) >= E) {
- itt++;
- int flag = -1;
- f1 = gornerf(x1 + alp1, x2, koef);
- f2 = gornerf(x1 - alp1, x2, koef);
- if (f1 < f) {
- x1 = x1 + alp1;
- f = f1;
- flag++;
- } else if (f2 < f) {
- x1 = x1 - alp1;
- f = f2;
- flag++;
- } else
- alp1 = 0.5 * alp1;
- f1 = gornerf(x1, x2 + alp2, koef);
- f2 = gornerf(x1, x2 - alp2, koef);
- if (f1 < f) {
- x2 = x2 + alp2;
- f = f1;
- flag++;
- } else if (f2 < f) {
- x2 = x2 - alp2;
- f = f2;
- flag++;
- } else
- alp2 = 0.5 * alp2;
- if(flag!=-1)
- {
- cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- i++;
- }
- fc = fc + 4;
- }
- if (k == 1) {
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1n.push_back(x1);
- x2n.push_back(x2);
- fn.push_back(f);
- ff1 = 1+(i-1)*4;
- }
- if (k == 2) {
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1s.push_back(x1);
- x2s.push_back(x2);
- fs.push_back(f);
- ff2 = 1+(i-1)*4;
- }
- return 0;
- }
- //градиент функции в точке
- vector<double> gradient(double x, double y, complex<double> *koef) {
- complex<double> div[n];
- for (int i = 0; i < n - 1; i++) {
- div[i] = (koef[i + 1] * (i + 1.0));
- }
- div[8] = ((0, 0));
- complex<double> a = conj(gorner1f(x, y, koef));
- complex<double> b = (gorner1f(x, y, div));
- vector<double> grad;
- grad.push_back((2.0 * a * b).real());
- grad.push_back(-(2.0 * a * b).imag());
- return grad;
- }
- //Градиентный спуск с постоянным шагом
- double ConstGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k) {
- double x1 = x01;
- double x2 = x02;
- double alp = alp0;
- int i = 1;
- double f;
- f = gornerf(x1, x2, koef);
- while (abs(f) >= E) {
- vector<double> grad = gradient(x1, x2, koef);
- cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1 = x1 - alp * grad[0];
- x2 = x2 - alp * grad[1];
- f = gornerf(x1, x2, koef);
- fc++;
- gc++;
- itt++;
- i++;
- }
- if (k == 1) {
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1n.push_back(x1);
- x2n.push_back(x2);
- fn.push_back(f);
- ff1 = i;
- }
- if (k == 2) {
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1s.push_back(x1);
- x2s.push_back(x2);
- fs.push_back(f);
- ff2=i;
- }
- return 0;
- }
- //Градиентный спуск с заранее заданным шагом
- double InAdvGrad(double x01, double x02, const double E, complex<double> *koef, int m) {
- double x1 = x01;
- double x2 = x02;
- int k = 1800;
- int i = 1;
- double f = gornerf(x1, x2, koef);
- vector<double> grad = gradient(x1, x2, koef);
- while (abs(f) >= E) {
- x1 = x1 - (1.0 / k) * grad[0];
- x2 = x2 - (1.0 / k) * grad[1];
- k++;
- f = gornerf(x1, x2, koef);
- grad = gradient(x1, x2, koef);
- itt++;
- fc++;
- gc++;
- i++;
- cout << "x1: " << x1 << " " << "x2:" << " " << x2 << " " << "f:" << gornerf(x1, x2, koef) << endl;
- }
- if (m == 1) {
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1n.push_back(x1);
- x2n.push_back(x2);
- fn.push_back(f);
- ff1 = i;
- }
- if (m == 2) {
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1s.push_back(x1);
- x2s.push_back(x2);
- fs.push_back(f);
- ff2 = i;
- }
- return 0;
- }
- //Градиентный спуск с дроблением шага
- double DivGrad(double x01, double x02, const double E, const double alp0, complex<double> *koef, int k) {
- double x1 = x01;
- double x2 = x02;
- double alp = alp0;
- double e = 0.5;
- int i = 1;
- int j = 1;
- double f = gornerf(x1, x2, koef);
- vector<double> grad = gradient(x1, x2, koef);
- while (abs(f) >= E) {
- cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- i++;
- itt++;
- double x = x1 - alp * grad[0];
- double y = x2 - alp * grad[1];
- if ((gornerf(x, y, koef) - f) <=
- -alp * e * (pow(grad[0], 2) + pow(grad[2], 2))) {
- x1 = x1 - alp * grad[0];
- x2 = x2 - alp * grad[1];
- f = gornerf(x1, x2, koef);
- j++;
- grad = gradient(x1, x2, koef);
- fc++;
- gc++;
- } else
- alp = 0.5 * alp;
- }
- if (k == 1) {
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1n.push_back(x1);
- x2n.push_back(x2);
- fn.push_back(f);
- ff1 = j;
- }
- if (k == 2) {
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1s.push_back(x1);
- x2s.push_back(x2);
- fs.push_back(f);
- ff2 = j;
- }
- return 0;
- }
- //Золотое сечение
- double goldsec(double E, double a0, double b0, double x1, double x2, double alpha, complex<double> *koef) {
- double a = a0, b = b0, con = (3 - sqrt(5)) / 2, c = con * (b - a) + a,
- d = (1 - con) * (b - a) + a, fc = funcForOneDimMinimize(x1, x2, c, koef), fd = funcForOneDimMinimize(x1, x2,
- d,
- koef);
- while (abs(b - a) > 2 * E) {
- if (fc <= fd) {
- b = d;
- d = c;
- c = con * (b - a) + a;
- fd = fc;
- fc = funcForOneDimMinimize(x1, x2, c, koef);
- } else {
- a = c;
- c = d;
- d = (1 - con) * (b - a) + a;
- fc = fd;
- fd = funcForOneDimMinimize(x1, x2, d, koef);
- }
- }
- return (b + a) / 2;
- }
- // Функция для одномерной минимизации
- double funcForOneDimMinimize(double x1, double x2, double alpha, complex<double> *koef) {
- return gornerf(x1 - alpha * gradient(x1, x2, koef)[0], x2 - alpha * gradient(x1, x2, koef)[1], koef);
- }
- //МНГС
- double AccelGrad(double x01, double x02, const double E, complex<double> *koef, int k) {
- double x1 = x01;
- double x2 = x02;
- int i = 1;
- double f = gornerf(x1, x2, koef);
- while (abs(f) >= E) {
- cout << i << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- i++;
- itt++;
- double alp = goldsec(0.000000000000009, -1000, 1000, x1, x2, 0.0001, koef);
- vector<double> grad = gradient(x1, x2, koef);
- x1 = x1 - alp * grad[0];
- x2 = x2 - alp * grad[1];
- f = gornerf(x1, x2, koef);
- fc++;
- gc++;
- }
- if (k == 1) {
- cout << "Exact root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1n.push_back(x1);
- x2n.push_back(x2);
- fn.push_back(f);
- }
- if (k == 2) {
- cout << "Aprox root: " << " x: " << x1 << " y: " << x2 << " f: " << f << endl;
- x1s.push_back(x1);
- x2s.push_back(x2);
- fs.push_back(f);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement