fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3.  
  4. #define n 100
  5.  
  6. double f(double, double);
  7.  
  8. double ya(double);
  9.  
  10. int main(void)
  11. {
  12. double a = 2, b = 4, h = (b-a)/n, x = a, y = b;
  13.  
  14. printf("%7.3lf %10.6lf %10.6lf\n",x,y,ya(x));
  15. while(x < b) // метод Рунге-Кутты
  16. {
  17.  
  18. double m1 = h*f(x,y);
  19. double m2 = h*f(x+h/2,y+m1/2);
  20. double m3 = h*f(x+h/2,y+m2/2);
  21. double m4 = h*f(x+h,y+m3);
  22. x += h;
  23. y += (m1 + 2*(m2+m3) + m4)/6;
  24.  
  25. printf("%7.3lf %10.6lf %10.6lf\n",x,y,ya(x));
  26. }
  27. }
  28.  
  29.  
  30. double f(double x, double y) // правая часть
  31.  
  32. {
  33. return (2*x-5)/(x*x)*y+5;
  34. }
  35.  
  36.  
  37. double ya(double x) // аналитическое решение
  38.  
  39. {
  40. return pow(x, 2);
  41. }
Success #stdin #stdout 0s 5520KB
stdin
Standard input is empty
stdout
  2.000     4.000000      4.000000
  2.020     4.080400      4.080400
  2.040     4.161600      4.161600
  2.060     4.243600      4.243600
  2.080     4.326400      4.326400
  2.100     4.410000      4.410000
  2.120     4.494400      4.494400
  2.140     4.579600      4.579600
  2.160     4.665600      4.665600
  2.180     4.752400      4.752400
  2.200     4.840000      4.840000
  2.220     4.928400      4.928400
  2.240     5.017600      5.017600
  2.260     5.107600      5.107600
  2.280     5.198400      5.198400
  2.300     5.290000      5.290000
  2.320     5.382400      5.382400
  2.340     5.475600      5.475600
  2.360     5.569600      5.569600
  2.380     5.664400      5.664400
  2.400     5.760000      5.760000
  2.420     5.856400      5.856400
  2.440     5.953600      5.953600
  2.460     6.051600      6.051600
  2.480     6.150400      6.150400
  2.500     6.250000      6.250000
  2.520     6.350400      6.350400
  2.540     6.451600      6.451600
  2.560     6.553600      6.553600
  2.580     6.656400      6.656400
  2.600     6.760000      6.760000
  2.620     6.864400      6.864400
  2.640     6.969600      6.969600
  2.660     7.075600      7.075600
  2.680     7.182400      7.182400
  2.700     7.290000      7.290000
  2.720     7.398400      7.398400
  2.740     7.507600      7.507600
  2.760     7.617600      7.617600
  2.780     7.728400      7.728400
  2.800     7.840000      7.840000
  2.820     7.952400      7.952400
  2.840     8.065600      8.065600
  2.860     8.179600      8.179600
  2.880     8.294400      8.294400
  2.900     8.410000      8.410000
  2.920     8.526400      8.526400
  2.940     8.643600      8.643600
  2.960     8.761600      8.761600
  2.980     8.880400      8.880400
  3.000     9.000000      9.000000
  3.020     9.120400      9.120400
  3.040     9.241600      9.241600
  3.060     9.363600      9.363600
  3.080     9.486400      9.486400
  3.100     9.610000      9.610000
  3.120     9.734400      9.734400
  3.140     9.859600      9.859600
  3.160     9.985600      9.985600
  3.180    10.112400     10.112400
  3.200    10.240000     10.240000
  3.220    10.368400     10.368400
  3.240    10.497600     10.497600
  3.260    10.627600     10.627600
  3.280    10.758400     10.758400
  3.300    10.890000     10.890000
  3.320    11.022400     11.022400
  3.340    11.155600     11.155600
  3.360    11.289600     11.289600
  3.380    11.424400     11.424400
  3.400    11.560000     11.560000
  3.420    11.696400     11.696400
  3.440    11.833600     11.833600
  3.460    11.971600     11.971600
  3.480    12.110400     12.110400
  3.500    12.250000     12.250000
  3.520    12.390400     12.390400
  3.540    12.531600     12.531600
  3.560    12.673600     12.673600
  3.580    12.816400     12.816400
  3.600    12.960000     12.960000
  3.620    13.104400     13.104400
  3.640    13.249600     13.249600
  3.660    13.395600     13.395600
  3.680    13.542400     13.542400
  3.700    13.690000     13.690000
  3.720    13.838400     13.838400
  3.740    13.987600     13.987600
  3.760    14.137600     14.137600
  3.780    14.288400     14.288400
  3.800    14.440000     14.440000
  3.820    14.592400     14.592400
  3.840    14.745600     14.745600
  3.860    14.899600     14.899600
  3.880    15.054400     15.054400
  3.900    15.210000     15.210000
  3.920    15.366400     15.366400
  3.940    15.523600     15.523600
  3.960    15.681600     15.681600
  3.980    15.840400     15.840400
  4.000    16.000000     16.000000