Euler Method – Algorithm, Implementation in C With Solved Examples

Numerical Methods & Algorithms / Tuesday, October 23rd, 2018

Euler Method for Differential Equation

Euler method is the most simple but crude method to solve differential equation of the form

\[\frac{dy}{dx}=f\left( x,y \right),~~~y\left( {{x}_{0}} \right)={{y}_{0}}\]

Let x1 = x0 +h, where h is small. Then by Taylor’s series

\[{{y}_{1}}=y\left( {{x}_{0}}+h \right)={{y}_{0}}+h{{\left( \frac{dy}{dx} \right)}_{{{x}_{0}}}}+\frac{{{h}^{2}}}{2}{{\left( \frac{{{d}^{2}}y}{d{{x}^{2}}} \right)}_{{{c}_{1}}}}\]


\[{{y}_{1}}={{y}_{0}}+hf\left( {{x}_{0}},{{y}_{0}} \right)+\frac{{{h}^{2}}}{2}{{y}^{”}}\left( {{c}_{1}} \right)\]

If the step size h is chosen small enough, then the second-order term may be neglected and hence y1 is given by

\[{{y}_{1}}={{y}_{0}}+hf\left( {{x}_{0}},{{y}_{0}} \right)\]


\[{{y}_{2}}={{y}_{1}}+hf\left( {{x}_{1}},{{y}_{1}} \right)\]

\[{{y}_{3}}={{y}_{2}}+hf\left( {{x}_{2}},{{y}_{2}} \right)~~~and~so~on.\]

In general,

\[{{y}_{n+1}}={{y}_{n}}+hf\left( {{x}_{n}},{{y}_{n}} \right),~n=0,1,2,….\]

This method is very slow. To get a reasonable accuracy with Euler’s methods, the value of h should be taken as small.

Algorithm of Euler’s Method

This algorithm finds the solution of the equation

\[{{y}^{‘}}=f\left( x,y \right),~~~y\left( {{x}_{0}} \right)={{y}_{0}}~~~over~the~\text{interval}~~~\left[ {{x}_{0}},{{x}_{n}} \right]\]

By Euler’s method

\[{{y}_{i+1}}={{y}_{i}}+hf\left( {{x}_{i}},{{y}_{i}} \right),~i=0,1,2,….,n-1.\]

Step 1. Start;
Step 2. Input function f(x, y);
Step 3. Read x0, y0, xn, h;
/* x0, y0 are initial values and xn is the last value of x
   where the process will terminate, h is the step size */
Step 4. for x = x0 to xn step h do
			y = y0 + h * f(x, y0);
			Print x, y;
			y0 = y;
Step 5. Stop;

Euler’s Method implementation in C


/* Program Euler
   Solution of a differential equation of the form y' = f(x, y),
   y(x0) = y0 by Euler's method */


void main()
	float x0,y0,xn,h,x,y;
	float f(float x, float y);
	printf("Enter the initial (x0) and final (xn) values of x: ");
	printf("Enter initial value of y: ");
	printf("Enter the step length h: ");
	printf(" x-value \t y-value ");
		printf("%f \t %f \n",x+h,y);

//definition of the function f(x, y)
float f(float x, float y)
	return (x*x+x*y+2);


Enter the initial (x0) and final (xn) values of x: 0 0.2

Enter initial value of y: 1

Enter the step length h: 0.5

x-value                y-value

0.050000            1.100000

0.100000            1.202875

0.150000            1.309389

0.200000            1.420335

 Example 01

Solve the differential equation by Euler’s method

\[\frac{dy}{dx}=1-y,y\left( 0 \right)=0\]

Find y at x = 0.1, 0.2


\[Here~~~f\left( x,y \right)=1-y,~~~{{x}_{0}}=0,{{y}_{0}}=0\]

Taking h = 0.1, we have by Euler’s method

\[{{y}_{1}}={{y}_{0}}+hf\left( {{x}_{0}},{{y}_{0}} \right)=0+0.1\left( 1-0 \right)=0.1\]

\[\therefore y\left( 0.1 \right)=0.1\]

Again, we have by Euler’s method

\[{{y}_{2}}={{y}_{1}}+hf\left( {{x}_{1}},{{y}_{1}} \right)=0.1+0.1f\left( 0.1,0.1 \right)\]

\[\Rightarrow {{y}_{2}}=0.1+0.1\left( 1-0.1 \right)=0.19\]

\[\therefore y\left( 0.2 \right)=0.19\]

 Example 02

Find y(0.10) and y(0.15) by Euler’s method from the differential equation

\[\frac{dy}{dx}={{x}^{2}}+{{y}^{2}},y\left( 0 \right)=0\]

Correct upto 4 decimal places, taking step h = 0.05.


\[Here~~~f\left( x,y \right)={{x}^{2}}+{{y}^{2}},~~~{{x}_{0}}=0,{{y}_{0}}=0,h=0.05\]

Therefore by Euler’s method we have

\[{{y}_{1}}={{y}_{0}}+hf\left( {{x}_{0}},{{y}_{0}} \right)\]

\[\Rightarrow {{y}_{1}}=0+0.05f(0,0)=0+0.5\times 0\]

\[\therefore {{y}_{1}}=0\]

\[{{y}_{2}}={{y}_{1}}+hf\left( {{x}_{1}},{{y}_{1}} \right)\]

\[\Rightarrow {{y}_{2}}=0+0.05f\left( 0.05,0 \right)=0+0.05\left( 0.0025+0 \right)\]

\[\therefore y\left( 0.10 \right)=0.000125\]


\[{{y}_{3}}={{y}_{2}}+hf\left( {{x}_{2}},{{y}_{2}} \right)\]

\[\Rightarrow {{y}_{3}}=0.000125+0.05f\left( 0.10,0.000125 \right)\]

\[\Rightarrow {{y}_{3}}=0.000125+0.05\left\{ {{\left( 0.10 \right)}^{2}}+{{\left( 0.000125 \right)}^{2}} \right\}\]

\[\therefore y\left( 0.15 \right)=0.000625\]


<< Previous     Next>>


Leave a Reply

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