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); |
> | ini1:=y(0)=0; |
Here is the analytical solution.
> | dsolve({eq1,ini1},{y(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); |
> | RungeKutta:=odeplot(sol,[x,y(x)],0..2*Pi,thickness=2): |
> | sol(Pi/2); |
It is difficult to visually distinguish between the Runge-Kutta solution and the analytical solution.
> | display(AnalSoln,RungeKutta); |
Here are some Euler solutions.
> | sol2:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/32); |
> | EulerSoln2:=odeplot(sol2,[x,y(x)],0..2*Pi,thickness=2): |
> | sol2(Pi/2); |
> | display(AnalSoln,EulerSoln2); |
> | sol3:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/64); |
> | EulerSoln3:=odeplot(sol3,[x,y(x)],0..2*Pi,thickness=2): |
> | sol3(Pi/2); |
> | display(AnalSoln,EulerSoln3); |
> | sol4:=dsolve({eq1,ini1},{y(x)},type=numeric,method=classical[foreuler],stepsize=Pi/128); |
> | EulerSoln4:=odeplot(sol4,[x,y(x)],0..2*Pi,thickness=2): |
> | sol4(Pi/2); |
> | display(AnalSoln,EulerSoln4); |
> |
> |