c programming for Euler’s Method
Euler’s Method Algorithm:
- Start
- Define function
- Get the values of x0, y0, h and xn
*Here x0 and y0 are the initial conditions
h is the interval
xn is the required value - n = (xn – x0)/h + 1
- Start loop from i=1 to n
- y = y0 + h*f(x0,y0)
x = x + h - Print values of y0 and x0
- Check if x < xn
If yes, assign x0 = x and y0 = y
If no, goto 9. - End loop i
- Stop
source code:
#include<stdio.h>
#include<math.h>
float f(float x, float y)
{
float f;
f=(y-x)/(y+x);
return(f);
}
int main()
{
int i,n;
float x0,xn,y0,h,x,y;
printf("\n Enter the initial value of x and y i.e x0 and y0\n");
scanf("%f%f",&x0,&y0);
printf("\nEnter the value of x for which we need to find the value of y\n");
scanf("%f",&xn);
printf("\nEnter the value of h\n");
scanf("%f",&h);
n=((xn-x0)/h)+1;
for(i=1;i<=n;i++)
{
y =y0 + h*f(x0,y0);
x= x + h;
printf(" \n the value of xo=%f",x0);
printf(" \n the value of yo=%f\n",y0);
if(x<xn)
{
x0=x;
y0=y;
}
}
printf("\n the value of y=%f",y0);
return 0;
}
output:
Enter the initial value of x and y i.e x0 and y0
0
1
Enter the value of x for which we need to find the value of y
.1
Enter the value of h
0.02
the value of xo=0.000000
the value of yo=1.000000
the value of xo=0.020000
the value of yo=1.020000
the value of xo=0.040000
the value of yo=1.039231
the value of xo=0.060000
the value of yo=1.057748
the value of xo=0.080000
the value of yo=1.075601
the value of xo=0.100000
the value of yo=1.092832
the value of y=1.092832
Comments
Post a Comment