Euler's Method Algorithm
(Ordinary Differential Equation):
Euler's
Method is a straightforward numerical technique used to approximate solutions
to ordinary differential equations (ODEs). It is particularly useful when an
analytical solution is difficult or impossible to obtain. The method is based
on the idea of using tangent line approximations to advance the solution
step-by-step.
Steps
of Euler's Method:
1.
Start
2.
Define function f(x,y)
3.
Read values of initial condition(x0 and y0), number of steps (n) and
calculation point(xn)
4. Calculate step size (h) = (xn - x0)/b
5.
Set i=0
6.
Loop
yn = y0 + h * f(x0 +
i*h, y0)
y0 = yn
i = i + 1
While i < n
7.
Display yn as result
8.
Stop
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
Regression Method Algorithm Using Least Square Method
Solve the initial value problems using Modified Euler's Method.
Integratea function using numerically using trapezoidal Method.
0 Comments