Problem: Here we have to find dy/dx=x+y where y(0)=1 at the point x=0.05 and x=0.10 taking h=0.05
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
Code:
#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t %0.3f\n",x,y);
}
}
Output:
