Solving an ordinary differential equation (ODE) or initial value problem means finding a clear expression for y in terms of a finite number of elementary functions of x. Euler’s method is one of the simplest method for the numerical solution of such equation or problem. This C program for Euler’s method considers an ordinary differential equations, and the initial values of x and y are known.
Mathematically, here, the curve of solution is approximated by a sequence of short lines i.e. by the tangent line in each interval. (Derivation) Using these information, the value of the value of ‘yn’ corresponding to the value of ‘xn‘ is to determined by dividing the length (xn – x) into n strips.
Therefore, strip width= (xn – x)/n and xn=x0+ nh.
Again, if m be the slope of the curve at point, y1= y0 + m(x0 , yo)h.
Similarly, values of all the intermediate y can be found out.
Below is a source code for Euler’s method in C to solve the ordinary differential equation dy/dx = x+y. It asks for the value of of x0 , y0 ,xn and h. The value of slope at different points is calculated using the function ‘fun’.
The values of y are calculated in while loop which runs till the initial value of x is not equal to the final value. All the values of ‘y’ at corresponding ‘x’ are shown in the output screen.
dy/dx = x+y
Source Code for Euler’s Method in C:
float fun(float x,float y)
printf("\nEnter x0,y0,h,xn: ");
printf("\n x\t y\n");
Euler’s method is unarguably very very simple, but it cannot be considered as one of the best approach for to find the solution of initial value problems. It is considered to be very slow, and hence it was later modified in the name of Modified Euler’s Method.
The Euler’s method code code in this post needs to be compiled in Code::Blocks. It is well-tested and is bug-free. Any questions related to Euler’s method, or its source code in C presented above, can be mentioned and discussed in the comments.