--Copyright The Numerical Algorithms Group Limited 1994.
-- Two scripts which can be combined to show how accurate the Runge-Kutta
-- method is for simple problems.

-- The first script finds the exact solution for  y'= cos(y)/(2+x)  given
-- the initial value condition.  This is then plotted.

)clear all
y := operator y
deq := D(y x,x) = cos(y x)/(2+x)
solve(deq,y,x=3,[0])
eval (%,y(x)=z)                 
solve(%,z)
%.1
rhs %
draw(%,x=3..4)

-- The second script uses rk4 to solve the same equation form x=3..4
-- Each point is merged into a list which is then plotted.
-- The two graphs can then be superimposed using the pick and drop facilities.

)clear all
y:Vector Float :=[0.0]
x1:=3.0
p0 := point[x1::SF,(y.1)::SF]$(Point SF)
n:=1
h:=0.1
der(d:Vector Float,y:Vector Float,x:Float):Void == setelt(d,1,cos(y.1)/(2+x))
rk4(y,n,x1,h,der)
y
x1:=x1+h
p1 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y
p2 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p3 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p4 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p5 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p6 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p7 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p8 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p9 := point[x1::SF,(y.1)::SF]$(Point SF)
rk4(y,n,x1,h,der);x1:=x1+h;y 
p10 := point[x1::SF,(y.1)::SF]$(Point SF)
llp := [[p0,p1],[p1,p2],[p2,p3],[p3,p4],[p4,p5],_
       [p5,p6],[p6,p7],[p7,p8],[p8,p9],[p9,p10]]
pc2 := dim green()
lpc := [pc2, pc2, pc2, pc2, pc2, pc2, pc2, pc2, pc2, pc2]
lc := [pastel blue(), light yellow(), dim green(),_
       bright red(), light green(),dim yellow(), _
       bright blue(),dark red(), pastel red(), light blue()]
size1 := 4::PositiveInteger
lsize := [size1, size1, size1, size1, size1, size1, size1, size1, size1, size1]
g:= makeGraphImage(llp,lpc,lc,lsize)$GRIMAGE
makeViewport2D(g,[title("RK4")])$VIEW2D


