|
|
rbmix.com |
|
|---|
C program for eulers method for solving a differential equation with intial conditions
to solve
dy/dx = f(x,y)
y(x0) = y0
f(x,y) is being evaluated as a separate function “funct” which can be suitably adjusted
-------------------------------------------------------
#include<stdio.h>
#include<conio.h>
#include<math.h>
float funct(float x,float y)
{
float f;
/*edit f into required function*/
f= (x+y)/2 ;
return (f);
}
void clearline(int i)
{
/*funtion for clearing "i" lines */
int j;
for(j=1; j<=i;j=j+1)
printf("\n");
}
main()
{
float x,y,x0,y0,xn,yn,xcurr=0,ycurr,xnext,ynext,h;
int n=0;
clearline(50);
printf("\nEnter the initial values:x0=");
scanf("%f",&x0);
printf("Enter the initial values:y0=");
scanf("%f",&y0);
printf("Enter the final value of x for which y is required:xn=");
scanf("%f",&xn);
printf("Enter the value ofh, h=");
scanf("%f",&h);
xcurr=x0;
ycurr=y0;
iterat:
if(h>0 && xcurr>=xn)
goto end;
if(h<0 && xcurr<=xn)
goto end;
ynext = ycurr + h*funct(xcurr,ycurr);
xnext = xcurr + h;
printf("y[%f] = %f \n ",xnext,ynext);
xcurr = xnext;
ycurr = ynext;
n=n+1;
if((n%10)==0)
{printf("press enter to continue");
getch();
printf("\n");
}
goto iterat;
end:
getch();
}
-------------------------------------------------------
|
|
-----------------------------------------------------------------------------------------------------------------------------------------
277