Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool criterionOfNewton(const matrix <double> &x, double eps) {
- for (int i = 0; i < x.rowSize(); i++) {
- for (int j = 0; j < x.colSize(); j++) {
- if (abs(x[i][j] > eps)) return false;
- }
- }
- return true;
- }
- matrix <double> getNewton(int n, double eps, int k,int m, const matrix <double> &first, int &number) {
- matrix <double> Jx(n, n);
- matrix <double> X(n, 1);
- matrix <double> Fx(n, 1);
- matrix <double> FL(n, n);
- matrix <double> FU(n, n);
- matrix <double> FP(n, n);
- int numberOfIterations = 0;
- int countForDet;
- //Jx = getJx(first);
- //getLUP(Jx,FL,FU, FP, countForDet);
- matrix <double> X_next(first);
- do {
- if (numberOfIterations < m) {
- Jx = getJx(X_next);
- getLUP(Jx,FL,FU,FP,countForDet);
- }
- X = X_next;
- Fx = getFx(X);
- //getSystemSolutions(Jx,-Fx,X_next);
- getSystemSolutions(FL, FU, FP, -Fx, X_next);
- numberOfIterations++;
- X_next += X;
- } while (!criterionOfNewton(X_next - X, eps) && numberOfIterations < k);
- //cout << "number of iterations: " << numberOfIterations << endl;
- number = numberOfIterations;
- return X;
- }
- // all for 3
- void ALLNEWTON() {
- //double sol = getNewton(&myFunction, &myDxFunction, 0, 0.000000001);
- //cout <<"SOL: " << sol <<" res: " << myFunction(sol) << endl;
- matrix <double> X(10, 1);
- matrix <double> FIRST(10, 1);
- FIRST[0][0] = 0.5;
- FIRST[1][0] = 0.5;
- FIRST[2][0] = 1.5;
- FIRST[3][0] = -1.0;
- FIRST[4][0] = -0.5;
- FIRST[5][0] = 1.5;
- FIRST[6][0] = 0.5;
- FIRST[7][0] = -0.5;
- FIRST[8][0] = 1.5;
- FIRST[9][0] = -1.5;
- int numberOfIterations = 0;
- double average_time = 0;
- /*for (int m = 1; m < 17; m++) {
- for (int i = 0; i < 10; i++) {
- clock_t start = clock();
- X = getNewton(10, 0.0000001, 50, m, FIRST,numberOfIterations);
- clock_t end = clock();
- average_time += (double)(end - start)/CLOCKS_PER_SEC;
- }
- cout << "x[4]=-0.5 , m= " << m << " , average time= " << average_time / 10 << endl;
- cout << "numberOfIterations: " << numberOfIterations << endl << endl;
- average_time = 0;
- }
- X[4][0] = -0.2;
- cout << endl;
- for (int m = 1; m < 17; m++) {
- for (int i = 0; i < 10; i++) {
- clock_t start = clock();
- X = getNewton(10, 0.0000001, 50, m, FIRST,numberOfIterations);
- clock_t end = clock();
- average_time += (double)(end - start) / CLOCKS_PER_SEC;
- }
- cout << "x[4]=-0.2 , m= " << m << " , average time= " << average_time / 10 << endl;
- cout << "numberOfIterations: " << numberOfIterations << endl << endl;
- average_time = 0;
- }
- */
- clock_t start = clock();
- X = getNewton(10, 0.0000001, 900, 1000, FIRST, numberOfIterations);
- clock_t end = clock();
- cout << "FIRST[4][0]=-0.5\n";
- cout << "Program execution time: " << (double)(end - start) / CLOCKS_PER_SEC << " sec" << endl;
- cout << "number Of Iterations " << numberOfIterations << endl;
- cout << "solutions:\n" << X << endl;
- cout << "F(x)= \n" << getFx(X) << endl;
- cout << "/////////////////////////////////\n";
- cout << "FIRST[4][0]=-0.2\n";
- FIRST[4][0] = -0.2;
- start = clock();
- X = getNewton(10, 0.0000001, 900, 1000, FIRST, numberOfIterations);
- end = clock();
- cout << "Program execution time: " << (double)(end - start) / CLOCKS_PER_SEC << " sec" << endl;
- cout << "number Of Iterations " << numberOfIterations << endl;
- cout << "solutions:\n" << X << endl;
- cout << "F(x)= \n" << getFx(X) << endl;
- cout << "/////////////////////////////////\n";
- cout << "FIRST[4][0]=-0.5\n";
- FIRST[4][0] = -0.5;
- average_time = 0;
- double min_time = 100000000000;
- int min_m = 1;
- for (int m = 1; m < 7; m++) {
- for (int i = 0; i < 10; i++) {
- clock_t start = clock();
- X = getNewton(10, 0.0000001, 500, m, FIRST, numberOfIterations);
- clock_t end = clock();
- average_time += (double)(end - start) / CLOCKS_PER_SEC;
- }
- cout << "x[4]=-0.5 , m= " << m << " , average time= " << average_time / 10 << endl;
- cout << "numberOfIterations: " << numberOfIterations << endl << endl;
- if (average_time < min_time) {
- min_time = average_time;
- min_m = m;
- }
- average_time = 0;
- }
- cout << endl << "m= " << min_m << " min time= " << min_time / 10 << endl;
- cout << "/////////////////////////////////\n";
- cout << "FIRST[4][0]=-0.2\n";
- matrix <double> N0(10, 1, 0); // нулевой вектор
- FIRST[4][0] = -0.2;
- average_time = 0;
- min_time = 100000000000;
- min_m = 1;
- for (int m = 1; m < 13; m++) {
- for (int i = 0; i < 10; i++) {
- clock_t start = clock();
- X = getNewton(10, 0.0000001, 500, m, FIRST, numberOfIterations);
- clock_t end = clock();
- average_time += (double)(end - start) / CLOCKS_PER_SEC;
- }
- if (average_time < min_time && cmp(getFx(X), N0)) {
- min_time = average_time;
- min_m = m;
- }
- cout << "x[4]=-0.2 , m= " << m << " , average time= " << average_time / 10 << endl;
- if (cmp(getFx(X), N0)) cout << "numberOfIterations: " << numberOfIterations << endl << endl;
- else cout << "not answer" << endl << endl;
- average_time = 0;
- }
- cout << endl << "m= " << min_m << " min time= " << min_time / 10 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement