C++ Program for Euler’s Method to solve an ODE(Ordinary Differential Equation)

//Eulers Method to solve a differential equation
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double df(double x, double y)            //function for defining dy/dx
{
    double a=x+y;                //dy/dx=x+y
    return a;
}
int main()
{
    int n;   
    double x0,y0,x,y,h;            //for initial values, width, etc.
    cout.precision(5);            //for precision
    cout.setf(ios::fixed);               
    cout<<"\nEnter the initial values of x and y respectively:\n";        //Initial values
    cin>>x0>>y0;                           
    cout<<"\nFor what value of x do you want to find the value of y\n";   
    cin>>x;
    cout<<"\nEnter the width of the sub-interval:\n";            //input width
    cin>>h;
    cout<<"x"<<setw(19)<<"y"<<setw(19)<<"dy/dx"<<setw(16)<<"y_new\n";
    cout<<"----------------------------------------------------------\n";
    while(fabs(x-x0)>0.0000001)        //I couldn't just write "while(x0<x)" as they both are floating point nos. It is dangerous to compare two floating point nos. as they are not the same in binary as they are in decimal. For instance, a computer cannot exactly represent 0.1 or 0.7 in binary just like decimal can't represent 1/3 exactly without recurring digits.
    {   
        y=y0+(h*df(x0,y0));            //calculate new y, which is y0+h*dy/dx   
        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<<"The approximate value of y at x=0 is "<<y<<endl;        //print the solution.
    return 0;
}

For dy/dx=-2x-y
euler 2 output
For dy/dx=x+y
euler_output_1

Explanation of the Code:

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



3 thoughts on “C++ Program for Euler’s Method to solve an ODE(Ordinary Differential Equation)

  1. Hello
    My son teacher have told them to program a program in C++ which can solve non-homogenous problems in differential eq. I tried best to teach him but couldnt solve it

    Can i have a program or tutorial?

  2. Hey ! Do you work as a freelancer? I need help with a project. Send me your email.

Leave a Reply

Your email address will not be published. Required fields are marked *