fork download
  1. //#include "pch.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. using namespace std;
  6. double df(double x, double y) //function for defining dy/dx
  7. {
  8. double a = (x * x) / (y - 1); //========Enter the Differential Equation to be solved========================================
  9. return a;
  10.  
  11. }
  12.  
  13. int main()
  14. {
  15. char ch, ch1;
  16. cout << "This is the Runge Kutta Method calculator." << endl;
  17. cout << "Enter the differential equation to be solved in line 8 of the code" << endl;
  18. cout << "have you entered the right equation? (Enter Y or N)" << endl;
  19. cin >> ch;
  20. ch1 = ch;
  21. while (ch1 == 'y' || ch1 == 'Y') {
  22.  
  23.  
  24. int n;
  25. double h,x0, y0, x, y, k1, k2,k3, k4, z2, z3, z4;
  26. cout.precision(5); //sets number of decimal places to be displayed.
  27. cout.setf(ios::fixed); //displays floating point numbers in standard notation
  28. cout << "\nEnter the initial values of x and y respectively:\n";
  29. cin >> x0 >> y0;
  30. cout << "\nFor what value of x do you want to find the value of y\n";
  31. cin >> x;
  32. cout << "\nEnter the step size h:\n";
  33. cin >> h; //set size to 0.05 for percision
  34. cout << "x0" << setw(16) << "y0" << setw(19) << "K1" << setw(16) << "k2" << setw(19) << "K3" << setw(16) << "k4" << setw(16)<< "y(0+1)\n";
  35. cout << "---------------------------------------------------------------------------------------------------------\n";
  36. while (fabs(x - x0) > 0.0000001)
  37. { //calculate next y
  38. k1 = df(x0, y0);
  39. z2 = y0 + (h / 2)*k1;
  40. k2 = df(x0 + (h / 2), z2);
  41. z3 = y0 + (h / 2)*k2;
  42. k3 = df(x0 + (h / 2), z3);
  43. z4 = y0 + h * k3;
  44. k4 = df(x0 + h, z4);
  45. y = y0 + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
  46.  
  47. cout << x0 << setw(16) << y0 << setw(16) << k1 << setw(16) << k2 << setw(16) << k3 << setw(16) << k4 << setw(19)<<y<< endl;
  48. y0 = y; //pass this new y as y0 in the next iteration.
  49. x0 = x0 + h; //calculate new x.
  50. }
  51. cout << x0 << setw(16) << y << endl;
  52. cout << "----------------------------------------------------------------------" << endl;
  53. cout << "y(" << x0 << ") = " << y << endl;
  54. break;
  55. }
  56. goto EndLable;
  57. EndLable:
  58. cout << "----------------------------------------------------------------------" << endl;
  59. cout << "Program will end so that you can enter the differential equation to be solved" << endl;
  60.  
  61. system("Pause");
  62. return 0;
  63. }
Success #stdin #stdout #stderr 0s 15240KB
stdin
y
-1
-0.5
2
0.05
stdout
This is the Runge Kutta Method calculator.
Enter the differential equation to be solved in line 8 of the code
have you entered the right equation? (Enter Y or N)

Enter the initial values of x and y respectively:

For what value of x do you want to find the value of y

