/***********************************************
****EULER METHOD FOR DIFFERENTIAL EQUATIONS*****
***********************************************/
#include<stdio.h>
#include<math.h>
/*Define the RHS of the first order differential equation here(Ex: dy/dx=f(x,y)) */
double f(double x, double y){
return -y+x+1;
}
int main()
{
int i;
double y,xi,yi,xf,h;
yi=1;
xi=0;
xf=1;
h=0.2;
printf("x\t\ty\t\ty'\t\thy'\t\ty+hy'\n");
printf("_________________________________________________\n");
//Begin Euler Routine
while(xi<=xf){
y=yi+h*f(xi,yi);
printf("%0.2lf\t%0.4lf\t%0.4lf\t%0.4lf\t%0.4lf\n",xi,yi,f(xi,yi),h*f(xi,yi),y);
yi=y;
xi=xi+h;
}
return 0;
}
輸出畫面
y y' hy' y+hy'
_________________________________________________
0.00 1.0000 0.0000 0.0000 1.0000
0.20 1.0000 0.2000 0.0400 1.0400
0.40 1.0400 0.3600 0.0720 1.1120
0.60 1.1120 0.4880 0.0976 1.2096
0.80 1.2096 0.5904 0.1181 1.3277
1.00 1.3277 0.6723 0.1345 1.4621
沒有留言:
張貼留言