Mathematics 285 Comparison of Methods

restart;
f:=(x,y)->0.2*sin(x)*y;
x[0]:=0;
y[0]:=1;
n:=8;
h:=5.0/n;
ye[0]:=y[0]:
for i from 0 to n-1 do
   x[i+1]:=x[0]+h*(i+1);
   ye[i+1]:=ye[i]+h*f(x[i],ye[i]);
od:
yi[0]:=y[0]:
for i from 0 to n-1 do
   x[i+1]:=x[0]+h*(i+1);
   yp:=yi[i]+h*f(x[i],yi[i]);
   yi[i+1]:=0.5*(yi[i]+yp+h*f(x[i+1],yp));
od:
yk[0]:=y[0]:
for i from 0 to n-1 do
   x[i+1]:=x[0]+h*(i+1);
   k1:=h*f(x[i],yk[i]);
   k2:=h*f(x[i]+0.5*h,yk[i]+0.5*k1);
   k3:=h*f(x[i]+0.5*h,yk[i]+0.5*k2);
   k4:=h*f(x[i+1],yk[i]+k3);
   yk[i+1]:=yk[i]+(1.0/6.0)*(k1+2*(k2+k3)+k4);
od:

plot([[seq([x[i],ye[i]],i=0..n)],
      [seq([x[i],yi[i]],i=0..n)],
      [seq([x[i],yk[i]],i=0..n)]]);
[x[n], ye[n], yi[n], yk[n]];
Last Updated: Fri Sep 17 11:46:26 PDT 2004