MapleRungeKutta.mws

Runge-Kutta and Euler's Method Using Maple

Some Comparisons

>    with(plots):

Warning, the name changecoords has been redefined

>    eq1:=diff(y(x),x)=cos(x);

eq1 := diff(y(x),x) = cos(x)

>    ini1:=y(0)=0;

ini1 := y(0) = 0

Here is the analytical solution.

>    dsolve({eq1,ini1},{y(x)});

y(x) = sin(x)

>    AnalSoln:=plot(sin(x),x=0..2*Pi,y=-1.5..1.5,color=blue,thickness=2):

Here is a Runge-Kutta solution (by default).

>    sol:=dsolve({eq1,ini1},{y(x)},type=numeric);

sol := proc (x_rkf45) local res, solnproc, outpoint, ndsol, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; _EnvDSNumericSaveDigits := Digits; Digits := 14; if _EnvInFsolve ...

>    RungeKutta:=odeplot(sol,[x,y(x)],0..2*Pi,thickness=2):

>    sol(Pi/2);

[x = 1.5707963267949, y(x) = .99999926285989026]

It is difficult to visually distinguish between the Runge-Kutta solution and the analytical solution.

>    display(AnalSoln,RungeKutta);

[Maple Plot]

Here are some Euler solutions.

>    sol2:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/32);

sol2 := proc (x_classical) local res, solnproc, outpoint, ndsol, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; _EnvDSNumericSaveDigits := Digits; Digits := 14; if _EnvInFs...

>    EulerSoln2:=odeplot(sol2,[x,y(x)],0..2*Pi,thickness=2):

>    sol2(Pi/2);

[x = 1.5707963267949, y(x) = 1.04828406569741262]

>    display(AnalSoln,EulerSoln2);

[Maple Plot]

>    sol3:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/64);

sol3 := proc (x_classical) local res, solnproc, outpoint, ndsol, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; _EnvDSNumericSaveDigits := Digits; Digits := 14; if _EnvInFs...

>    EulerSoln3:=odeplot(sol3,[x,y(x)],0..2*Pi,thickness=2):

>    sol3(Pi/2);

[x = 1.5707963267949, y(x) = 1.02434288692618925]

>    display(AnalSoln,EulerSoln3);

[Maple Plot]

>    sol4:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/128);

sol4 := proc (x_classical) local res, solnproc, outpoint, ndsol, i; option `Copyright (c) 2000 by Waterloo Maple Inc. All rights reserved.`; _EnvDSNumericSaveDigits := Digits; Digits := 14; if _EnvInFs...

>    EulerSoln4:=odeplot(sol4,[x,y(x)],0..2*Pi,thickness=2):

>    sol4(Pi/2);

[x = 1.5707963267949, y(x) = 1.01222164639518675]

>    display(AnalSoln,EulerSoln4);

[Maple Plot]

>   

>