The analytical method of solution of the differential equation is time-consuming and tedious. Sometimes it fails to give accurate solutions 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 the numerical solutions 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. x0 and xn, the initial value of y i.e y0, 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 the initial condition i.e. the initial value of x and y, value of increment h, and the 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’ = x2 + y
- Then, value of x is increased by h i.e. x1 = x0 + h and so on. Using the new value of x and y, new slope is calculated. The mean slope of this newly calculated and the initial slope is calculated.
- The new value of y is : y= y0 + h * slope. The value of x is not increased till the any two consecutive value of y are found to be the same or within 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 the curve and its equations.
Source Code for Modified Euler’s Method in C:
#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
The smaller the value of h, the 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 a greater improvement in accuracy over the Euler’s method; but it is a bit long and tedious to some extent.