Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <math.h>
- #include <vector>
- #include <iostream>
- #include <iomanip>
- typedef std::vector<double> Vector;
- typedef std::vector<std::vector<double>> Matrix;
- void fillZeros(Matrix& M, int n)
- {
- M.clear();
- for (int i = 0; i < n; ++i)
- {
- M.push_back(std::vector<double>());
- for (int j = 0; j < n; ++j)
- M[i].push_back(0);
- }
- }
- void printVector(const Vector& v)
- {
- int n = v.size();
- for (int i = 0; i < n; ++i)
- std::cout << std::left << std::setw(11) << v[i];
- std::cout << std::endl;
- }
- void fillZeros(Vector& V, int n)
- {
- V.clear();
- V.resize(n, 0);
- }
- void read_f(double& alpha, double& beta, double& a, double& b, double& c, double& d)
- {
- FILE* f;
- f = fopen("Text.txt", "r");
- fscanf_s(f, "%lf", &alpha);
- fscanf_s(f, "%lf", &beta);
- fscanf_s(f, "%lf", &a);
- fscanf_s(f, "%lf", &b);
- fscanf_s(f, "%lf", &c);
- fscanf_s(f, "%lf", &d);
- fclose(f);
- }
- void W_Matr(Matrix& W, Vector& x, double& alpha, double& a, double& b)
- {
- fillZeros(W, 2);
- W[0][0] = (x[1] / pow(cos(x[0] * x[1] + alpha), 2)) - 2 * x[0];
- W[0][1]= (x[0] / pow(cos(x[0] * x[1] + alpha), 2));
- W[1][0] = 2 * a*x[0];
- W[1][1] = 2 * b*x[1];
- }
- double Det(Matrix& W)
- {
- double det;
- det = W[0][0] * W[1][1] - W[0][1] * W[1][0];
- return det;
- }
- void W_reverese(Matrix& W, Matrix& W_rev, double& det)
- {
- fillZeros(W_rev, 2);
- W_rev[0][0] = (1 / det)*W[1][1];
- W_rev[0][1] = -(1 / det)*W[0][1];
- W_rev[1][0] = -(1 / det)*W[1][0];
- W_rev[1][1] = (1 / det)*W[0][0];
- }
- double NormVector(const Vector& a)
- {
- double sum = 0;
- for (int i = 0; i < a.size(); ++i)
- sum += a[i] * a[i];
- return sqrt(sum);
- }
- void SubVectors(const Vector& a, const Vector& b, Vector& r)
- {
- r.clear();
- r.resize(a.size(), 0);
- int n = a.size();
- for (int i = 0; i < n; ++i)
- {
- r[i] = a[i] - b[i];
- }
- }
- void MulMatrixVector(const Matrix& A, const Vector& b, Vector& r)
- {
- r.clear();
- r.resize(A.size(), 0);
- int n = A.size();
- for (int i = 0; i < n; ++i)
- {
- double sum = 0;
- for (int j = 0; j < n; ++j)
- {
- sum += A[i][j] * b[j];
- }
- r[i] = sum;
- }
- }
- void Iterations(Matrix& W_reverse, double alpha, double beta, double b, double a, double d, Vector& x)
- {
- Vector F;
- fillZeros(x, 2);
- Vector r;
- fillZeros(r, 2);
- int iteration = 0;
- double err = 0, norm_D = 0;
- Vector t(2, 0);
- /*
- x[0]= ;
- x[1]= ;
- */
- do
- {
- ++iteration;
- t[0] = x[0];
- t[1] = x[1];
- x[0] = t[0]-MulMatrixVector(W_reverse, F, r);
- x[1] = t[1]-;
- Vector e(2, 0);
- e[0] = tan(x[0] * x[1] + alpha) - x[0] * x[0];
- e[1] = a * x[0] * x[0] + b * x[1] * x[1] - 1;
- err = NormVector(e);
- Vector D;
- SubVectors(x, t, D);
- norm_D = NormVector(D);
- std::cout << " * Iteration #" << iteration << ": error = " << err << ", delta = " << norm_D << std::endl;
- std::cout << "X vector: ";
- printVector(x);
- std::cout << "E vector: ";
- printVector(e);
- std::cout << "D vector: ";
- printVector(D);
- } while ((abs(err) > 0.00001) || (abs(norm_D) > 0.00001));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement