# Euler. Group 1, initialize. f:=(x,y)->1-x-y: x0:=0:y0:=3:h:=0.2:Dots:=[x0,y0]:n:=2: # Group 2, repeat n times. Euler's method for i from 1 to n do Y:=y0+h*f(x0,y0); x0:=x0+h:y0:=Y:Dots:=Dots,[x0,y0]; od: # Group 3, display dots and plot. P1:=Dots; plot([Dots]); # Heun. Group 1, initialize. f:=(x,y)->1-x-y: x0:=0:y0:=3:h:=0.2:Dots:=[x0,y0]:n:=2: # Group 2, repeat n times. Heun method. for i from 1 to n do Y1:=y0+h*f(x0,y0); Y:=y0+h*(f(x0,y0)+f(x0+h,Y1))/2: x0:=x0+h:y0:=Y:Dots:=Dots,[x0,y0]; od: # Group 3, display dots and plot. Dots; plot([Dots]); P2:=Dots; # RK4. Group 1, initialize. f:=(x,y)->1-x-y: x0:=0:y0:=3:h:=0.2:Dots:=[x0,y0]:n:=2: # Group 2, repeat n times. RK4 method. for i from 1 to n do k1:=h*f(x0,y0): k2:=h*f(x0+h/2,y0+k1/2): k3:=h*f(x0+h/2,y0+k2/2): k4:=h*f(x0+h,y0+k3): Y:=y0+(k1+2*k2+2*k3+k4)/6: x0:=x0+h:y0:=Y:Dots:=Dots,[x0,y0]; od: # Group 3, display some dots and plot. P3:=Dots; plot([Dots]); plot([[P1],[P2],[P3],2-x+exp(-x)],x=0.38..0.4,y=2.24..2.31,color=[red,blue,green,black],legend=["Euler","Heun","RK4","Exact"]);