{ prog6a.pas -- Solve First Order ODE by RK2 Method Written Apr 19, 2001 by Eric Olson for Mathematics 483 For information see Section 4.4 in Numerical Analysis for Applied Mathematics, Science, and Engineering, by Greenspan and Casulli } Program main; function Fc(x,y:real):real; begin Fc:=y*sin(x) end; function Yc(x:real):real; begin Yc:=exp(1.0-cos(x)) end; function RK2(x0,y,xn:real;n:integer):real; var h,x,K0,K1:real; i:integer; begin h:=(xn-x0)/n; for i:=1 to n do begin x:=x0+i*h; K0:=h*Fc(x,y); K1:=h*Fc(x+h,y+K0); y:=y+0.5*(K0+K1) end; RK2:=y end; var Ycn,yn,E:real; i,n:integer; begin Ycn:=Yc(10.0); writeln('# n yn err'); n:=16; for i:=4 to 20 do begin yn:=RK2(0.0,1.0,10.0,n); E:=abs(yn-Ycn); writeln(n,yn,E); n:=n*2 end end.