Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- plot "D:\graphic.txt" with lines title "(x_i, U_i)"
- */
- #include <iostream>
- #include <math.h>
- #include <cmath>
- #include <stdlib.h>
- #include <fstream>
- using namespace std;
- const double PI = 3.141592653589793238463;
- //Задание функции
- typedef double(*functiontype1)(double x);
- typedef double(*functiontype2)(double x, double s);
- double f(double x)
- {
- return x * x * x;
- }
- double K(double x, double s)
- {
- return x * x + s * s;
- }
- functiontype1 Func1 = &f;
- functiontype2 Func2 = &K;
- //Задает набор узлов, равномерно удаленных друг от друга
- void ValueUniformTable(double* Array, double Initial, double End, int CountDots)
- {
- double h = abs(End - Initial) / (CountDots - 1);
- Array[0] = Initial;
- for (int i = 1; i < CountDots; i++)
- Array[i] = Array[i - 1] + h;
- }
- //Задает набор узлов по Чебышеву
- void ValueChebyshevTable(double* Array, double Initial, double End, int CountDots)
- {
- double k1, k2, denom;
- Array[0] = Initial;
- Array[CountDots - 1] = End;
- k1 = (End + Initial) / 2;
- k2 = (End - Initial) / 2;
- denom = 2 * (CountDots);
- for (int i = 1; i < CountDots - 1; i++)
- Array[CountDots - 1 - i] = k1 + k2 * cos(((2 * i + 1) * PI) / (denom));
- }
- void Gaussian_elimination(double** coeffs_massiv, double* y_Massiv, double* U_massiv, int n) {
- double* tmp_mas;
- tmp_mas = new double[n];
- //double tmp_mas[n];
- int i, j, k, index;
- double tmp, max, r;
- for (j = 0; j <= n - 2; j++) {
- max = abs(coeffs_massiv[j][j]);
- index = j;
- //Поиск строки с макс по модулю элементом в jом столбце
- for (i = index + 1; i <= n - 1; i++) { // Идем по столбцу index=j до самого низа
- if (abs(coeffs_massiv[i][index]) > max) {
- max = abs(coeffs_massiv[i][index]);
- index = i;
- }
- } //index хранит строку с макс эл в jом столбце
- cout << "max: " << coeffs_massiv[index][j] << endl;
- cout << "Переставляем " << j << " и " << index << " строку " << endl;
- //Реализация перестановки 2х строк: j-ой и найденной = index перед итерацией Гаусса
- if (index != j) {
- for (i = 0; i <= n - 1; i++) {
- tmp_mas[i] = coeffs_massiv[j][i];
- coeffs_massiv[j][i] = coeffs_massiv[index][i];
- coeffs_massiv[index][i] = tmp_mas[i];
- }
- tmp = y_Massiv[j]; //правая часть Jой строки
- y_Massiv[j] = y_Massiv[index];
- y_Massiv[index] = tmp;
- }
- // Будем Jой строкой убивать jые элементы во всех строках находящихся ниже
- for (i = j + 1; i <= n - 1; i++) { //фиксируем iую строку (это внутри цикла для jj элемента)
- r = -coeffs_massiv[i][j] / coeffs_massiv[j][j];
- for (k = j; k <= n - 1; k++) { //Домножили jую и сложили с ioй - изменилась iая строка
- coeffs_massiv[i][k] = coeffs_massiv[i][k] + r * coeffs_massiv[j][k];
- }
- y_Massiv[i] = y_Massiv[i] + r * y_Massiv[j];
- }
- }
- //обратная подстановка
- U_massiv[n - 1] = y_Massiv[n - 1] / coeffs_massiv[n - 1][n - 1]; //Первый шаг длаем сами
- for (i = n - 2; i >= 0; i--) { //фиксируем iую строку - будем искать xi
- tmp = 0;
- for (int j = i + 1; j <= n - 1; j++)
- tmp += coeffs_massiv[i][j] * U_massiv[j]; //Переносим вправо всё что известно и суммируем, но в отдельную переменную чтобы не испортить правую часть.
- U_massiv[i] = (y_Massiv[i] - tmp) / coeffs_massiv[i][i];
- }
- }
- //Запись в файл (x_i, U_i)
- void table_in_file(double* x_Array, int CountDots, double* U_massiv)
- {
- ofstream fout("D:/graphic.txt");
- for (int i = 0; i < CountDots; i++)
- fout << x_Array[i] << " " << U_massiv[i] << endl;
- fout.close();
- cout << "Запись в файл осуществлена!" << endl;
- }
- void get_coeffs_massiv(functiontype2* K, functiontype1* f, int CountDots, double** coeffs_massiv, double* x_Array, double* y_Array) {
- int i, j;
- for (i = 0; i < CountDots; i++) {
- for (j = 0; j < CountDots; j++) {
- if (j == 0)
- coeffs_massiv[i][j] = 1;
- else
- coeffs_massiv[i][j] = -(x_Array[j] - x_Array[j - 1]) * (*K)(x_Array[i], x_Array[j]);
- }
- y_Array[i] = (*f)(x_Array[i]);
- }
- }
- int main()
- {
- setlocale(LC_ALL, "RUS");
- int i, j;
- /*---------------------------------Входные данные-------------------------------------*/
- double Initial = -5, End = 5;
- int CountDots = 3; //Количество узлов (иксов)
- /*---------------------------------Задаём необходимые массивы-------------------------*/
- double** coeffs_massiv; //массив хранящий коэффициенты СЛАУ
- coeffs_massiv = new double* [CountDots];
- for (i = 0; i < CountDots; i++)
- coeffs_massiv[i] = new double[2];
- double* U_massiv = new double[CountDots]; //Хранит U_i
- double* y_Array = new double[CountDots]; //Правая часть СЛАУ
- double* x_Array = new double[CountDots]; //Узлы
- /*---------------------------------Заполняем и решаем---------------------------------*/
- ValueUniformTable(x_Array, Initial, End, CountDots); //Строим сетку
- get_coeffs_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array); //Строим СЛАУ
- cout << endl << "Полученная СЛАУ: " << endl;
- for (i = 0; i < CountDots; i++) {
- for (int j = 0; j < CountDots; j++) {
- cout << coeffs_massiv[i][j] << " ";
- }
- cout << y_Array[i] << endl;
- cout << endl;
- }
- Gaussian_elimination(coeffs_massiv, y_Array, U_massiv, CountDots); //Гауссим и получаем ответ - U_massiv
- table_in_file(x_Array, CountDots, U_massiv);
- /*---------------------------------Вывод в консоль------------------------------------*/
- cout << endl << "Полученная СЛАУ: " << endl;
- for (i = 0; i < CountDots; i++) {
- for (int j = 0; j < CountDots; j++) {
- cout << coeffs_massiv[i][j] << " ";
- }
- cout << y_Array[i] << endl;
- cout << endl;
- }
- cout << endl << "Узлы: " << endl;
- for (j = 0; j < CountDots; j++) {
- cout << x_Array[j] << " ";
- }
- cout << endl << "U_massiv: " << endl;
- for (j = 0; j < CountDots; j++) {
- cout << U_massiv[j] << " ";
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment