fork(4) download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. double LP(double x, vector<double> xv, vector<double> yv) { //Lagrange polynomial
  6. int size = xv.size(); //Количество точек (для удобства)
  7. double sum = 0; //Значение функции
  8. for(int i = 0; i < size; i++){
  9. double mul = 1; //Произведение
  10. for(int j = 0; j < size; j++){
  11. if(i!=j) mul *= (x - xv[j])/(xv[i]-xv[j]);
  12. }
  13. sum += yv[i]*mul;
  14. }
  15. return sum;
  16. }
  17.  
  18. int main() {
  19. int counter = 0;
  20. double a = -3; //Левая грань
  21. double b = 3; //Правая грань
  22. int fragments = 101; //Разбиение
  23. vector<double> xv {};
  24. vector<double> yv {};
  25. double inp;
  26. while(cin >> inp) counter++%2 ? yv.push_back(inp) : xv.push_back(inp); //Заполнение
  27. for(int i = 0; i < fragments; i++){
  28. double x = a + (b-a)/(fragments-1)*i;
  29. printf("%6.3lf\t%6.3lf\n", x, LP(x, xv, yv));
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 3432KB
stdin
-1   1
 0   2
 1   3
 2  -1
stdout
-3.000	19.000
-2.940	17.787
-2.880	16.627
-2.820	15.518
-2.760	14.460
-2.700	13.453
-2.640	12.493
-2.580	11.581
-2.520	10.716
-2.460	 9.896
-2.400	 9.120
-2.340	 8.387
-2.280	 7.697
-2.220	 7.048
-2.160	 6.438
-2.100	 5.868
-2.040	 5.335
-1.980	 4.839
-1.920	 4.378
-1.860	 3.952
-1.800	 3.560
-1.740	 3.200
-1.680	 2.871
-1.620	 2.573
-1.560	 2.304
-1.500	 2.062
-1.440	 1.848
-1.380	 1.660
-1.320	 1.497
-1.260	 1.357
-1.200	 1.240
-1.140	 1.145
-1.080	 1.070
-1.020	 1.014
-0.960	 0.977
-0.900	 0.958
-0.840	 0.954
-0.780	 0.965
-0.720	 0.991
-0.660	 1.030
-0.600	 1.080
-0.540	 1.141
-0.480	 1.212
-0.420	 1.292
-0.360	 1.379
-0.300	 1.472
-0.240	 1.572
-0.180	 1.675
-0.120	 1.781
-0.060	 1.890
-0.000	 2.000
 0.060	 2.110
 0.120	 2.219
 0.180	 2.325
 0.240	 2.428
 0.300	 2.527
 0.360	 2.621
 0.420	 2.708
 0.480	 2.788
 0.540	 2.859
 0.600	 2.920
 0.660	 2.970
 0.720	 3.009
 0.780	 3.035
 0.840	 3.046
 0.900	 3.042
 0.960	 3.023
 1.020	 2.986
 1.080	 2.930
 1.140	 2.855
 1.200	 2.760
 1.260	 2.643
 1.320	 2.503
 1.380	 2.340
 1.440	 2.152
 1.500	 1.938
 1.560	 1.696
 1.620	 1.427
 1.680	 1.129
 1.740	 0.800
 1.800	 0.440
 1.860	 0.048
 1.920	-0.378
 1.980	-0.839
 2.040	-1.335
 2.100	-1.867
 2.160	-2.438
 2.220	-3.048
 2.280	-3.697
 2.340	-4.387
 2.400	-5.120
 2.460	-5.896
 2.520	-6.716
 2.580	-7.581
 2.640	-8.493
 2.700	-9.452
 2.760	-10.460
 2.820	-11.518
 2.880	-12.627
 2.940	-13.787
 3.000	-15.000