Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- plot "D:\spline_graphic.txt" with lines title "Сплайн", "D:\original_graphic.txt" with lines title "График функции"
- plot "D:\difference_graphic.txt" with lines title "Разница"
- */
- #include <iostream>
- #include <math.h>
- #include <cmath>
- #include <Windows.h>
- #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 exp(x) + (1 + 593.653)*x*x;
- }
- double K(double x, double s)
- {
- return x * s;
- }
- functiontype1 Func1 = &f;
- functiontype2 Func2 = &K;
- void ValueUniformTable(double* Array, double Initial, double End, int CountSegments)
- {
- double h;
- ofstream fout4("D:\setka_uni.txt");
- Array[0] = Initial;
- fout4 << Array[0] << " " << endl;
- h = abs(End - Initial) / CountSegments;
- for (int i = 1; i <= CountSegments; i++)
- {
- Array[i] = Array[i - 1] + h;
- fout4 << Array[i] << endl;
- }
- fout4.close();
- }
- void ValueChebyshevTable(double* Array, double Initial, double End, int CountSegments)
- {
- ofstream fout5("D:\setka_cheb.txt");
- Array[0] = Initial;
- fout5 << Array[0] << " " << Array[0] << endl;
- double k1, k2, denom;
- k1 = (End + Initial) / 2;
- k2 = (End - Initial) / 2;
- denom = 2 * (CountSegments + 1);
- for (int i = 1; i < CountSegments; i++)
- {
- cout << k1 + k2 * cos(PI / (denom)) << endl;
- Array[CountSegments - i] = k1 + k2 * cos(((2 * i + 1)*PI) / (denom));
- fout5 << Array[CountSegments - i] << " " << endl;
- }
- Array[CountSegments] = End;
- fout5 << Array[CountSegments] << " " << endl;
- fout5.close();
- }
- void Gauss_with_vibor(double** coeffs_massiv, double* y_Massiv, int n) {
- double* tmp_mas;
- tmp_mas = new double[n];
- int p, 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[i][index] << 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];
- }
- }
- //обратная подстановка
- x[n - 1] = y_Massiv[n - 1]/coeffs_massiv[n - 1][n - 1]; //Первый шаг длаем сами
- for (j = n - 2; j >= 0; j--) { //фиксируем Jую строку - будем искать xj
- for (int i = j+1; i <= n-1; i++)
- y_Massiv[j] = y_Massiv[j] - coeffs_massiv[j][i] * x[j]; //Переносим вправо всё что известно и суммируем
- x[j] = y_Massiv[j]/coeffs_massiv[j][j]
- }
- }
- //Запись в 3 файла: значений сплайна, интерполируемой функции и их разницы в countdots точках
- void tables_in_file(functiontype1* f, double* Array, int Count_dots, int CountSegments, double Initial, double End)
- {
- int i = 0;
- double step = (End - Initial) / (Count_dots - 1), y_value1, y_value2, x_value;
- ofstream fout1("D:/spline_graphic.txt");
- ofstream fout2("D:/original_graphic.txt");
- ofstream fout3("D:/difference_graphic.txt");
- x_value = Initial;
- for (i = 0; i < CountSegments; i++)
- {
- while (x_value < Array[i + 1])
- {
- fout1 << x_value << " ";
- fout2 << x_value << " ";
- fout3 << x_value << " ";
- x_value += step;
- }
- }
- fout1 << End << " ";
- fout2 << End << " ";
- fout2 << (*f)(End) << endl;
- fout1.close();
- fout2.close();
- //cout << endl << "Запись в файл осуществлена!" << endl;
- }
- void get_equations_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]);
- }
- for (i = 0; i < CountDots; i++) {
- for (j = 0; j < CountDots; j++) {
- cout << coeffs_massiv[i][j] << " ";
- }
- cout << y_Array[i] << endl;
- cout << endl;
- }
- }
- int main()
- {
- setlocale(LC_ALL, "RUS");
- functiontype1 Func = &f;
- int CountDots = 3, i, Graphic_Dots = 250;
- /*---------------------------------Входные данные-------------------------------------*/
- double Initial = -10, End = 10;
- double** coeffs_massiv;
- coeffs_massiv = new double*[CountDots];
- for (i = 0; i < CountDots; i++)
- coeffs_massiv[i] = new double[2];
- double* y_Array = new double[CountDots];
- /*double* x_Array = new double[CountDots];
- ValueUniformTable(x_Array, Initial, End, CountDots);
- double* y_Array = new double[CountDots];
- get_equations_massiv(&Func2, &Func1, CountDots, coeffs_massiv, x_Array, y_Array);*/
- coeffs_massiv[0][0] = 1;
- coeffs_massiv[0][1] = 0;
- coeffs_massiv[0][2] = 1;
- coeffs_massiv[1][0] = 0;
- coeffs_massiv[1][1] = 1;
- coeffs_massiv[1][2] = 1;
- coeffs_massiv[2][0] = 1;
- coeffs_massiv[2][1] = 0;
- coeffs_massiv[2][2] = 0;
- y_Array[0] = 1;
- y_Array[1] = 2;
- y_Array[2] = 3;
- Gauss_with_vibor(coeffs_massiv, y_Array, CountDots);
- cout << 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;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement