//#include "pch.h" #include<iostream> #include<iomanip> #include<cmath> using namespace std; double df(double x, double y) //function for defining dy/dx { double a = (x * x)/(y-1); //Enter the Differential Equation to be solved return a; } int main() { char ch, ch1; cout << "This is the Eullers Method calculator." << endl; cout << "Enter the differential equation to be solved in line 8 of the code." << endl; cout<<"have you entered the right equation? (Enter Y or N)" << endl; cin >> ch; ch1 = ch; while (ch1 == 'y' || ch1 == 'Y') { int n; double x0, y0, x, y, h; cout.precision(4); //sets number of decimal places to be displayed. cout.setf(ios::fixed); //displays floating point numbers in standard notation cout << "\nEnter the initial values of x and y respectively:\n"; cin >> x0 >> y0; cout << "\nFor what value of x do you want to find the value of y\n"; cin >> x; cout << "\nEnter the step size h:\n"; cin >> h; //Step size of 0.0005 for percision cout << "x" << setw(19) << "y" << setw(19) << " k " << setw(16) << "y(i+1)\n"; cout << "----------------------------------------------------------\n"; while (fabs(x - x0) > 0.000001) { y = y0 + (h*df(x0, y0)); //calculate next y cout << x0 << setw(16) << y0 << setw(16) << df(x0, y0) << setw(16) << y << endl; y0 = y; //pass this new y as y0 in the next iteration. x0 = x0 + h; //calculate new x. } cout << x0 << setw(16) << y << endl; cout << "----------------------------------------------------------------------" << endl; cout << "y(" << x0 << ") = " << y << endl; break; } goto EndLable; EndLable: cout << "----------------------------------------------------------------------" << endl; cout << "Program will end so that you may enter a different equation in line 8 of the program\n" << endl; return 0; }
y -1 -0.5 2 0.025
This is the 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 k y(i+1) ---------------------------------------------------------- -1.0000 -0.5000 -0.6667 -0.5167 -0.9750 -0.5167 -0.6268 -0.5323 -0.9500 -0.5323 -0.5890 -0.5471 -0.9250 -0.5471 -0.5531 -0.5609 -0.9000 -0.5609 -0.5189 -0.5739 -0.8750 -0.5739 -0.4865 -0.5860 -0.8500 -0.5860 -0.4555 -0.5974 -0.8250 -0.5974 -0.4261 -0.6081 -0.8000 -0.6081 -0.3980 -0.6180 -0.7750 -0.6180 -0.3712 -0.6273 -0.7500 -0.6273 -0.3457 -0.6359 -0.7250 -0.6359 -0.3213 -0.6440 -0.7000 -0.6440 -0.2981 -0.6514 -0.6750 -0.6514 -0.2759 -0.6583 -0.6500 -0.6583 -0.2548 -0.6647 -0.6250 -0.6647 -0.2347 -0.6706 -0.6000 -0.6706 -0.2155 -0.6759 -0.5750 -0.6759 -0.1973 -0.6809 -0.5500 -0.6809 -0.1800 -0.6854 -0.5250 -0.6854 -0.1635 -0.6895 -0.5000 -0.6895 -0.1480 -0.6932 -0.4750 -0.6932 -0.1333 -0.6965 -0.4500 -0.6965 -0.1194 -0.6995 -0.4250 -0.6995 -0.1063 -0.7021 -0.4000 -0.7021 -0.0940 -0.7045 -0.3750 -0.7045 -0.0825 -0.7065 -0.3500 -0.7065 -0.0718 -0.7083 -0.3250 -0.7083 -0.0618 -0.7099 -0.3000 -0.7099 -0.0526 -0.7112 -0.2750 -0.7112 -0.0442 -0.7123 -0.2500 -0.7123 -0.0365 -0.7132 -0.2250 -0.7132 -0.0295 -0.7140 -0.2000 -0.7140 -0.0233 -0.7145 -0.1750 -0.7145 -0.0179 -0.7150 -0.1500 -0.7150 -0.0131 -0.7153 -0.1250 -0.7153 -0.0091 -0.7155 -0.1000 -0.7155 -0.0058 -0.7157 -0.0750 -0.7157 -0.0033 -0.7158 -0.0500 -0.7158 -0.0015 -0.7158 -0.0250 -0.7158 -0.0004 -0.7158 0.0000 -0.7158 -0.0000 -0.7158 0.0250 -0.7158 -0.0004 -0.7158 0.0500 -0.7158 -0.0015 -0.7159 0.0750 -0.7159 -0.0033 -0.7159 0.1000 -0.7159 -0.0058 -0.7161 0.1250 -0.7161 -0.0091 -0.7163 0.1500 -0.7163 -0.0131 -0.7166 0.1750 -0.7166 -0.0178 -0.7171 0.2000 -0.7171 -0.0233 -0.7177 0.2250 -0.7177 -0.0295 -0.7184 0.2500 -0.7184 -0.0364 -0.7193 0.2750 -0.7193 -0.0440 -0.7204 0.3000 -0.7204 -0.0523 -0.7217 0.3250 -0.7217 -0.0613 -0.7233 0.3500 -0.7233 -0.0711 -0.7250 0.3750 -0.7250 -0.0815 -0.7271 0.4000 -0.7271 -0.0926 -0.7294 0.4250 -0.7294 -0.1044 -0.7320 0.4500 -0.7320 -0.1169 -0.7349 0.4750 -0.7349 -0.1300 -0.7382 0.5000 -0.7382 -0.1438 -0.7418 0.5250 -0.7418 -0.1582 -0.7457 0.5500 -0.7457 -0.1733 -0.7501 0.5750 -0.7501 -0.1889 -0.7548 0.6000 -0.7548 -0.2052 -0.7599 0.6250 -0.7599 -0.2220 -0.7655 0.6500 -0.7655 -0.2393 -0.7714 0.6750 -0.7714 -0.2572 -0.7779 0.7000 -0.7779 -0.2756 -0.7848 0.7250 -0.7848 -0.2945 -0.7921 0.7500 -0.7921 -0.3139 -0.8000 0.7750 -0.8000 -0.3337 -0.8083 0.8000 -0.8083 -0.3539 -0.8172 0.8250 -0.8172 -0.3746 -0.8265 0.8500 -0.8265 -0.3956 -0.8364 0.8750 -0.8364 -0.4169 -0.8468 0.9000 -0.8468 -0.4386 -0.8578 0.9250 -0.8578 -0.4606 -0.8693 0.9500 -0.8693 -0.4828 -0.8814 0.9750 -0.8814 -0.5053 -0.8940 1.0000 -0.8940 -0.5280 -0.9072 1.0250 -0.9072 -0.5509 -0.9210 1.0500 -0.9210 -0.5739 -0.9353 1.0750 -0.9353 -0.5971 -0.9503 1.1000 -0.9503 -0.6204 -0.9658 1.1250 -0.9658 -0.6438 -0.9819 1.1500 -0.9819 -0.6673 -0.9986 1.1750 -0.9986 -0.6908 -1.0158 1.2000 -1.0158 -0.7143 -1.0337 1.2250 -1.0337 -0.7379 -1.0521 1.2500 -1.0521 -0.7614 -1.0712 1.2750 -1.0712 -0.7849 -1.0908 1.3000 -1.0908 -0.8083 -1.1110 1.3250 -1.1110 -0.8317 -1.1318 1.3500 -1.1318 -0.8549 -1.1532 1.3750 -1.1532 -0.8781 -1.1751 1.4000 -1.1751 -0.9011 -1.1976 1.4250 -1.1976 -0.9240 -1.2207 1.4500 -1.2207 -0.9468 -1.2444 1.4750 -1.2444 -0.9694 -1.2686 1.5000 -1.2686 -0.9918 -1.2934 1.5250 -1.2934 -1.0140 -1.3188 1.5500 -1.3188 -1.0361 -1.3447 1.5750 -1.3447 -1.0580 -1.3711 1.6000 -1.3711 -1.0796 -1.3981 1.6250 -1.3981 -1.1011 -1.4257 1.6500 -1.4257 -1.1224 -1.4537 1.6750 -1.4537 -1.1434 -1.4823 1.7000 -1.4823 -1.1642 -1.5114 1.7250 -1.5114 -1.1848 -1.5410 1.7500 -1.5410 -1.2052 -1.5712 1.7750 -1.5712 -1.2254 -1.6018 1.8000 -1.6018 -1.2453 -1.6329 1.8250 -1.6329 -1.2650 -1.6646 1.8500 -1.6646 -1.2845 -1.6967 1.8750 -1.6967 -1.3037 -1.7293 1.9000 -1.7293 -1.3227 -1.7623 1.9250 -1.7623 -1.3415 -1.7959 1.9500 -1.7959 -1.3600 -1.8299 1.9750 -1.8299 -1.3784 -1.8643 2.0000 -1.8643 ---------------------------------------------------------------------- y(2.0000) = -1.8643 ---------------------------------------------------------------------- Program will end so that you may enter a different equation in line 8 of the program