Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <cmath>
- #include <math.h>
- #include <vector>
- #include <fstream>
- using namespace std;
- vector<double> funcY(int n, vector<double> x, vector<double> y)
- {
- for (int k = 0; k < n; k++){
- y[k] = sin(x[k]);
- }
- return y;
- }
- vector<double> funcX(int n, vector<double> x)
- {
- for (int k = 0; k < n; k++){
- x[k] = M_PI*k / 4 / n;
- }
- return x;
- }
- vector<double> Lagr(double point, vector<double> x)
- {
- vector<double> Lagra(x.size());
- for (int i = 0; i < x.size(); i++){
- Lagra[i] = 1;
- for (int j = 0; j <x.size(); j++){
- (j != i) ? Lagra[i] *= (point - x[j]) : 1;
- }
- }
- return Lagra;
- }
- double Interp(double point, vector<double> x, vector<double> y)
- {
- double InterY = 0;
- for (int i = 0; i < x.size(); i++){
- InterY += Lagr(point, x)[i] / Lagr(x[i], x)[i] * y[i];
- }
- return InterY;
- }
- void main()
- {
- ofstream fout("Task 6.txt");
- int n = 4;
- vector<double> x(n), y(n), Lagra(n);
- x = funcX(n, x);
- y = funcY(n, x, y);
- int N = 10;
- int add = 1;
- fout << "X" << endl;
- for (int k = 0; k < N+add; k++){
- if (k != (N +add - 1))
- {
- fout << M_PI*k / N / n << ", ";
- }
- else { fout << sin(M_PI*k / N / n); }
- }
- fout << endl<< "FUNC" << endl;
- for (int k = 0; k < N+add; k++){
- if (k != (N+add - 1))
- {
- fout << sin(M_PI*k / N / n) << ", ";
- }
- else { fout << sin(M_PI*k / N / n); }
- }
- fout << endl<<"INTERP" << endl;
- for (int k = 0; k < N+add; k++){
- if (k != (N+add - 1))
- {
- fout << Interp(M_PI*k / N / n, x, y) << ", ";
- }
- else { fout << Interp(M_PI*k / N / n, x, y); }
- }
- fout <<endl<< "DIFFERENCE" << endl;
- for (int k = 0; k < N+add; k++){
- if (k != (N +add- 1))
- {
- fout << Interp(M_PI*k / N / n, x, y) - sin(M_PI*k / N / n) << ", ";
- }
- else { fout << Interp(M_PI*k / N / n, x, y) - sin(M_PI*k / N / n); }
- }
- fout.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement