Optimistic(JAVADREAMS)

All kinds of program(short term) like as C,C++,Java,Perl,Software with java and database and the solution of computer problem you can search here..........

Alamgir Hossain

LightBlog

Breaking

Saturday, October 20, 2018

Tuesday, July 10, 2018

July 10, 2018

Eulers improved method implementation in cpp with equation input

#include<bits/stdc++.h>
using namespace std;
///You can use equations like-----
/*
    dy/dx = 2x+3y+5;
    dy/dx = 6x+10;
    dy/dx = 5y-10;
    dy/dx = 2x+3y;
*/
double solve(string str, double x, double y)

{
    // for finding x
    bool xflag = false;
    int a;
    for(int i=0; i<str.size(); i++){
        // x has a value
        if(str[i]=='x' || str[i]=='X'){
            xflag = true;
            if(i==0){
                // for the equation x + 6y + 3
                a = 1;
                break;
            }
            else if(str[i-1]=='y' || str[i-1]=='Y'){
                // for the equation yx + 6
                if(i-2==0){
                    a = 1;
                    break;
                }
                else{
                    a = 0;
                    for(int j=i-2, k=0; j>=0; j--, k++){
                        if(str[j]>='0' && str[j]<='9'){
                            int t = str[j] - '0';
                            a += (t*pow(10, k));
                        }
                        else{
                            if(str[j]=='-'){
                                a *= -1;
                            }
                            break;
                        }
                    }

                }

            }

            else{
                a = 0;
                for(int j=i-1, k=0; j>=0; j--, k++){
                    if(str[j]>='0' && str[j]<='9'){
                        int t = str[j] - '0';
                        a += (t*pow(10, k));
                    }
                    else{
                        if(str[j]=='-'){
                            a *= -1;
                        }
                        break;
                    }
                }
            }
        }
    }

    bool yflag = false;
    int b;

    for(int i=0; i<str.size(); i++){
        // x has a value
        if(str[i]=='y' || str[i]=='Y'){
            yflag = true;
            if(i==0){
                // for the equation y + 6x + 3
                b = 1;
                break;
            }
            else if(str[i-1]=='X' || str[i-1]=='x'){
                // for the equation xy + 6
                if(i-2==0){
                    b = 1;
                    break;
                }
                else{
                    b = 0;
                    for(int j=i-2, k=0; j>=0; j--, k++){

                        if(str[j]>='0' && str[j]<='9'){

                            int t = str[j] - '0';

                            b += (t*pow(10, k));

                        }

                        else{

                            if(str[j]=='-'){

                                b *= -1;

                            }

                            break;

                        }

                    }

                }

            }

            else{

                b = 0;

                for(int j=i-1, k=0; j>=0; j--, k++){

                    if(str[j]>='0' && str[j]<='9'){

                        int t = str[j] - '0';

                        b += (t*pow(10, k));
                    }
                    else{
                        if(str[j]=='-'){
                            b *= -1;
                        }
                        break;
                    }
                }
            }
        }
    }

    if(!xflag){
        a = 0;
    }

    if(!yflag){

        b = 0;

    }
    int c = 0;

    for(int i=str.size()-1, k=0; i>=0; i--, k++){

        if(str[i]>='0' && str[i]<='9'){

            int t = str[i] - '0';

            c += (t*pow(10, k));
        }
        else{
            if(str[i]=='-'){
                c *= -1;
            }
            break;
        }
    }

    //cout << a << endl;
    //cout << b << endl;
    //cout << c << endl;

    double ans = (a*x)+(b*y)+c;
    return ans;
}

int main()
{
    double x0,y0,h;
    string equation;
    cout<<"Enter the equation : ";
    cin>>equation;
    cout<<"Enter the value of X0 : ";
    cin>>x0;
    cout<<"Enter the value of Y0 : ";
    cin>>y0;

    cout<<"Enter the value of h : ";
    cin>>h;

    int n;

    cout<<"Enter how many y you wants : ";
    cin>>n;

    cout<<"\nSerial----X----------Y-----------------------------------------------------\n";

    cout<<"Y(1)     "<<x0<<"       "<<y0<<"\n";
    cout<<"----------------------------------------------------------------------------\n";
    for(int i=0;i<n-1;i++){
        double x,y,y1,y2;
        x = x0+h;
        y1 = solve(equation,x0,y0);

        y2 = solve(equation,x0+h,y0+(h*solve(equation,x0,y0)));

        y = y0+h*((y1+y2)/2);

        cout<<"Y("<<i+2<<")"<<"   "<<x<<"       "<<y<<"\n";
        cout<<"---------------------------------------------------------------------------\n";

        x0 = x;
        y0 = y;

    }
    return 0;
}

/*

Input Example---

Enter the equation : 2x+3y+5
Enter the value of X0 : 1
Enter the value of Y0 : 1
Enter the value of h : .1
Enter how many y you wants : 10

Serial----X----------Y-----------------------------------------------------
Y(1)     1       1
----------------------------------------------------------------------------
Y(2)   1.1       2.16
---------------------------------------------------------------------------
Y(3)   1.2       3.7432
---------------------------------------------------------------------------
Y(4)   1.3       5.8956
---------------------------------------------------------------------------
Y(5)   1.4       8.81359
---------------------------------------------------------------------------
Y(6)   1.5       12.7613
---------------------------------------------------------------------------
Y(7)   1.6       18.0939
---------------------------------------------------------------------------
Y(8)   1.7       25.2893
---------------------------------------------------------------------------
Y(9)   1.8       34.9901
---------------------------------------------------------------------------
Y(10)   1.9       48.0607
---------------------------------------------------------------------------
*/

Wednesday, July 4, 2018

July 04, 2018

Improved Eulers method implementation in CPP Language

/*
    Improved Eulers method implementation in CPP Language-----

    Md. Alamgir Hossain
    Department of Computer Science & Engineering.
    Jessore University of Science & Technology, Bangladesh.
    Email : alamgir.cse14.just@gmail.com
*/

#include<bits/stdc++.h>
using namespace std;
double equation(double x, double y)
{
    ///Here enter the right side of your differential equation---
    ///Some example of equation in the res variable--

    ///You can check with your desired equation
    ///From here remove comment and check for another equation...
    double res;
    ///Equation start
   // res = (3*x*x)+1;
    //res = (x*x)+(y*y);
    res = x*y;
   // res = y+(x*x);
   // res = ((2*y)/x)+(x*x*x);
    //res = y-((2*x)/y);

    return res;

}
int main()
{
    double x0,y0,h;

    cout<<"Enter the value of X0 : ";
    cin>>x0;
    cout<<"Enter the value of Y0 : ";
    cin>>y0;

    cout<<"Enter the value of h : ";
    cin>>h;

    int n;

    cout<<"Enter how many y you wants : ";
    cin>>n;

    cout<<"\nSerial----X----------Y-----------------------------------------------------\n";

    cout<<"Y(1)     "<<x0<<"       "<<y0<<"\n";
    cout<<"----------------------------------------------------------------------------\n";
    for(int i=0;i<n-1;i++){
        double x,y,y1,y2;
        x = x0+h;
        y1 = equation(x0,y0);

        y2 = equation(x0+h,y0+(h*equation(x0,y0)));

        y = y0+h*((y1+y2)/2);

        cout<<"Y("<<i+2<<")"<<"   "<<x<<"       "<<y<<"\n";
        cout<<"---------------------------------------------------------------------------\n";

        x0 = x;
        y0 = y;

    }
    return 0;
}

/*

Input Example---For the Equation y' = (x*y);

Enter the value of X0 : 1
Enter the value of Y0 : 1
Enter the value of h : .1
Enter how many y you wants : 10

Serial----X----------Y------------------------------------------------------
Y(1)     1       1
----------------------------------------------------------------------------
Y(2)   1.1       1.1105
---------------------------------------------------------------------------
Y(3)   1.2       1.24554
---------------------------------------------------------------------------
Y(4)   1.3       1.41094
---------------------------------------------------------------------------
Y(5)   1.4       1.61426
---------------------------------------------------------------------------
Y(6)   1.5       1.86528
---------------------------------------------------------------------------
Y(7)   1.6       2.17678
---------------------------------------------------------------------------
Y(8)   1.7       2.56555
---------------------------------------------------------------------------
Y(9)   1.8       3.05378
---------------------------------------------------------------------------
Y(10)   1.9       3.67095
---------------------------------------------------------------------------
*/