> #Einschrittverfahren > restart; > > Eulervorwärts:=proc(y0,t0,f,T,N) > # Schrittweite h=(T-t0)/N > local y,t,j,h; > y:=y0; t:=t0; h:=(T-t0)/N; > print(`Euler-Verfahren:`); > for j from 1 to N do > y:=y+h*f(t,y); t:=t0+j*h; print(t,y) > od > end: > > Eulerverbessert:=proc(y0,t0,f,T,N) > # Schrittweite h=(T-t0)/N > local y,t,k1,k2,j,h; > y:=y0; t:=t0; h:=(T-t0)/N; > print(`verbessertes Euler-Verfahren:`); > for j from 0 to N-1 do > k1:=f(t,y); k2:=f(t+h/2,y+k1*h/2); t:=t+h; > y:=y+k2*h; print(t,y) > od > end: > > Heun:=proc(y0,t0,f,T,N) > # Schrittweite h=(T-t0)/N > local y,t,k1,k2,j,h; > y:=y0; t:=t0; h:=(T-t0)/N; > print(`Heun-Verfahren:`); > for j from 0 to N-1 do > k1:=f(t,y); t:=t+h; k2:=f(t,y+k1*h); > y:=y+h*(k1+k2)/2; print(t,y) > od > end: > > RungeKutta:=proc(y0,t0,f,T,N) > # Schrittweite ist (T-t0)/N > local y,t,k1,k2,k3,k4,j,h; > y:=y0; t:=t0; h:=(T-t0)/N; > print(`Runge-Kutta-Verfahren:`); > for j from 0 to N-1 do > k1:=f(t,y); k2:=f(t+h/2,y+k1*h/2); > k3:=f(t+h/2,y+k2*h/2); t:=t+h; k4:=f(t,y+k3*h); > y:=y+h*(k1+2*k2+2*k3+k4)/6; print(t,y) > od > end: > > #Beispiel 1 > f:=proc(t,y) local g; g:=-2*t*y^2; RETURN(g) end: > Eulervorwärts(1.0,0.0,f,0.3,3); > Eulerverbessert(1.0,0.0,f,0.3,3); > Heun(1.0,0.0,f,0.3,3); > RungeKutta(1.0,0.0,f,0.3,3); > # Vergleich mit genauer Lösung > print(`genaue Lösung:`); for j from 1 to 3 do evalf(1/(1+(j*0.1)^2)) od; Euler-Verfahren: .1000000000, 1.0 .2000000000, .9800000000 .3000000000, .9415840000 verbessertes Euler-Verfahren: .1000000000, .9900000000 .2000000000, .9611762976 .3000000000, .9167422179 Heun-Verfahren: .1000000000, .9900000000 .2000000000, .9613655544 .3000000000, .9172458073 Runge-Kutta-Verfahren: .1000000000, .9900989250 .2000000000, .9615381437 .3000000000, .9174305976 genaue Lösung: .9900990099 .9615384615 .9174311927 > #Beispiel 2 > f:=proc(t,y) local g; g:=y^2; RETURN(g) end: > Eulervorwärts(-4.0,0.0,f,0.3,3); > Eulerverbessert(-4.0,0.0,f,0.3,3); > Heun(-4.0,0.0,f,0.3,3); > RungeKutta(-4.0,0.0,f,0.3,3); > # Vergleich mit genauer Lösung > print(`genaue Lösung:`); for j from 1 to 3 do evalf(-1/(1/4+(j*0.1))) od; Euler-Verfahren: .1000000000, -2.400000000 .2000000000, -1.824000000 .3000000000, -1.491302400 verbessertes Euler-Verfahren: .1000000000, -2.976000000 .2000000000, -2.334304367 .3000000000, -1.909179547 Heun-Verfahren: .1000000000, -2.912000000 .2000000000, -2.275002716 .3000000000, -1.861791260 Runge-Kutta-Verfahren: .1000000000, -2.857341277 .2000000000, -2.222404387 .3000000000, -1.818323046 genaue Lösung: -2.857142857 -2.222222222 -1.818181818