Integration af 1.ordens differentialligning
Runga-Kutta sammenlignet med Adams-Moulton
Opgave 2 i Afsnit 20.2

Clear[x,y,f];

h=0.1; n=10;

x[0]=0; Do[x[i]=x[i-1]+h,{i,n}]

f[x_,y_]=y;  y[0]=1;

F[x_]=Exp[x];

Do[{
k1 = h f[x[i-1],y[i-1]]; 
k2 = h f[x[i-1]+0.5h,y[i-1]+0.5k1];
k3 = h f[x[i-1]+0.5h,y[i-1]+0.5k2];
k4 = h f[x[i],y[i-1]+k3];
y[i] = y[i-1] + (k1+2k2+2k3+k4)/6},{i,n}]

w=Table[{x[i-1],y[i-1],F[x[i-1]],F[x[i-1]]-y[i-1]},{i,n+1}];

TableForm[w]

Table[{x[i-1],Part[w,i,4]},{i,n+1}];

g1=ListPlot[%]

Adams-Moulton metode med Runga-Kutta tilnaermelsen som start

Do[ff[i-1]=f[x[i-1],y[i-1]],{i,4}];

Do[{
yst[i]=y[i-1]+(h/24)(55ff[i-1]-59ff[i-2]+37ff[i-3]-9ff[i-4]),
y[i]=y[i-1]+(h/24)(9f[x[i],yst[i]]+19ff[i-1]-5ff[i-2]+ff[i-3]),
ff[i]=f[x[i],y[i]]},
{i,4,n}]

z=Table[{x[i-1],y[i-1],F[x[i-1]],F[x[i-1]]-y[i-1]},{i,n+1}];

TableForm[z]

Table[{x[i-1],Part[z,i,4]},{i,n+1}];

g2=ListPlot[%,PlotStyle->{RGBColor[1,0,0]}]

Show[g1,g2]