SHARE
TWEET

Untitled

a guest Dec 9th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. #include <vector>
  7. #include <iostream>
  8. #include <iomanip>
  9.  
  10. typedef std::vector<double> Vector;
  11. typedef std::vector<std::vector<double>> Matrix;
  12.  
  13. void fillZeros(Matrix& M, int n)
  14. {
  15.     M.clear();
  16.     for (int i = 0; i < n; ++i)
  17.     {
  18.         M.push_back(std::vector<double>());
  19.         for (int j = 0; j < n; ++j)
  20.             M[i].push_back(0);
  21.     }
  22.  
  23. }
  24.  
  25. void printVector(const Vector& v)
  26. {
  27.     int n = v.size();
  28.     for (int i = 0; i < n; ++i)
  29.         std::cout << std::left << std::setw(11) << v[i];
  30.     std::cout << std::endl;
  31. }
  32.  
  33.  
  34.  
  35. void fillZeros(Vector& V, int n)
  36. {
  37.     V.clear();
  38.     V.resize(n, 0);
  39. }
  40.  
  41.  
  42. void read_f(double& alpha, double& beta, double& a, double& b, double& c, double& d)
  43. {
  44.     FILE* f;
  45.     f = fopen("Text.txt", "r");
  46.     fscanf_s(f, "%lf", &alpha);
  47.     fscanf_s(f, "%lf", &beta);
  48.     fscanf_s(f, "%lf", &a);
  49.     fscanf_s(f, "%lf", &b);
  50.     fscanf_s(f, "%lf", &c);
  51.     fscanf_s(f, "%lf", &d);
  52.     fclose(f);
  53. }
  54.  
  55.  
  56. void W_Matr(Matrix& W, Vector& x, double& alpha, double& a, double& b)
  57. {
  58.     fillZeros(W, 2);
  59.     W[0][0] = (x[1] / pow(cos(x[0] * x[1] + alpha), 2)) - 2 * x[0];
  60.     W[0][1]= (x[0] / pow(cos(x[0] * x[1] + alpha), 2));
  61.     W[1][0] = 2 * a*x[0];
  62.     W[1][1] = 2 * b*x[1];
  63. }
  64.  
  65. double Det(Matrix& W)
  66. {
  67.     double det;
  68.     det = W[0][0] * W[1][1] - W[0][1] * W[1][0];
  69.     return det;
  70. }
  71.  
  72. void W_reverese(Matrix& W, Matrix& W_rev, double& det)
  73. {
  74.     fillZeros(W_rev, 2);
  75.     W_rev[0][0] = (1 / det)*W[1][1];
  76.     W_rev[0][1] = -(1 / det)*W[0][1];
  77.     W_rev[1][0] = -(1 / det)*W[1][0];
  78.     W_rev[1][1] = (1 / det)*W[0][0];
  79. }
  80.  
  81. double NormVector(const Vector& a)
  82. {
  83.     double sum = 0;
  84.     for (int i = 0; i < a.size(); ++i)
  85.         sum += a[i] * a[i];
  86.  
  87.     return sqrt(sum);
  88. }
  89.  
  90. void SubVectors(const Vector& a, const Vector& b, Vector& r)
  91. {
  92.     r.clear();
  93.     r.resize(a.size(), 0);
  94.     int n = a.size();
  95.  
  96.     for (int i = 0; i < n; ++i)
  97.     {
  98.         r[i] = a[i] - b[i];
  99.     }
  100. }
  101.  
  102. void MulMatrixVector(const Matrix& A, const Vector& b, Vector& r)
  103. {
  104.     r.clear();
  105.     r.resize(A.size(), 0);
  106.     int n = A.size();
  107.  
  108.     for (int i = 0; i < n; ++i)
  109.     {
  110.         double sum = 0;
  111.         for (int j = 0; j < n; ++j)
  112.         {
  113.             sum += A[i][j] * b[j];
  114.         }
  115.         r[i] = sum;
  116.     }
  117. }
  118.  
  119. void Iterations(Matrix& W_reverse, double alpha, double beta, double b, double a, double d, Vector& x)
  120. {
  121.     Vector F;
  122.  
  123.     fillZeros(x, 2);
  124.     Vector r;
  125.     fillZeros(r, 2);
  126.     int iteration = 0;
  127.     double err = 0, norm_D = 0;
  128.     Vector t(2, 0);
  129.  
  130.     /*
  131.     x[0]= ;
  132.     x[1]= ;
  133.     */
  134.  
  135.     do
  136.     {
  137.         ++iteration;
  138.  
  139.         t[0] = x[0];
  140.         t[1] = x[1];
  141.  
  142.  
  143.         x[0] = t[0]-MulMatrixVector(W_reverse, F, r);
  144.         x[1] = t[1]-;
  145.  
  146.         Vector e(2, 0);
  147.         e[0] = tan(x[0] * x[1] + alpha) - x[0] * x[0];
  148.         e[1] = a * x[0] * x[0] + b * x[1] * x[1] - 1;
  149.  
  150.         err = NormVector(e);
  151.  
  152.         Vector D;
  153.         SubVectors(x, t, D);
  154.  
  155.         norm_D = NormVector(D);
  156.  
  157.         std::cout << " * Iteration #" << iteration << ": error = " << err << ", delta = " << norm_D << std::endl;
  158.         std::cout << "X vector: ";
  159.         printVector(x);
  160.         std::cout << "E vector: ";
  161.         printVector(e);
  162.         std::cout << "D vector: ";
  163.         printVector(D);
  164.     } while ((abs(err) > 0.00001) || (abs(norm_D) > 0.00001));
  165. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top