Runge-Kutta method implementation using CPP language for all order



                        ///Runge-Kutta method implementation using CPP.............


/*Md. Alamgir Hossain
Dept. of Computer Science & Engineering.
Jessore University of Science & Technology.
*/
#include<bits/stdc++.h>
using namespace std;

void senond_order(float x,float y)
{
    cout<<"The result for Second Order is : \n"<<(x+y);
}
void third_order(float x,float y)
{
    cout<<"The result for Third Order is : \n"<<(x+y);
}
void fourth_order(float x,float y)
{
    cout<<"The result for Fourth Order is : \n"<<(x+y);
}
float func(float x, float y)
{
    ///Enter he function(dy/dx) in here..........
    //return ((x*x)*(y*y));

    float res = ((2*x*y)+exp(x))/((x*x)+(x*exp(x)));
    return res;
}

int main()
{
    float h = 0.0;
    cout<<"Enter the value of x0 : ";
    float x0;
    cin>>x0;

    cout<<"Enter the value of y0 : ";
    float y0;
    cin>>y0;

    cout<<"Inter the value of x(n) : ";
    float xn;
    cin>>xn;
    cout<<"If you want to enter the value of h press 1 otherwise press any key....";
    float pr;
    cin>>pr;
    float h2;
    if(pr==1){
        cout<<"\nEnter the value of h : ";
        cin>>h2;
        h=h2;
    }
    else{
        h = xn-x0;
    }

    float k1,k2;
    k1 = h*func(x0,y0);
    k2 = h*func((x0+(h/2)),(y0+(k1/2)));


    cout<<"Press 2 for 2nd order, 3 for 3rd order, 4 for 4th order....."<<endl;
    int pv;
    cin>>pv;
    if(pv==2){
        senond_order(y0,k2);
    }
    if(pv==3){
        float k3 = h*func((x0+h),(y0+2*k2-k1));
        float dh = (k1 + 4*k2 + k3)/6;

        third_order(y0,dh);
    }
     if(pv==4){
        float k3 = h*func((x0+(h/2)),(y0+(k2/2)));
        float k4 = h*func(x0+h,y0+k3);
        float dh = (k1 + 2*k2 + 2*k3 + k4)/6;

        fourth_order(y0,dh);
    }

    return 0;
}

No comments:

Post a Comment