Enter the step size h:
x0              y0                 K1              k2                 K3              k4         y(0+1)
---------------------------------------------------------------------------------------------------------
-1.00000        -0.50000        -0.66667        -0.62679        -0.62720        -0.58935           -0.53137
-0.95000        -0.53137        -0.58934        -0.55341        -0.55373        -0.51955           -0.55906
-0.90000        -0.55906        -0.51954        -0.48702        -0.48728        -0.45629           -0.58343
-0.85000        -0.58343        -0.45629        -0.42677        -0.42697        -0.39881           -0.60478
-0.80000        -0.60478        -0.39881        -0.37196        -0.37212        -0.34650           -0.62340
-0.75000        -0.62340        -0.34650        -0.32206        -0.32218        -0.29887           -0.63951
-0.70000        -0.63951        -0.29887        -0.27664        -0.27674        -0.25554           -0.65336
-0.65000        -0.65336        -0.25554        -0.23535        -0.23542        -0.21620           -0.66513
-0.60000        -0.66513        -0.21620        -0.19792        -0.19797        -0.18059           -0.67504
-0.55000        -0.67504        -0.18059        -0.16411        -0.16415        -0.14852           -0.68325
-0.50000        -0.68325        -0.14852        -0.13375        -0.13378        -0.11983           -0.68995
-0.45000        -0.68995        -0.11983        -0.10669        -0.10671        -0.09438           -0.69529
-0.40000        -0.69529        -0.09438        -0.08284        -0.08285        -0.07208           -0.69944
-0.35000        -0.69944        -0.07208        -0.06209        -0.06210        -0.05286           -0.70255
-0.30000        -0.70255        -0.05286        -0.04438        -0.04439        -0.03666           -0.70477
-0.25000        -0.70477        -0.03666        -0.02968        -0.02968        -0.02344           -0.70626
-0.20000        -0.70626        -0.02344        -0.01794        -0.01794        -0.01318           -0.70717
-0.15000        -0.70717        -0.01318        -0.00915        -0.00915        -0.00586           -0.70763
-0.10000        -0.70763        -0.00586        -0.00329        -0.00329        -0.00146           -0.70780
-0.05000        -0.70780        -0.00146        -0.00037        -0.00037        -0.00000           -0.70783
0.00000        -0.70783        -0.00000        -0.00037        -0.00037        -0.00146           -0.70785
0.05000        -0.70785        -0.00146        -0.00329        -0.00329        -0.00585           -0.70802
0.10000        -0.70802        -0.00585        -0.00915        -0.00915        -0.01317           -0.70848
0.15000        -0.70848        -0.01317        -0.01792        -0.01792        -0.02340           -0.70939
0.20000        -0.70939        -0.02340        -0.02961        -0.02960        -0.03653           -0.71087
0.25000        -0.71087        -0.03653        -0.04418        -0.04417        -0.05254           -0.71309
0.30000        -0.71309        -0.05254        -0.06161        -0.06160        -0.07138           -0.71617
0.35000        -0.71617        -0.07138        -0.08186        -0.08184        -0.09301           -0.72027
0.40000        -0.72027        -0.09301        -0.10486        -0.10484        -0.11736           -0.72552
0.45000        -0.72552        -0.11736        -0.13054        -0.13051        -0.14434           -0.73205
0.50000        -0.73205        -0.14434        -0.15880        -0.15877        -0.17385           -0.74000
0.55000        -0.74000        -0.17385        -0.18954        -0.18950        -0.20578           -0.74948
0.60000        -0.74948        -0.20578        -0.22263        -0.22257        -0.23997           -0.76061
0.65000        -0.76061        -0.23997        -0.25791        -0.25784        -0.27629           -0.77351
0.70000        -0.77351        -0.27629        -0.29523        -0.29515        -0.31455           -0.78827
0.75000        -0.78827        -0.31455        -0.33440        -0.33431        -0.35457           -0.80499
0.80000        -0.80499        -0.35457        -0.37524        -0.37513        -0.39616           -0.82376
0.85000        -0.82376        -0.39616        -0.41754        -0.41742        -0.43911           -0.84463
0.90000        -0.84463        -0.43911        -0.46110        -0.46097        -0.48322           -0.86769
0.95000        -0.86769        -0.48322        -0.50571        -0.50556        -0.52827           -0.89297
1.00000        -0.89297        -0.52827        -0.55117        -0.55100        -0.57406           -0.92053
1.05000        -0.92053        -0.57406        -0.59726        -0.59708        -0.62039           -0.95038
1.10000        -0.95038        -0.62039        -0.64379        -0.64360        -0.66707           -0.98257
1.15000        -0.98257        -0.66706        -0.69057        -0.69037        -0.71390           -1.01709
1.20000        -1.01709        -0.71390        -0.73743        -0.73722        -0.76073           -1.05396
1.25000        -1.05396        -0.76073        -0.78420        -0.78398        -0.80739           -1.09316
1.30000        -1.09316        -0.80739        -0.83073        -0.83050        -0.85375           -1.13469
1.35000        -1.13469        -0.85375        -0.87690        -0.87666        -0.89969           -1.17853
1.40000        -1.17853        -0.89969        -0.92258        -0.92234        -0.94509           -1.22465
1.45000        -1.22465        -0.94509        -0.96768        -0.96744        -0.98987           -1.27303
1.50000        -1.27303        -0.98987        -1.01212        -1.01187        -1.03395           -1.32363
1.55000        -1.32363        -1.03394        -1.05582        -1.05557        -1.07726           -1.37641
1.60000        -1.37641        -1.07725        -1.09873        -1.09848        -1.11975           -1.43134
1.65000        -1.43134        -1.11975        -1.14081        -1.14056        -1.16140           -1.48837
1.70000        -1.48837        -1.16140        -1.18202        -1.18178        -1.20218           -1.54747
1.75000        -1.54747        -1.20217        -1.22235        -1.22211        -1.24206           -1.60858
1.80000        -1.60858        -1.24206        -1.26178        -1.26154        -1.28104           -1.67166
1.85000        -1.67166        -1.28104        -1.30031        -1.30008        -1.31913           -1.73666
1.90000        -1.73666        -1.31912        -1.33794        -1.33772        -1.35632           -1.80355
1.95000        -1.80355        -1.35631        -1.37469        -1.37447        -1.39262           -1.87228
2.00000        -1.87228
----------------------------------------------------------------------
y(2.00000) = -1.87228
----------------------------------------------------------------------
Program will end so that you can enter the differential equation to be solved
stderr
sh: 1: Pause: not found