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();
- }
- //Запись в 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* 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);
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment