Euler Method for solving differential equation in C++


In this problem, we are given a differential equation f(x, y) = dy/dx with initial value y(x0) = y0. Our task is to find the solution of the equation using the Euler method for solving differential equations.

EULER METHOD

Euler method also known as forward euler Method is a first order numerical procedure to find the solution of the given differential equation using the given initial value.

For a differential equation f(x, y) = dy / dx. Euler method is defined as,

y(n+1) = y(n) + h * f( x(n), y(n) )

The value h is step size which is calculated as,

h = (x(n) - x(0)) / n

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

float equation(float x, float y) {

   return (x + y);
}

void solveEquationEulers(float x0, float y, float h, float x) {

   float temp = 0.0;

   while (x0 < x) {
      temp = y;
      y = y + h * equation(x0, y);
      x0 = x0 + h;
   }
   cout<<"The solution of the differential equation at x = "<< x <<" is f(x, y) = "<<y;
}

int main()
{
   float x0 = 0;
   float y0 = 1;
   float h = 0.5;
   float x = 0.1;
   solveEquationEulers(x0, y0, h, x);
   return 0;
}

Output −

The solution of the differential equation at x = 0.1 is f(x, y) = 1.5

Updated on: 22-Jan-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements