Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class RungeSecondOrder{
- double x0, y0, h, no_of_intervals;
- public:
- RungeSecondOrder(double = 0, double = 0, double = 0, int = 0);
- double f(double, double);
- double calcY(double, double);
- double displayAll();
- };
- inline double RungeSecondOrder::f(double x, double y){
- return y + (0 * x);
- }
- inline RungeSecondOrder::RungeSecondOrder(double h, double x0, double y0, int no_of_intervals){
- RungeSecondOrder::h = h;
- RungeSecondOrder::x0 = x0;
- RungeSecondOrder::y0 = y0;
- RungeSecondOrder::no_of_intervals = no_of_intervals;
- }
- inline double RungeSecondOrder::calcY(double x, double y){
- double k1 = h * f(x, y);
- double k2 = h * f(x + h, y + h);
- return y + (.5 * (k1 + k2));
- }
- double RungeSecondOrder::displayAll(){
- double curr_x = x0, curr_y = y0;
- cout << "x = " << curr_x << " y = " << curr_y << endl;
- for(double i = 1; i <= no_of_intervals; i++){
- curr_y = calcY(curr_x, curr_y);
- curr_x += h;
- cout << "x = " << curr_x << " y = " << curr_y << endl;
- }
- }
- int main() {
- system("cls");
- double x0, y0, h, no_of_intervals;
- cout << "Enter x0 = ";
- cin >> x0;
- cout << "Enter y0 = ";
- cin >> y0;
- cout << "Enter h = ";
- cin >> h;
- cout << "Enter number of intervals = ";
- cin >> no_of_intervals;
- RungeSecondOrder obj(h, x0, y0, no_of_intervals);
- obj.displayAll();
- cin.ignore();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment