Input : eq = , y(0) = 0.5, step size(h) = 0.2
#include <stdio.h>
#include <math.h>
#define F(y,t) (y-(2*t*t)+1)
int main()
{
int i,n=5;
double a=0.0,b=1.0,y0=0.5,k1,k2,h,y,t;
h=(b-a)/n;
t=a;
y=y0;
printf("t y(t) k1 k2\n");
printf("=========================================\n");
printf("%.2lf %10.6lf %10.6lf %10.6lf\n",t,y);
for(i=1;i<=n;i++)
{
k1=h*F(y,t);
k2=h*F((y+k1),(t+h));
y=y+(k1+k2)/2.0;
t=a+i*h;
printf("%.2lf %10.6lf %10.6lf %10.6lf\n",t,y,k1,k2);
}
return 0;
}
輸出畫面
t y(t) k1 k2
==================================
0.00 0.500000 0.000000 0.000000
0.20 0.822000 0.300000 0.344000
0.40 1.181240 0.348400 0.370080
0.60 1.550713 0.372248 0.366698
0.80 1.897470 0.366143 0.327371
1.00 2.181313 0.323494 0.244193
沒有留言:
張貼留言