I'm trying to compare (graphically) approximations obtained using Euler's method and RK4-method.
ode := {diff(y(x), x) = 2*cos(x)*y(x), y(0) = 1};
p := dsolve(ode, y(x), numeric, method = classical[rk4], stepsize = .25);
f := dsolve(ode, y(x), numeric, method = classical[foreuler], stepsize = .25)
But can't plot them together.
plots:-odeplot([p, f], x = 0 .. 10);
Error, (in plots/odeplot) input is not a valid dsolve/numeric solution
How can I solve this problem? Besides, I want to use style=point for "euler" curve, and plot exact solution on the same graph, if it's possible. What is the best method to obtain exact solution?
Any help would be appreciated.
The key to answering you question is that you can produce the plots separately (using
odeplotorplot), and then combine them together using theplots:-displaycommand.For fun, let's plot the two numeric methods with
style=point, at the x-values that match the fixed step-size of the forward-Euler method.I find it more convenient to use
plotinstead ofplots:-odeplothere, after using theoutput=listprocedureoption ofdsolve(numeric).You can issue the commands
eval(y(x),e)andeval(y(x),f)separately, to see that they are a syntax for picking off the RHS expression or procedure from thedsolvesolution.