fork(1) 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 Improved Eullers 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 x0, y0, x, y, h, K1, K2, Z;
  26. cout.precision(4); //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;
  34. cout << "x" << setw(16) << "y" << setw(19) << "K1" << setw(16) << "Z" << setw(19) << "K2" << setw(16) << "y(i+1)\n";
  35. cout << "---------------------------------------------------------------------------------------------------------\n";
  36. while (fabs(x - x0) > 0.000001)
  37. {
  38. //y = y0 + (h*df(x0, y0)); //calculate next y
  39. K1 = df(x0, y0);
  40. Z = y0 + (h*df(x0, y0));
  41. K2 = df(x0 + h, Z);
  42. y = y0 + ((K1 + K2) / 2)*h;
  43. cout << x0 << setw(16) << y0 << setw(16) << K1 << setw(16) << Z << setw(16) << K2 << setw(16) << y << endl;
  44. y0 = y; //pass this new y as y0 in the next iteration.
  45. x0 = x0 + h; //calculate new x.
  46. }
  47. cout << x0 << setw(16) << y << endl;
  48. cout << "----------------------------------------------------------------------" << endl;
  49. cout << "y(" << x0 << ") = " << y << endl;
  50. break;
  51. }
  52. goto EndLable;
  53. EndLable:
  54. cout << "----------------------------------------------------------------------" << endl;
  55. cout << "Program will end so that you can enter the differential equation to be solved" << endl;
  56.  
  57. system("Pause");
  58. return 0;
  59. }
Success #stdin #stdout #stderr 0s 15240KB
stdin
y
-1
-0.5
2
0.05
stdout
This is the Improved Eullers 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:
x               y                 K1               Z                 K2         y(i+1)
---------------------------------------------------------------------------------------------------------
-1.0000         -0.5000         -0.6667         -0.5333         -0.5886         -0.5314
-0.9500         -0.5314         -0.5893         -0.5608         -0.5189         -0.5591
-0.9000         -0.5591         -0.5195         -0.5851         -0.4558         -0.5835
-0.8500         -0.5835         -0.4563         -0.6063         -0.3984         -0.6048
-0.8000         -0.6048         -0.3988         -0.6248         -0.3462         -0.6235
-0.7500         -0.6235         -0.3465         -0.6408         -0.2986         -0.6396
-0.7000         -0.6396         -0.2989         -0.6545         -0.2554         -0.6534
-0.6500         -0.6534         -0.2555         -0.6662         -0.2161         -0.6652
-0.6000         -0.6652         -0.2162         -0.6760         -0.1805         -0.6752
-0.5500         -0.6752         -0.1806         -0.6842         -0.1484         -0.6834
-0.5000         -0.6834         -0.1485         -0.6908         -0.1198         -0.6901
-0.4500         -0.6901         -0.1198         -0.6961         -0.0943         -0.6954
-0.4000         -0.6954         -0.0944         -0.7002         -0.0721         -0.6996
-0.3500         -0.6996         -0.0721         -0.7032         -0.0528         -0.7027
-0.3000         -0.7027         -0.0529         -0.7054         -0.0366         -0.7050
-0.2500         -0.7050         -0.0367         -0.7068         -0.0234         -0.7065
-0.2000         -0.7065         -0.0234         -0.7076         -0.0132         -0.7074
-0.1500         -0.7074         -0.0132         -0.7080         -0.0059         -0.7079
-0.1000         -0.7079         -0.0059         -0.7081         -0.0015         -0.7080
-0.0500         -0.7080         -0.0015         -0.7081         -0.0000         -0.7081
0.0000         -0.7081         -0.0000         -0.7081         -0.0015         -0.7081
0.0500         -0.7081         -0.0015         -0.7082         -0.0059         -0.7083
0.1000         -0.7083         -0.0059         -0.7086         -0.0132         -0.7088
0.1500         -0.7088         -0.0132         -0.7094         -0.0234         -0.7097
0.2000         -0.7097         -0.0234         -0.7109         -0.0365         -0.7112
0.2500         -0.7112         -0.0365         -0.7130         -0.0525         -0.7134
0.3000         -0.7134         -0.0525         -0.7160         -0.0714         -0.7165
0.3500         -0.7165         -0.0714         -0.7201         -0.0930         -0.7206
0.4000         -0.7206         -0.0930         -0.7253         -0.1174         -0.7259
0.4500         -0.7259         -0.1173         -0.7317         -0.1444         -0.7324
0.5000         -0.7324         -0.1443         -0.7396         -0.1739         -0.7404
0.5500         -0.7404         -0.1738         -0.7491         -0.2058         -0.7499
0.6000         -0.7499         -0.2057         -0.7602         -0.2400         -0.7610
0.6500         -0.7610         -0.2399         -0.7730         -0.2764         -0.7739
0.7000         -0.7739         -0.2762         -0.7877         -0.3146         -0.7887
0.7500         -0.7887         -0.3145         -0.8044         -0.3547         -0.8054
0.8000         -0.8054         -0.3545         -0.8231         -0.3963         -0.8242
0.8500         -0.8242         -0.3961         -0.8440         -0.4393         -0.8451
0.9000         -0.8451         -0.4390         -0.8670         -0.4834         -0.8681
0.9500         -0.8681         -0.4831         -0.8923         -0.5285         -0.8934
1.0000         -0.8934         -0.5281         -0.9198         -0.5743         -0.9210
1.0500         -0.9210         -0.5739         -0.9497         -0.6206         -0.9508
1.1000         -0.9508         -0.6202         -0.9819         -0.6673         -0.9830
1.1500         -0.9830         -0.6669         -1.0164         -0.7142         -1.0176
1.2000         -1.0176         -0.7137         -1.0532         -0.7610         -1.0544
1.2500         -1.0544         -0.7606         -1.0925         -0.8077         -1.0936
1.3000         -1.0936         -0.8072         -1.1340         -0.8540         -1.1352
1.3500         -1.1352         -0.8536         -1.1778         -0.9000         -1.1790
1.4000         -1.1790         -0.8995         -1.2240         -0.9454         -1.2251
1.4500         -1.2251         -0.9449         -1.2724         -0.9902         -1.2735
1.5000         -1.2735         -0.9897         -1.3230         -1.0342         -1.3241
1.5500         -1.3241         -1.0337         -1.3758         -1.0775         -1.3769
1.6000         -1.3769         -1.0770         -1.4307         -1.1200         -1.4318
1.6500         -1.4318         -1.1195         -1.4878         -1.1617         -1.4888
1.7000         -1.4888         -1.1612         -1.5469         -1.2024         -1.5479
1.7500         -1.5479         -1.2020         -1.6080         -1.2423         -1.6090
1.8000         -1.6090         -1.2418         -1.6711         -1.2813         -1.6721
1.8500         -1.6721         -1.2808         -1.7362         -1.3194         -1.7371
1.9000         -1.7371         -1.3189         -1.8031         -1.3566         -1.8040
1.9500         -1.8040         -1.3561         -1.8718         -1.3929         -1.8727
2.0000         -1.8727
----------------------------------------------------------------------
y(2.0000) = -1.8727
----------------------------------------------------------------------
Program will end so that you can enter the differential equation to be solved
stderr
sh: 1: Pause: not found