The analytical method of solution of differential equation is time consuming and tedious. Sometimes it fails to give accurate solution to complicated problems. In order to save time and labor, methods of numerical solution of differential equations such as Euler’s method, Modified Euler’s Method, Runge Kutta Method etc. have been developed.

Here, we are going to discuss a **C program for Modified Euler’s method** with its source code and sample output. The program is designed for numerical solution of ordinary differential equations or initial value problems in which all the initial conditions are specified.

The input data for **Modified Euler’s Method in C** given below are initial and final values of x i.e. x_{0 }and x_{n, }initial value of y i.e y_{0 }and the value of increment i.e h.

Here’s how the C source code for Modified Euler’s method works:

- When the program is executed, it checks the error and if the program is found to be error free, the program runs. Firstly, it asks the value of initial condition i.e. initial value of x and y, value of increment h, and final value of x.
- After getting the initial condition, this C program calls the defined function float fun(float,float) for the calculation of slope. The function for calculation of slope is:-

** y’ = x ^{2 }+ y**

- Then, value of x is increased by h i.e. x
_{1 }= x_{0 }+ h and so on. Using the new value of x and y, new slope is calculated. Mean slope of this newly calculated and initial slope is calculated. - The new value of y is : y= y
_{0}+ h * slope. The value of x is not increased till the any two consecutive value of y are found to be same or with the given limit of tolerance.

- The process is repeated till the initial value of x is not equal to the final value of x.
- Finally, the program prints the intermediate as well as final values of x and corresponding y. These values can be transferred to Curve Software to obtain curve and its equations.

## Source Code for Modified Euler’s Method in C:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include<stdio.h> #include<math.h> #include<string.h> float fun(float,float); main() { int i,j,c; float x[100],y[100],h,m[100],m1,m2,a,s[100],w; printf("\n C program for Modified Euler Method \n\n"); printf(" Enter the initial value of x:"); scanf("%f",&x[0]); printf("\n Enter the value of increment h:"); scanf("%f",&h); printf("\n Enter the final value of x:"); scanf("%f",&a); printf("\n Enter the initial value of the variable y :"); scanf("%f",&y[0]); s[0]=y[0]; for(i=1;x[i-1]<a;i++) { w=100.0; x[i]= x[i-1]+h; m[i]=fun(x[i-1],y[i-1]); c=0; while(w>0.0001) { m1=fun(x[i],s[c]); m2=(m[i]+m1)/2; s[c+1]=y[i-1]+m2*h; w=s[c]-s[c+1]; w=fabs(w); c=c+1; } y[i]=s[c]; } printf("\n\n The respective values of x and y are\n x \t y\n\n"); for(j=0;j<i;j++) { printf(" %f\t%f",x[j],y[j]); printf("\n"); } } float fun(float a,float b) { float c; c=a*a+b; return(c); } |

### Input/Output:

Also see,

Modified Euler’s Matlab Program

Modified Euler’s Algorithm/Flowchart

Numerical Methods Tutorial Compilation

Smaller the value of h, higher will be the accuracy of the result obtained from this program for** modified Euler’s method in C**. This method was developed by Leonhard Euler during the 1770s. Modified Euler’s method gives greater improvement in accuracy over the Euler’s method; but it is a bit long and tedious to some extent.