fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. using std::cout;
  6. using std::endl;
  7. using std::ofstream;
  8. using std::ios;
  9.  
  10. long double *Exact(double *x, const int size) {
  11. long double *f = new long double[size];
  12. for (int i = 0; i < size; ++i) {
  13. f[i] = exp(x[i] + sin(x[i]));
  14. }
  15. return f;
  16. }
  17.  
  18. double *Intervals(double a, double h, const int size) {
  19. double *x = new double[size];
  20. h /= 10;
  21. for (int i = 0; i < size; ++i) {
  22. a += h;
  23. x[i] = a;
  24. }
  25. return x;
  26. }
  27.  
  28. long double *LSpline(double *x, long double *f, const int size) {
  29. long double *l = new long double[size];
  30. l[0] = f[0];
  31. for (int a = 0; a < size - 1; a += 10) {
  32. int b = a + 10;
  33. long double denominator = x[b] - x[a];
  34. if (denominator != 0) {
  35. long double s = (f[a] - f[b]) / denominator;
  36. for (int i = a; i <= b; ++i) {
  37. l[i] = f[b] + (x[b] - x[i]) * s;
  38. }
  39. }
  40. }
  41. return l;
  42. }
  43.  
  44. void Out(long double *l, double *x, long double *f, const int size) {
  45. cout.setf(ios::left);
  46. cout << "x F(x) L2\n";
  47. for (int i = 0; i < size; i++) {
  48. cout.width(7);
  49. cout.precision(2);
  50. cout << x[i];
  51. cout.width(10);
  52. cout.precision(4);
  53. cout << f[i];
  54. cout.width(10);
  55. cout << l[i];
  56. cout << endl;
  57. }
  58. cout << endl;
  59. }
  60.  
  61. void FileOut(long double *l, double *x, long double *f, const int size) {
  62. cout << "test1\n";
  63. ofstream ffout("out.txt");
  64. cout << "test2\n";
  65. ffout << "x F(x) L2 R\n";
  66. for (int i = 0; i < size; i++) {
  67. ffout << x[i] << " " << f[i] << " " << l[i] << " " << fabs(f[i] - l[i])
  68. << endl;
  69. }
  70. ffout.close();
  71. }
  72.  
  73. int main() {
  74. std::cout << "a,h\n";
  75. const int a = -4;
  76. const int h = 1;
  77. const int size = 51;
  78.  
  79. double *x = Intervals(a, h, size);
  80. long double *f = Exact(x, size);
  81. long double *l = LSpline(x, f, size);
  82.  
  83. Out(l, x, f, size);
  84. FileOut(l, x, f, size);
  85. delete[] l;
  86. delete[] x;
  87. delete[] f;
  88. cout << "Finish";
  89. }
  90.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
a,h
x      F(x)      L2
-3.9   0.04027   0.04027   
-3.8   0.04125   0.04057   
-3.7   0.042     0.04088   
-3.6   0.04253   0.04118   
-3.5   0.04289   0.04149   
-3.4   0.04309   0.04179   
-3.3   0.04319   0.0421    
-3.2   0.04321   0.0424    
-3.1   0.04321   0.04271   
-3     0.04323   0.04301   
-2.9   0.04332   0.04332   
-2.8   0.0435    0.04479   
-2.7   0.04383   0.04626   
-2.6   0.04436   0.04774   
-2.5   0.04512   0.04921   
-2.4   0.04617   0.05069   
-2.3   0.04756   0.05216   
-2.2   0.04937   0.05364   
-2.1   0.05165   0.05511   
-2     0.05451   0.05658   
-1.9   0.05806   0.05806   
-1.8   0.06242   0.07083   
-1.7   0.06777   0.0836    
-1.6   0.07431   0.09637   
-1.5   0.08229   0.1091    
-1.4   0.09205   0.1219    
-1.3   0.104     0.1347    
-1.2   0.1186    0.1474    
-1.1   0.1365    0.1602    
-1     0.1586    0.173     
-0.9   0.1858    0.1858    
-0.8   0.2193    0.2893    
-0.7   0.2607    0.3928    
-0.6   0.312     0.4964    
-0.5   0.3755    0.5999    
-0.4   0.4541    0.7035    
-0.3   0.5513    0.807     
-0.2   0.6712    0.9106    
-0.1   0.8189    1.014     
-1.4e-181         1.118     
0.1    1.221     1.221     
0.2    1.49      1.832     
0.3    1.814     2.442     
0.4    2.202     3.052     
0.5    2.663     3.662     
0.6    3.205     4.273     
0.7    3.835     4.883     
0.8    4.56      5.493     
0.9    5.383     6.104     
1      6.306     6.714     
1.1    7.324     7.324     

test1
test2
Finish