fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. // Funkcja 1: exp(x) * cos(0.5 * x^2)
  7. double func1(double x) {
  8. return std::exp(x) * std::cos(0.5 * x * x);
  9. }
  10.  
  11. // Funkcja 2: 1 / (12 * x^2 + 1)
  12. double func2(double x) {
  13. return 1.0 / (12.0 * x * x + 1.0);
  14. }
  15.  
  16. // Generowanie węzłów Czebyszewa
  17. std::vector<double> chebyshevNodes(int degree, double a, double b) {
  18. std::vector<double> nodes(degree);
  19. for (int i = 0; i < degree; ++i) {
  20. double cos_theta = std::cos(M_PI * (2.0 * i + 1) / (2.0 * degree));
  21. nodes[i] = 0.5 * (a + b) + 0.5 * (b - a) * cos_theta;
  22. }
  23. return nodes;
  24. }
  25.  
  26. // Obliczanie współczynników wielomianu Czebyszewa
  27. std::vector<double> chebyshevCoefficients(int degree, double a, double b, double (*func)(double)) {
  28. std::vector<double> nodes = chebyshevNodes(degree, a, b);
  29. std::vector<double> coeffs(degree, 0.0);
  30.  
  31. for (int k = 0; k < degree; ++k) {
  32. double sum = 0.0;
  33. for (int j = 0; j < degree; ++j) {
  34. sum += func(nodes[j]) * std::cos(M_PI * k * (2.0 * j + 1) / (2.0 * degree));
  35. }
  36. coeffs[k] = (2.0 / degree) * sum;
  37. }
  38.  
  39. coeffs[0] *= 0.5; // Współczynnik dla T_0(x)
  40. return coeffs;
  41. }
  42.  
  43. // Obliczanie wartości wielomianu Czebyszewa
  44. double chebyshevPolynomial(const std::vector<double>& coeffs, double x, double a, double b) {
  45. double t = (2.0 * x - (a + b)) / (b - a); // Przekształcenie na [-1, 1]
  46. double y = 0.0;
  47. double T_k_minus_2 = 1.0;
  48. double T_k_minus_1 = t;
  49.  
  50. for (size_t k = 0; k < coeffs.size(); ++k) {
  51. if (k == 0) {
  52. y += coeffs[k] * T_k_minus_2;
  53. } else if (k == 1) {
  54. y += coeffs[k] * T_k_minus_1;
  55. } else {
  56. double T_k = 2.0 * t * T_k_minus_1 - T_k_minus_2;
  57. y += coeffs[k] * T_k;
  58. T_k_minus_2 = T_k_minus_1;
  59. T_k_minus_1 = T_k;
  60. }
  61. }
  62.  
  63. return y;
  64. }
  65.  
  66. int main() {
  67. // Parametry
  68. double a1 = 0.0, b1 = 3.0;
  69. double a2 = -2.0, b2 = 2.0;
  70. std::vector<int> degrees = {3, 10, 40};
  71.  
  72. // Pliki wynikowe
  73. std::ofstream file1("func1.dat");
  74. std::ofstream file2("func2.dat");
  75.  
  76. for (int degree : degrees) {
  77. std::vector<double> coeffs1 = chebyshevCoefficients(degree, a1, b1, func1);
  78. std::vector<double> coeffs2 = chebyshevCoefficients(degree, a2, b2, func2);
  79.  
  80. for (double x = a1; x <= b1; x += 0.01) {
  81. double approx1 = chebyshevPolynomial(coeffs1, x, a1, b1);
  82. file1 << x << " " << func1(x) << " " << approx1 << "\n";
  83. }
  84.  
  85. for (double x = a2; x <= b2; x += 0.01) {
  86. double approx2 = chebyshevPolynomial(coeffs2, x, a2, b2);
  87. file2 << x << " " << func2(x) << " " << approx2 << "\n";
  88. }
  89.  
  90. file1 << "\n\n";
  91. file2 << "\n\n";
  92. }
  93.  
  94. file1.close();
  95. file2.close();
  96.  
  97. std::cout << "Dane zapisane do plikow func1.dat i func2.dat. Wykresy mozna rysowac za pomoca Gnuplot." << std::endl;
  98. return 0;
  99. }
  100.  
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Dane zapisane do plikow func1.dat i func2.dat. Wykresy mozna rysowac za pomoca Gnuplot.