Euler's Method C Program for Solving Ordinary Differential Equations
Implementation of Euler's method for solving ordinary differential equation using C programming language.
Output of this is program is solution for dy/dx = x + y with initial condition y = 1 for x = 0 i.e. y(0) = 1 and we are trying to evaluate this differential equation at y = 1. ( Here y = 1 i.e. y(1) = ? is our calculation point)
C Program for Euler's Method
#include<stdio.h>
#include<conio.h>
#define f(x,y) x+y
int main()
{
float x0, y0, xn, h, yn, slope;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Calculating step size (h) */
h = (xn-x0)/n;
/* Euler's Method */
printf("\nx0\ty0\tslope\tyn\n");
printf("------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
getch();
return 0;
}
Euler's Method C Program Output
Enter Initial Condition x0 = 0 y0 = 1 Enter calculation point xn = 1 Enter number of steps: 10 x0 y0 slope yn ------------------------------ 0.0000 1.0000 1.0000 1.1000 0.1000 1.1000 1.2000 1.2200 0.2000 1.2200 1.4200 1.3620 0.3000 1.3620 1.6620 1.5282 0.4000 1.5282 1.9282 1.7210 0.5000 1.7210 2.2210 1.9431 0.6000 1.9431 2.5431 2.1974 0.7000 2.1974 2.8974 2.4872 0.8000 2.4872 3.2872 2.8159 0.9000 2.8159 3.7159 3.1875 Value of y at x = 1.00 is 3.187
Recommended Readings
- Ordinary Differential Equation Using Euler's Method Algorithm
- Ordinary Differential Equation Using Euler's Method Pseudocode
- Ordinary Differential Equation Using Euler's Method Using C Programming
- Ordinary Differential Equation Using Euler's Method Using C++ Programming
- Ordinary Differential Equation Using Fourth Order Runge Kutta (RK) Method Algorithm
- Ordinary Differential Equation Using Fourth Order Runge Kutta (RK) Method Pseudocode
- Ordinary Differential Equation Using Fourth Order Runge Kutta (RK) Method Using C
- Runge Kutta (RK) Fourth Order Using C++ with Output