Integration af 1.ordens differentialligning
Euler's metode, Opgave 1 i Afsnit 20.1

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;

Do[y[i]=y[i-1]+h f[x[i-1],y[i-1]],{i,n}]

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

TableForm[z]

g1=ListPlot[z,PlotStyle->{PointSize[0.015],RGBColor[1,0,0]}]

F[x_]=Exp[x]; g2=Plot[F[x],{x,0,1}]

Show[g1,g2]

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

TableForm[w]

Euler's forbedrede metode, Opgave 5

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

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

TableForm[z]

g3=ListPlot[z,PlotStyle->{PointSize[0.015],RGBColor[0,1,0]}]

Show[g1,g2,g3]

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

TableForm[w]

Runge-Kutta metoden, Opgave 9

h=0.2; n=5; f[x_,y_]=x y; Do[x[i]=x[i-1]+h,{i,n}]

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}]

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

TableForm[z]

g4=ListPlot[z,PlotStyle->{PointSize[0.015],RGBColor[0,0,1]}]

F[x_]=Exp[x^2/2]; g5=Plot[F[x],{x,0,1}]

Show[g4,g5]

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

TableForm[w]

Mathematica's numeriske loesning af differentialligningen

Clear[x,y,f]

solution = NDSolve[{y'[x] == y[x], y[0] == 1}, y, {x,0,1}];

x=0; Table[{x=x+h ; y[x] /. solution},{i,n}];

z=Flatten[%]

x=0; Table[{x=x+h; Exp[x]},{i,5}];

w=Flatten[%]

w-z