Integration af 2.ordens differentialligning

Simpel ("Euler") metode:

Clear[y,x,f,F]

f[x_,y_,y1_]=0.5(x+y+y1+2);

F[x_]=Exp[x]-x-1;

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

y[0]=0; y1[0]=0; y2[0]=f[0,y[0],y1[0]];

Do[{
y[i] = y[i-1] + h y1[i-1] + (h^2/2) y2[i-1], 
y1[i] = y1[i-1] + h y2[i-1],
y2[i] = f[x[i],y[i],y1[i]]},
{i,n}]

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

TableForm[z]

Runge-Kutta-Nystrom metoden

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

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

TableForm[w]