Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- inline double function(double x, double y)
- {
- return x + cos(y/sqrt(5));
- }
- void Euler(const double x0, const double y0, const double x1, const double h)
- {
- double x = x0, y = y0;
- cout << " Euler's method\nx = " << x << "\t y = " << y << endl;
- while (x <= x1)
- {
- double f = function(x, y); //визначення приросту
- y += h*f;
- x += h;
- cout << "x = " << x << "\t y = " << y << endl;
- }
- }
- void Runge_Kutta(const double x0, const double y0, const double x1, const double h)
- {
- double x = x0, y = y0;
- cout << "\nRunge-Kutta's method\nx = " << x << "\t y = " << y << endl;
- while (x <= x1)
- {
- double k1 = h* function(x,y); //визначення коефіцієнтів
- double k2 = h * function(x+h/2, y+k1/2);
- double k3 = h * function(x+h/2, y+k2/2);
- double k4 = h * function(x+h, y+k3);
- double f = (k1+2*k2+2*k3+k4)/6; //визначення приросту
- y += f;
- x += h;
- cout << "x = " << x << "\t y = " << y << endl;
- }
- }
- void Adams(const double x0, const double y0, const double x, const double h)
- {
- double x4 = x0, y = y0, y4 = y0, x1 = x4+3*h, x2 = x4 + 2 * h, x3 = x4 + h;
- double y3 = y4 + h * function(x4, y4);
- double y2 = y3 + h*(3*function(x3, y3) - function(x4, y4))/2;
- double y1 = y2 + h*(23*function(x2,y2)-16*function(x3, y3) + 5*(x4, y4))/12;
- cout << "\n Adams method\nx = " << x4 << "\t y = " << y4 << endl;
- cout << "x = " << x3 << "\t y = " << y3 << endl;
- cout << "x = " << x2 << "\t y = " << y2 << endl;
- cout << "x = " << x1 << "\t y = " << y1 << endl;
- while (x1 <= x)
- {
- y = y1 + h*(55*function(x1,y1)-59*function(x2,y2)+37*function(x3,y3)-9*function(x4,y4))/24;
- y4 = y3;
- y3 = y2;
- y2 = y1;
- y1 = y;
- x4 = x3;
- x3 = x2;
- x2 = x1;
- x1 += h;
- cout << "x = " << x1 << "\t y = " << y << endl;
- }
- }
- int main()
- {
- const double x0 = 1.8;
- const double y0 = 2.6;
- const double x1 = 2.8;
- const int n = 10;
- const double h = (x1 - x0) / n;
- Euler(x0, y0, x1, h);
- Runge_Kutta(x0, y0, x1, h);
- Adams(x0, y0, x1, h);
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment